1 | <?php |
||
15 | class Keycloak extends AbstractProvider |
||
16 | { |
||
17 | use BearerAuthorizationTrait; |
||
18 | |||
19 | /** |
||
20 | * Keycloak URL, eg. http://localhost:8080/auth. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | public $authServerUrl = null; |
||
25 | |||
26 | /** |
||
27 | * Realm name, eg. demo. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | public $realm = null; |
||
32 | |||
33 | /** |
||
34 | * Encryption algorithm. |
||
35 | * |
||
36 | * You must specify supported algorithms for your application. See |
||
37 | * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40 |
||
38 | * for a list of spec-compliant algorithms. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | public $encryptionAlgorithm = null; |
||
43 | |||
44 | /** |
||
45 | * Encryption key. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | public $encryptionKey = null; |
||
50 | |||
51 | /** |
||
52 | * Constructs an OAuth 2.0 service provider. |
||
53 | * |
||
54 | * @param array $options An array of options to set on this provider. |
||
55 | * Options include `clientId`, `clientSecret`, `redirectUri`, and `state`. |
||
56 | * Individual providers may introduce more options, as needed. |
||
57 | * @param array $collaborators An array of collaborators that may be used to |
||
58 | * override this provider's default behavior. Collaborators include |
||
59 | * `grantFactory`, `requestFactory`, `httpClient`, and `randomFactory`. |
||
60 | * Individual providers may introduce more collaborators, as needed. |
||
61 | 28 | * @throws EncryptionKeyPathNotFoundException |
|
62 | */ |
||
63 | 28 | public function __construct(array $options = [], array $collaborators = []) |
|
71 | |||
72 | /** |
||
73 | * Attempts to decrypt the given response. |
||
74 | * |
||
75 | * @param string|array|null $response |
||
76 | * |
||
77 | 6 | * @return string|array|null |
|
78 | * @throws EncryptionConfigurationException |
||
79 | 6 | */ |
|
80 | 2 | public function decryptResponse($response) |
|
101 | |||
102 | /** |
||
103 | * Get authorization url to begin OAuth flow |
||
104 | 6 | * |
|
105 | * @return string |
||
106 | 6 | */ |
|
107 | public function getBaseAuthorizationUrl() |
||
111 | |||
112 | /** |
||
113 | * Get access token url to retrieve token |
||
114 | * |
||
115 | * @param array $params |
||
116 | 12 | * |
|
117 | * @return string |
||
118 | 12 | */ |
|
119 | public function getBaseAccessTokenUrl(array $params) |
||
123 | |||
124 | /** |
||
125 | * Get provider url to fetch user details |
||
126 | * |
||
127 | * @param AccessToken $token |
||
128 | 6 | * |
|
129 | * @return string |
||
130 | 6 | */ |
|
131 | public function getResourceOwnerDetailsUrl(AccessToken $token) |
||
135 | |||
136 | /** |
||
137 | * Builds the logout URL. |
||
138 | * |
||
139 | 2 | * @param array $options |
|
140 | * @return string Authorization URL |
||
141 | 2 | */ |
|
142 | 2 | public function getLogoutUrl(array $options = []) |
|
149 | |||
150 | /** |
||
151 | * Get logout url to logout of session token |
||
152 | 2 | * |
|
153 | * @return string |
||
154 | 2 | */ |
|
155 | private function getBaseLogoutUrl() |
||
159 | |||
160 | /** |
||
161 | * Creates base url from provider configuration. |
||
162 | 20 | * |
|
163 | * @return string |
||
164 | 20 | */ |
|
165 | protected function getBaseUrlWithRealm() |
||
169 | |||
170 | /** |
||
171 | * Get the default scopes used by this provider. |
||
172 | * |
||
173 | * This should not be a complete list of all scopes, but the minimum |
||
174 | * required for the provider user interface! |
||
175 | 6 | * |
|
176 | * @return string[] |
||
177 | 6 | */ |
|
178 | protected function getDefaultScopes() |
||
182 | |||
183 | /** |
||
184 | * Check a provider response for errors. |
||
185 | * |
||
186 | * @throws IdentityProviderException |
||
187 | * @param ResponseInterface $response |
||
188 | 10 | * @param string $data Parsed response data |
|
189 | * @return void |
||
190 | 10 | */ |
|
191 | 2 | protected function checkResponse(ResponseInterface $response, $data) |
|
198 | |||
199 | /** |
||
200 | * Generate a user object from a successful user details request. |
||
201 | * |
||
202 | * @param array $response |
||
203 | 4 | * @param AccessToken $token |
|
204 | * @return KeycloakResourceOwner |
||
205 | 4 | */ |
|
206 | protected function createResourceOwner(array $response, AccessToken $token) |
||
210 | |||
211 | /** |
||
212 | * Requests and returns the resource owner of given access token. |
||
213 | * |
||
214 | 6 | * @param AccessToken $token |
|
215 | * @return KeycloakResourceOwner |
||
216 | 6 | * @throws EncryptionConfigurationException |
|
217 | */ |
||
218 | 6 | public function getResourceOwner(AccessToken $token) |
|
232 | 4 | ||
233 | /** |
||
234 | 4 | * Updates expected encryption algorithm of Keycloak instance. |
|
235 | * |
||
236 | * @param string $encryptionAlgorithm |
||
237 | * |
||
238 | * @return Keycloak |
||
239 | */ |
||
240 | public function setEncryptionAlgorithm($encryptionAlgorithm) |
||
246 | 4 | ||
247 | /** |
||
248 | 4 | * Updates expected encryption key of Keycloak instance. |
|
249 | * |
||
250 | * @param string $encryptionKey |
||
251 | * |
||
252 | * @return Keycloak |
||
253 | */ |
||
254 | public function setEncryptionKey($encryptionKey) |
||
260 | |||
261 | /** |
||
262 | 4 | * Updates expected encryption key of Keycloak instance to content of given |
|
263 | 2 | * file path. |
|
264 | * |
||
265 | * @param string $encryptionKeyPath |
||
266 | * |
||
267 | 4 | * @return Keycloak |
|
268 | * @throws EncryptionKeyPathNotFoundException |
||
269 | */ |
||
270 | public function setEncryptionKeyPath($encryptionKeyPath) |
||
281 | |||
282 | /** |
||
283 | * Checks if provider is configured to use encryption. |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function usesEncryption() |
||
291 | |||
292 | /** |
||
293 | * Parses the response according to its content-type header. |
||
294 | * |
||
295 | * @throws UnexpectedValueException |
||
296 | * @param ResponseInterface $response |
||
297 | * @return array |
||
298 | */ |
||
299 | protected function parseResponse(ResponseInterface $response) |
||
317 | } |
||
318 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.