1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;
}
}
}
|