aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Auth/ForteeAuth.php
diff options
context:
space:
mode:
Diffstat (limited to 'services/app/src/Auth/ForteeAuth.php')
-rw-r--r--services/app/src/Auth/ForteeAuth.php50
1 files changed, 50 insertions, 0 deletions
diff --git a/services/app/src/Auth/ForteeAuth.php b/services/app/src/Auth/ForteeAuth.php
new file mode 100644
index 0000000..4f711be
--- /dev/null
+++ b/services/app/src/Auth/ForteeAuth.php
@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Albatross\Auth;
+
+final class ForteeAuth implements AuthProviderInterface
+{
+ public function __construct(
+ private string $apiEndpoint,
+ ) {
+ }
+
+ public function login(string $username, string $password): AuthenticationResult
+ {
+ $query_params = [
+ 'username' => $username,
+ 'password' => $password,
+ ];
+ $context = stream_context_create([
+ 'http' => [
+ 'method' => 'POST',
+ 'follow_location' => 0,
+ 'header' => [
+ 'Content-type: application/x-www-form-urlencoded',
+ 'Accept: application/json',
+ ],
+ 'timeout' => 5.0,
+ 'content' => http_build_query($query_params),
+ ],
+ ]);
+ $result = file_get_contents(
+ $this->apiEndpoint . '/api/user/login',
+ context: $context,
+ );
+ if ($result === false) {
+ return AuthenticationResult::UnknownError;
+ }
+ $result = json_decode($result, true);
+ if (!is_array($result)) {
+ return AuthenticationResult::InvalidJson;
+ }
+ $ok = ($result['loggedIn'] ?? null) === true;
+ if ($ok) {
+ return AuthenticationResult::Success;
+ } else {
+ return AuthenticationResult::InvalidCredentials;
+ }
+ }
+}