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 | * @var KeycloakEntitlements |
||
62 | */ |
||
63 | private $keycloakEntitlements = null; |
||
64 | |||
65 | /** |
||
66 | * Constructs an OAuth 2.0 service provider. |
||
67 | * |
||
68 | * @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 | 34 | public function __construct(array $options = [], array $collaborators = []) |
|
84 | |||
85 | /** |
||
86 | * We need to cache the access token locally allowing for later optional post-processing |
||
87 | * by `checkForKeycloakRoles()` |
||
88 | * |
||
89 | * @param mixed $grant |
||
90 | * @param array $options |
||
91 | * @return AccessToken |
||
92 | */ |
||
93 | 10 | public function getAccessToken($grant, array $options = []) |
|
98 | |||
99 | /** |
||
100 | * Check for Keycloak-supplied additional fields held by the access token which in turn is inside accessToken. |
||
101 | * |
||
102 | * @return KeyCloakRoles |
||
103 | */ |
||
104 | 1 | 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 | 1 | public function getKeycloakEntitlements() |
|
140 | |||
141 | /** |
||
142 | * Attempts to decrypt the given response. |
||
143 | * |
||
144 | * @param string|array|null $response |
||
145 | * @return array|null|string |
||
146 | * @throws EncryptionConfigurationException |
||
147 | */ |
||
148 | 6 | public function decryptResponse($response) |
|
173 | |||
174 | /** |
||
175 | * Get authorization url to begin OAuth flow |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | 6 | public function getBaseAuthorizationUrl() |
|
183 | |||
184 | /** |
||
185 | * Get access token url to retrieve token |
||
186 | * |
||
187 | * @param array $params |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | 12 | public function getBaseAccessTokenUrl(array $params) |
|
195 | |||
196 | /** |
||
197 | * Get provider url to fetch user details |
||
198 | * |
||
199 | * @param AccessToken $token |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 6 | public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
207 | |||
208 | /** |
||
209 | * Keycloak extension supporting entitlements. |
||
210 | * |
||
211 | * @param AccessToken $token |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getEntitlementsUrl(AccessToken $token) |
||
218 | |||
219 | /** |
||
220 | * Creates base url from provider configuration. |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | 18 | protected function getBaseUrlWithRealm() |
|
228 | |||
229 | /** |
||
230 | * Get the default scopes used by this provider. |
||
231 | * |
||
232 | * This should not be a complete list of all scopes, but the minimum |
||
233 | * required for the provider user interface! |
||
234 | * |
||
235 | * @return string[] |
||
236 | */ |
||
237 | 4 | protected function getDefaultScopes() |
|
241 | |||
242 | 6 | protected function getAuthorizationHeaders($token = null) |
|
250 | |||
251 | /** |
||
252 | * Check a provider response for errors. |
||
253 | * |
||
254 | * @throws IdentityProviderException |
||
255 | * @param ResponseInterface $response |
||
256 | * @param string $data Parsed response data |
||
257 | * @return void |
||
258 | */ |
||
259 | 10 | protected function checkResponse(ResponseInterface $response, $data) |
|
266 | |||
267 | /** |
||
268 | * Generate a user object from a successful user details request. |
||
269 | * |
||
270 | * @param array $response |
||
271 | * @param AccessToken $token |
||
272 | * @return KeycloakResourceOwner |
||
273 | */ |
||
274 | 4 | protected function createResourceOwner(array $response, AccessToken $token) |
|
278 | |||
279 | /** |
||
280 | * Requests and returns the resource owner of given access token. |
||
281 | * |
||
282 | * @param AccessToken $token |
||
283 | * @return KeycloakResourceOwner |
||
284 | */ |
||
285 | 6 | public function getResourceOwner(AccessToken $token) |
|
293 | |||
294 | /** |
||
295 | * Updates expected encryption algorithm of Keycloak instance. |
||
296 | * |
||
297 | * @param string $encryptionAlgorithm |
||
298 | * |
||
299 | * @return Keycloak |
||
300 | */ |
||
301 | 4 | public function setEncryptionAlgorithm($encryptionAlgorithm) |
|
307 | |||
308 | /** |
||
309 | * Updates expected encryption key of Keycloak instance. |
||
310 | * |
||
311 | * @param string $encryptionKey |
||
312 | * |
||
313 | * @return Keycloak |
||
314 | */ |
||
315 | 4 | public function setEncryptionKey($encryptionKey) |
|
321 | |||
322 | /** |
||
323 | * Updates expected encryption key of Keycloak instance to content of given |
||
324 | * file path. |
||
325 | * |
||
326 | * @param string $encryptionKeyPath |
||
327 | * |
||
328 | * @return Keycloak |
||
329 | */ |
||
330 | 2 | public function setEncryptionKeyPath($encryptionKeyPath) |
|
340 | } |
||
341 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.