ChasterApp::getScopeSeparator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 1
c 2
b 1
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/** @noinspection PhpIllegalPsrClassPathInspection */
4
5
namespace Austomos\OAuth2\Client\Provider;
6
7
use League\OAuth2\Client\Provider\AbstractProvider;
8
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
9
use League\OAuth2\Client\Token\AccessToken;
10
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
11
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
12
use Psr\Http\Message\ResponseInterface;
13
14
/**
15
 *
16
 */
17
class ChasterApp extends AbstractProvider
18
{
19
    use ArrayAccessorTrait;
20
    use BearerAuthorizationTrait;
21
22
    /**
23
     * OAuth 2 Base URL used by Chaster API
24
     * @var string
25
     */
26
    protected string $baseAuthUrl = 'https://sso.chaster.app/auth/realms/app/protocol/openid-connect';
27
28
    /**
29
     * API Domain used for endpoints
30
     * @link https://api.chaster.app/api
31
     * @var string
32
     */
33
    protected string $apiDomain = 'https://api.chaster.app';
34
35
    /**
36
     * Returns the base URL for requesting an access token.
37
     *
38
     * @param array $params
39
     *
40
     * @return string
41
     */
42
    public function getBaseAccessTokenUrl(array $params): string
43
    {
44
        return $this->baseAuthUrl . '/token';
45
    }
46
47
    /**
48
     * Returns the base URL for authorizing a client.
49
     *
50
     * @return string
51
     */
52
    public function getBaseAuthorizationUrl(): string
53
    {
54
        return $this->baseAuthUrl . '/auth';
55
    }
56
57
    /**
58
     * Returns the URL for requesting the resource owner's details.
59
     *
60
     * By default, the owner's details is an auth request to /auth/profile endpoint
61
     *
62
     * @param AccessToken $token
63
     *
64
     * @return string
65
     */
66
    public function getResourceOwnerDetailsUrl(AccessToken $token): string
67
    {
68
        return $this->apiDomain . '/auth/profile';
69
    }
70
71
    /**
72
     * Check a provider response for errors.
73
     *
74
     * @param ResponseInterface $response
75
     * @param array $data Parsed response data
76
     *
77
     * @return void
78
     *
79
     * @throws IdentityProviderException
80
     */
81
    protected function checkResponse(ResponseInterface $response, $data): void
82
    {
83
        if ($response->getStatusCode() >= 400) {
84
            throw new IdentityProviderException(
85
                $data['message'] ?? $response->getReasonPhrase(),
86
                $response->getStatusCode(),
87
                $data
88
            );
89
        }
90
    }
91
92
    /**
93
     * Generate a user object from a successful user details request.
94
     *
95
     * @param array $response
96
     * @param AccessToken $token
97
     *
98
     * @return ChasterAppResourceOwner
99
     */
100
    protected function createResourceOwner(array $response, AccessToken $token): ChasterAppResourceOwner
101
    {
102
        return new ChasterAppResourceOwner($response);
103
    }
104
105
    /**
106
     * Get the default scopes used by API.
107
     *
108
     * This should only be the scopes that are required to request the details
109
     * of the resource owner, rather than all the available scopes.
110
     *
111
     * The default scope required for owner's details is only 'profile'
112
     *
113
     * @link https://docs.chaster.app/api-scopes
114
     *
115
     * @return array
116
     */
117
    protected function getDefaultScopes(): array
118
    {
119
        return ['profile'];
120
    }
121
122
    /**
123
     * Returns the string that should be used to separate scopes when building
124
     * the URL for requesting an access token.
125
     *
126
     * @return string Scope separator, defaults to ' '
127
     */
128
    protected function getScopeSeparator(): string
129
    {
130
        return ' ';
131
    }
132
}
133