1
|
|
|
<?php |
2
|
|
|
namespace CrudSys\OAuth2\Client\Provider; |
3
|
|
|
|
4
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
5
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
6
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
7
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use CrudSys\OAuth2\Client\Entity\User; |
10
|
|
|
|
11
|
|
|
class Lichess extends AbstractProvider |
12
|
|
|
{ |
13
|
|
|
use BearerAuthorizationTrait; |
14
|
|
|
|
15
|
|
|
public const SCOPE_PREFERENCE_READ = 'preference:read'; |
16
|
|
|
public const SCOPE_PREFERENCE_WRITE = 'preference:write'; |
17
|
|
|
public const SCOPE_EMAIL = 'email:read'; |
18
|
|
|
public const SCOPE_CHALLENGE_READ = 'challenge:read'; |
19
|
|
|
public const SCOPE_CHALLENGE_WRITE = 'challenge:write'; |
20
|
|
|
public const SCOPE_CHALLENGE_BULK = 'challenge:bulk'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Code Challenge Method. Defaults to S256. Available options are: |
24
|
|
|
* * S256 |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $code_challenge_method; |
28
|
|
|
|
29
|
7 |
|
public function __construct(array $options = [ ], array $collaborators = [ ]) |
30
|
|
|
{ |
31
|
7 |
|
$options['code_challenge_method'] = 'S256'; |
32
|
7 |
|
parent::__construct($options, $collaborators); |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public function getBaseAuthorizationUrl(): string |
36
|
|
|
{ |
37
|
2 |
|
return 'https://lichess.org/oauth'; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
public function getBaseAccessTokenUrl(array $params): string |
41
|
|
|
{ |
42
|
3 |
|
return 'https://lichess.org/api/token'; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token): string |
46
|
|
|
{ |
47
|
2 |
|
return 'https://lichess.org/api/account'; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
protected function getDefaultScopes(): array |
51
|
|
|
{ |
52
|
1 |
|
return []; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
protected function getScopeSeparator(): string |
56
|
|
|
{ |
57
|
1 |
|
return ' '; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array<string, mixed>|string $data |
62
|
|
|
* @throws IdentityProviderException |
63
|
|
|
*/ |
64
|
3 |
|
protected function checkResponse(ResponseInterface $response, $data) |
65
|
|
|
{ |
66
|
3 |
|
if ($response->getStatusCode() !== 200) { |
67
|
1 |
|
$errorDescription = ''; |
68
|
1 |
|
$error = ''; |
69
|
1 |
|
if (\is_array($data) && !empty($data)) { |
70
|
1 |
|
$errorDescription = $data['error_description'] ?? $data['message']; |
71
|
1 |
|
$error = $data['error']; |
72
|
|
|
} |
73
|
1 |
|
throw new IdentityProviderException( |
74
|
1 |
|
sprintf("%d - %s: %s", $response->getStatusCode(), $error, $errorDescription), |
75
|
1 |
|
$response->getStatusCode(), |
76
|
|
|
$data |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
protected function createResourceOwner(array $response, AccessToken $token): User |
82
|
|
|
{ |
83
|
1 |
|
return new User($response); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|