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 by `checkForKeycloakRoles()` |
||
87 | * |
||
88 | * @param mixed $grant |
||
89 | * @param array $options |
||
90 | * @return AccessToken |
||
91 | */ |
||
92 | 10 | public function getAccessToken($grant, array $options = []) |
|
97 | |||
98 | /** |
||
99 | * Check for Keycloak-supplied additional fields held by the access token which in turn is inside accessToken. |
||
100 | * |
||
101 | */ |
||
102 | public function checkForKeycloakRoles() { |
||
107 | |||
108 | /** |
||
109 | * @return KeyCloakRoles |
||
110 | */ |
||
111 | public function getKeycloakRoles() |
||
115 | |||
116 | /** |
||
117 | * Obtain the entitlements (permissions) this authenticated user has for this resource (by client-id). |
||
118 | * |
||
119 | * This uses the Entitlement API offered by Keycloak. |
||
120 | * @return KeycloakEntitlements Entitlements in a convenient wrapper model |
||
121 | */ |
||
122 | 1 | public function getEntitlements() { |
|
133 | |||
134 | /** |
||
135 | * Attempts to decrypt the given response. |
||
136 | * |
||
137 | * @param string|array|null $response |
||
138 | * @return array|null|string |
||
139 | * @throws EncryptionConfigurationException |
||
140 | */ |
||
141 | 6 | public function decryptResponse($response) |
|
166 | |||
167 | /** |
||
168 | * Get authorization url to begin OAuth flow |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 6 | public function getBaseAuthorizationUrl() |
|
176 | |||
177 | /** |
||
178 | * Get access token url to retrieve token |
||
179 | * |
||
180 | * @param array $params |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | 12 | public function getBaseAccessTokenUrl(array $params) |
|
188 | |||
189 | /** |
||
190 | * Get provider url to fetch user details |
||
191 | * |
||
192 | * @param AccessToken $token |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | 6 | public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
200 | |||
201 | /** |
||
202 | * Keycloak extension supporting entitlements. |
||
203 | * |
||
204 | * @param AccessToken $token |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getEntitlementsUrl(AccessToken $token) { |
||
210 | |||
211 | /** |
||
212 | * Creates base url from provider configuration. |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | 18 | protected function getBaseUrlWithRealm() |
|
220 | |||
221 | /** |
||
222 | * Get the default scopes used by this provider. |
||
223 | * |
||
224 | * This should not be a complete list of all scopes, but the minimum |
||
225 | * required for the provider user interface! |
||
226 | * |
||
227 | * @return string[] |
||
228 | */ |
||
229 | 4 | protected function getDefaultScopes() |
|
233 | |||
234 | 6 | protected function getAuthorizationHeaders($token = null) |
|
242 | |||
243 | /** |
||
244 | * Check a provider response for errors. |
||
245 | * |
||
246 | * @throws IdentityProviderException |
||
247 | * @param ResponseInterface $response |
||
248 | * @param string $data Parsed response data |
||
249 | * @return void |
||
250 | */ |
||
251 | 10 | protected function checkResponse(ResponseInterface $response, $data) |
|
258 | |||
259 | /** |
||
260 | * Generate a user object from a successful user details request. |
||
261 | * |
||
262 | * @param array $response |
||
263 | * @param AccessToken $token |
||
264 | * @return KeycloakResourceOwner |
||
265 | */ |
||
266 | 4 | protected function createResourceOwner(array $response, AccessToken $token) |
|
270 | |||
271 | /** |
||
272 | * Requests and returns the resource owner of given access token. |
||
273 | * |
||
274 | * @param AccessToken $token |
||
275 | * @return KeycloakResourceOwner |
||
276 | */ |
||
277 | 6 | public function getResourceOwner(AccessToken $token) |
|
285 | |||
286 | /** |
||
287 | * Updates expected encryption algorithm of Keycloak instance. |
||
288 | * |
||
289 | * @param string $encryptionAlgorithm |
||
290 | * |
||
291 | * @return Keycloak |
||
292 | */ |
||
293 | 4 | public function setEncryptionAlgorithm($encryptionAlgorithm) |
|
299 | |||
300 | /** |
||
301 | * Updates expected encryption key of Keycloak instance. |
||
302 | * |
||
303 | * @param string $encryptionKey |
||
304 | * |
||
305 | * @return Keycloak |
||
306 | */ |
||
307 | 4 | public function setEncryptionKey($encryptionKey) |
|
313 | |||
314 | /** |
||
315 | * Updates expected encryption key of Keycloak instance to content of given |
||
316 | * file path. |
||
317 | * |
||
318 | * @param string $encryptionKeyPath |
||
319 | * |
||
320 | * @return Keycloak |
||
321 | */ |
||
322 | 2 | public function setEncryptionKeyPath($encryptionKeyPath) |
|
332 | } |
||
333 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.