Passed
Push — main ( 858704...ca7407 )
by José
07:45
created

Lichess::getDefaultCodeChallengeMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 7
    public function __construct(array $options = [ ], array $collaborators = [ ])
23
    {
24 7
        parent::__construct($options, $collaborators);
25
    }
26
27 2
    public function getBaseAuthorizationUrl(): string
28
    {
29 2
        return 'https://lichess.org/oauth';
30
    }
31
32 3
    public function getBaseAccessTokenUrl(array $params): string
33
    {
34 3
        return 'https://lichess.org/api/token';
35
    }
36
37 2
    public function getResourceOwnerDetailsUrl(AccessToken $token): string
38
    {
39 2
        return 'https://lichess.org/api/account';
40
    }
41
42 1
    protected function getDefaultScopes(): array
43
    {
44 1
        return [];
45
    }
46
47 1
    protected function getScopeSeparator(): string
48
    {
49 1
        return ' ';
50
    }
51
52 1
    protected function getDefaultCodeChallengeMethod(): string
53
    {
54 1
        return 'S256';
55
    }
56
57
    /**
58
     * Returns authorization parameters based on provided options.
59
     * Added Code Challenge Method
60
     *
61
     * @param  array $options
62
     * @return array Authorization parameters
63
     */
64 1
    protected function getAuthorizationParameters(array $options)
65
    {
66 1
        if (empty($options['code_challenge_method'])) {
67 1
            $options['code_challenge_method'] = $this->getDefaultCodeChallengeMethod();
68
        }
69
70 1
        return parent::getAuthorizationParameters($options);
71
    }
72
73
    /**
74
     * @param array<string, mixed>|string $data
75
     * @throws IdentityProviderException
76
     */
77 3
    protected function checkResponse(ResponseInterface $response, $data)
78
    {
79 3
        if ($response->getStatusCode() !== 200) {
80 1
            $errorDescription = '';
81 1
            $error = '';
82 1
            if (\is_array($data) && !empty($data)) {
83 1
                $errorDescription = $data['error_description'] ?? $data['message'];
84 1
                $error = $data['error'];
85
            }
86 1
            throw new IdentityProviderException(
87 1
                sprintf("%d - %s: %s", $response->getStatusCode(), $error, $errorDescription),
88 1
                $response->getStatusCode(),
89
                $data
90
            );
91
        }
92
    }
93
94 1
    protected function createResourceOwner(array $response, AccessToken $token): User
95
    {
96 1
        return new User($response);
97
    }
98
}
99