ApereoCas   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 23
c 2
b 0
f 1
dl 0
loc 157
rs 10
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultScopes() 0 10 1
A getBaseAccessTokenUrl() 0 3 1
A getBaseUrl() 0 3 1
A getBaseAuthorizationUrl() 0 3 1
A parseResponse() 0 3 1
A getResourceOwnerDetailsUrl() 0 3 1
A checkResponse() 0 8 3
A getResourceOwner() 0 5 1
A createResourceOwner() 0 3 1
A __construct() 0 3 1
A getScopeSeparator() 0 3 1
1
<?php
2
3
namespace Ajtak\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use League\OAuth2\Client\Token\AccessToken;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use Psr\Http\Message\ResponseInterface;
10
use UnexpectedValueException;
11
12
class ApereoCas extends AbstractProvider
13
{
14
    use BearerAuthorizationTrait;
15
16
    /**
17
     * Keycloak URL, eg. http://localhost:8080/auth.
18
     *
19
     * @var string
20
     */
21
    public $authServerUrl = null;
22
23
    /**
24
     * Constructs an OAuth 2.0 service provider.
25
     *
26
     * @param array $options An array of options to set on this provider.
27
     *     Options include `clientId`, `clientSecret`, `redirectUri`, and `state`.
28
     *     Individual providers may introduce more options, as needed.
29
     * @param array $collaborators An array of collaborators that may be used to
30
     *     override this provider's default behavior. Collaborators include
31
     *     `grantFactory`, `requestFactory`, `httpClient`, and `randomFactory`.
32
     *     Individual providers may introduce more collaborators, as needed.
33
     */
34
    public function __construct(array $options = [], array $collaborators = [])
35
    {
36
        parent::__construct($options, $collaborators);
37
    }
38
39
    /**
40
     * Get authorization url to begin OAuth flow
41
     *
42
     * @return string
43
     */
44
    public function getBaseAuthorizationUrl(): string
45
    {
46
        return $this->getBaseUrl() . '/oidc/authorize';
47
    }
48
49
    /**
50
     * Get access token url to retrieve token
51
     *
52
     * @param array $params
53
     *
54
     * @return string
55
     */
56
    public function getBaseAccessTokenUrl(array $params)
57
    {
58
        return $this->getBaseUrl() . '/oidc/accessToken';
59
    }
60
61
    /**
62
     * Get provider url to fetch user details
63
     *
64
     * @param AccessToken $token
65
     *
66
     * @return string
67
     */
68
    public function getResourceOwnerDetailsUrl(AccessToken $token)
69
    {
70
        return $this->getBaseUrl() . '/oidc/profile';
71
    }
72
73
    /**
74
     * Creates base url from provider configuration.
75
     *
76
     * @return string
77
     */
78
    protected function getBaseUrl()
79
    {
80
        return $this->authServerUrl;
81
    }
82
83
    /**
84
     * Get the default scopes used by this provider.
85
     *
86
     * This should not be a complete list of all scopes, but the minimum
87
     * required for the provider user interface!
88
     *
89
     * @return string[]
90
     */
91
    protected function getDefaultScopes()
92
    {
93
        $scopes = [
94
            'openid',
95
            'profile',
96
            'email'
97
        ];
98
99
100
        return $scopes;
101
    }
102
103
    /**
104
     * Returns the string that should be used to separate scopes when building
105
     * the URL for requesting an access token.
106
     *
107
     * @return string Scope separator, defaults to ','
108
     */
109
    protected function getScopeSeparator()
110
    {
111
        return ' ';
112
    }
113
114
115
    /**
116
     * Check a provider response for errors.
117
     *
118
     * @param ResponseInterface $response
119
     * @param string $data Parsed response data
120
     * @return void
121
     * @throws IdentityProviderException
122
     */
123
    protected function checkResponse(ResponseInterface $response, $data)
124
    {
125
        if (!empty($data['error'])) {
126
            $error = $data['error'];
127
            if (isset($data['error_description'])) {
128
                $error .= ': ' . $data['error_description'];
129
            }
130
            throw new IdentityProviderException($error, 0, $data);
131
        }
132
    }
133
134
    /**
135
     * Generate a user object from a successful user details request.
136
     *
137
     * @param array $response
138
     * @param AccessToken $token
139
     * @return ApereoCasResourceOwner
140
     */
141
    protected function createResourceOwner(array $response, AccessToken $token)
142
    {
143
        return new ApereoCasResourceOwner($response);
144
    }
145
146
    /**
147
     * Requests and returns the resource owner of given access token.
148
     *
149
     * @param AccessToken $token
150
     * @return ApereoCasResourceOwner
151
     */
152
    public function getResourceOwner(AccessToken $token)
153
    {
154
        $response = $this->fetchResourceOwnerDetails($token);
155
156
        return $this->createResourceOwner($response, $token);
157
    }
158
159
    /**
160
     * Parses the response according to its content-type header.
161
     *
162
     * @param ResponseInterface $response
163
     * @return array
164
     * @throws UnexpectedValueException
165
     */
166
    protected function parseResponse(ResponseInterface $response): array
167
    {
168
        return parent::parseResponse($response);
169
    }
170
}
171