1 | <?php |
||
14 | class Keycloak extends AbstractProvider |
||
15 | { |
||
16 | use BearerAuthorizationTrait; |
||
17 | |||
18 | /** |
||
19 | * Keycloak URL, eg. http://localhost:8080/auth. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | public $authServerUrl = null; |
||
24 | |||
25 | /** |
||
26 | * Realm name, eg. demo. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | public $realm = null; |
||
31 | |||
32 | /** |
||
33 | * Encryption algorithm. |
||
34 | * |
||
35 | * You must specify supported algorithms for your application. See |
||
36 | * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40 |
||
37 | * for a list of spec-compliant algorithms. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | public $encryptionAlgorithm = null; |
||
42 | |||
43 | /** |
||
44 | * Encryption key. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | public $encryptionKey = null; |
||
49 | /** |
||
50 | * Access Token once authenticated. |
||
51 | * |
||
52 | * @var AccessToken |
||
53 | */ |
||
54 | protected $accessToken = null; |
||
55 | |||
56 | /** |
||
57 | * @var KeyCloakRoles Any roles obtained from the access token. |
||
58 | */ |
||
59 | private $keycloakRoles = null; |
||
60 | /** |
||
61 | 24 | * @var KeycloakEntitlements |
|
62 | */ |
||
63 | 24 | private $keycloakEntitlements = null; |
|
64 | 2 | ||
65 | 2 | /** |
|
66 | 1 | * Constructs an OAuth 2.0 service provider. |
|
67 | 24 | * |
|
68 | 24 | * @param array $options An array of options to set on this provider. |
|
69 | * Options include `clientId`, `clientSecret`, `redirectUri`, and `state`. |
||
70 | * Individual providers may introduce more options, as needed. |
||
71 | * @param array $collaborators An array of collaborators that may be used to |
||
72 | * override this provider's default behavior. Collaborators include |
||
73 | * `grantFactory`, `requestFactory`, `httpClient`, and `randomFactory`. |
||
74 | * Individual providers may introduce more collaborators, as needed. |
||
75 | */ |
||
76 | public function __construct(array $options = [], array $collaborators = []) |
||
77 | 6 | { |
|
78 | if (isset($options['encryptionKeyPath'])) { |
||
79 | 6 | $this->setEncryptionKeyPath($options['encryptionKeyPath']); |
|
80 | 4 | unset($options['encryptionKeyPath']); |
|
81 | 2 | } |
|
82 | 1 | parent::__construct($options, $collaborators); |
|
83 | 2 | } |
|
84 | 1 | ||
85 | 2 | /** |
|
86 | 2 | * We need to cache the access token locally allowing for later optional post-processing |
|
87 | 1 | * by `checkForKeycloakRoles()` |
|
88 | 1 | * |
|
89 | 1 | * @param mixed $grant |
|
90 | 1 | * @param array $options |
|
91 | 1 | * @return AccessToken |
|
92 | 2 | */ |
|
93 | public function getAccessToken($grant, array $options = []) |
||
98 | 1 | ||
99 | /** |
||
100 | 4 | * Check for Keycloak-supplied additional fields held by the access token which in turn is inside accessToken. |
|
101 | * |
||
102 | * @return KeyCloakRoles |
||
103 | */ |
||
104 | public function getKeycloakRoles() |
||
112 | |||
113 | /** |
||
114 | * Obtain the Keycloak entitlements (permissions) this authenticated user has for this resource (by client-id). |
||
115 | * |
||
116 | * This uses the Entitlement API offered by Keycloak. |
||
117 | * @return KeycloakEntitlements Entitlements in a convenient wrapper model |
||
118 | */ |
||
119 | public function getKeycloakEntitlements() |
||
140 | |||
141 | /** |
||
142 | 18 | * Attempts to decrypt the given response. |
|
143 | * |
||
144 | 18 | * @param string|array|null $response |
|
145 | * @return array|null|string |
||
146 | * @throws EncryptionConfigurationException |
||
147 | */ |
||
148 | public function decryptResponse($response) |
||
173 | |||
174 | 8 | /** |
|
175 | * Builds the logout URL. |
||
176 | * |
||
177 | * @param array $options |
||
178 | * @return string Authorization URL |
||
179 | */ |
||
180 | public function getLogoutUrl(array $options = []) |
||
187 | |||
188 | /** |
||
189 | * Get logout url to logout of session token |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getBaseLogoutUrl() |
||
197 | |||
198 | 6 | /** |
|
199 | * Get authorization url to begin OAuth flow |
||
200 | 4 | * |
|
201 | * @return string |
||
202 | */ |
||
203 | public function getBaseAuthorizationUrl() |
||
207 | |||
208 | /** |
||
209 | * Get access token url to retrieve token |
||
210 | 4 | * |
|
211 | * @param array $params |
||
212 | 4 | * |
|
213 | * @return string |
||
214 | 4 | */ |
|
215 | public function getBaseAccessTokenUrl(array $params) |
||
219 | |||
220 | /** |
||
221 | * Get provider url to fetch user details |
||
222 | * |
||
223 | * @param AccessToken $token |
||
224 | 4 | * |
|
225 | * @return string |
||
226 | 4 | */ |
|
227 | public function getResourceOwnerDetailsUrl(AccessToken $token) |
||
231 | |||
232 | /** |
||
233 | * Keycloak extension supporting entitlements. |
||
234 | * |
||
235 | * @param AccessToken $token |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getEntitlementsUrl(AccessToken $token) |
||
242 | 2 | ||
243 | 1 | /** |
|
244 | * Creates base url from provider configuration. |
||
245 | * |
||
246 | * @return string |
||
247 | 2 | */ |
|
248 | protected function getBaseUrlWithRealm() |
||
252 | |||
253 | /** |
||
254 | * Get the default scopes used by this provider. |
||
255 | * |
||
256 | * This should not be a complete list of all scopes, but the minimum |
||
257 | * required for the provider user interface! |
||
258 | * |
||
259 | * @return string[] |
||
260 | */ |
||
261 | protected function getDefaultScopes() |
||
265 | |||
266 | /** |
||
267 | * Check a provider response for errors. |
||
268 | * |
||
269 | * @throws IdentityProviderException |
||
270 | * @param ResponseInterface $response |
||
271 | * @param string $data Parsed response data |
||
272 | * @return void |
||
273 | */ |
||
274 | protected function checkResponse(ResponseInterface $response, $data) |
||
281 | |||
282 | /** |
||
283 | * Generate a user object from a successful user details request. |
||
284 | * |
||
285 | * @param array $response |
||
286 | * @param AccessToken $token |
||
287 | * @return KeycloakResourceOwner |
||
288 | */ |
||
289 | protected function createResourceOwner(array $response, AccessToken $token) |
||
293 | |||
294 | /** |
||
295 | * Requests and returns the resource owner of given access token. |
||
296 | * |
||
297 | * @param AccessToken $token |
||
298 | * @return KeycloakResourceOwner |
||
299 | */ |
||
300 | public function getResourceOwner(AccessToken $token) |
||
308 | |||
309 | /** |
||
310 | * Updates expected encryption algorithm of Keycloak instance. |
||
311 | * |
||
312 | * @param string $encryptionAlgorithm |
||
313 | * |
||
314 | * @return Keycloak |
||
315 | */ |
||
316 | public function setEncryptionAlgorithm($encryptionAlgorithm) |
||
322 | |||
323 | /** |
||
324 | * Updates expected encryption key of Keycloak instance. |
||
325 | * |
||
326 | * @param string $encryptionKey |
||
327 | * |
||
328 | * @return Keycloak |
||
329 | */ |
||
330 | public function setEncryptionKey($encryptionKey) |
||
336 | |||
337 | /** |
||
338 | * Updates expected encryption key of Keycloak instance to content of given |
||
339 | * file path. |
||
340 | * |
||
341 | * @param string $encryptionKeyPath |
||
342 | * |
||
343 | * @return Keycloak |
||
344 | */ |
||
345 | public function setEncryptionKeyPath($encryptionKeyPath) |
||
355 | } |
||
356 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.