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 | */ |
|
62 | public function __construct(array $options = [], array $collaborators = []) |
||
63 | 28 | { |
|
64 | 4 | if (isset($options['encryptionKeyPath'])) { |
|
65 | 4 | $this->setEncryptionKeyPath($options['encryptionKeyPath']); |
|
66 | 2 | unset($options['encryptionKeyPath']); |
|
67 | 28 | } |
|
68 | 28 | parent::__construct($options, $collaborators); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Attempts to decrypt the given response. |
||
73 | * |
||
74 | * @param string|array|null $response |
||
75 | * |
||
76 | * @return string|array|null |
||
77 | 6 | */ |
|
78 | public function decryptResponse($response) |
||
79 | 6 | { |
|
80 | 2 | if (!is_string($response)) { |
|
81 | return $response; |
||
82 | } |
||
83 | 4 | ||
84 | 2 | if ($this->usesEncryption()) { |
|
85 | 2 | return json_decode( |
|
86 | 2 | json_encode( |
|
87 | 2 | JWT::decode( |
|
88 | 2 | $response, |
|
89 | 2 | $this->encryptionKey, |
|
90 | 1 | array($this->encryptionAlgorithm) |
|
91 | 1 | ) |
|
92 | 1 | ), |
|
93 | 1 | true |
|
94 | ); |
||
95 | } |
||
96 | 2 | ||
97 | throw EncryptionConfigurationException::undeterminedEncryption(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get authorization url to begin OAuth flow |
||
102 | * |
||
103 | * @return string |
||
104 | 6 | */ |
|
105 | public function getBaseAuthorizationUrl() |
||
109 | |||
110 | /** |
||
111 | * Get access token url to retrieve token |
||
112 | * |
||
113 | * @param array $params |
||
114 | * |
||
115 | * @return string |
||
116 | 12 | */ |
|
117 | public function getBaseAccessTokenUrl(array $params) |
||
121 | |||
122 | /** |
||
123 | * Get provider url to fetch user details |
||
124 | * |
||
125 | * @param AccessToken $token |
||
126 | * |
||
127 | * @return string |
||
128 | 6 | */ |
|
129 | public function getResourceOwnerDetailsUrl(AccessToken $token) |
||
133 | |||
134 | /** |
||
135 | * Builds the logout URL. |
||
136 | * |
||
137 | * @param array $options |
||
138 | * @return string Authorization URL |
||
139 | 2 | */ |
|
140 | public function getLogoutUrl(array $options = []) |
||
147 | |||
148 | /** |
||
149 | * Get logout url to logout of session token |
||
150 | * |
||
151 | * @return string |
||
152 | 2 | */ |
|
153 | private function getBaseLogoutUrl() |
||
157 | |||
158 | /** |
||
159 | * Creates base url from provider configuration. |
||
160 | * |
||
161 | * @return string |
||
162 | 20 | */ |
|
163 | protected function getBaseUrlWithRealm() |
||
167 | |||
168 | /** |
||
169 | * Get the default scopes used by this provider. |
||
170 | * |
||
171 | * This should not be a complete list of all scopes, but the minimum |
||
172 | * required for the provider user interface! |
||
173 | * |
||
174 | * @return string[] |
||
175 | 6 | */ |
|
176 | protected function getDefaultScopes() |
||
180 | |||
181 | /** |
||
182 | * Returns the string that should be used to separate scopes when building |
||
183 | * the URL for requesting an access token. |
||
184 | * |
||
185 | * @return string Scope separator, defaults to ',' |
||
186 | 8 | */ |
|
187 | protected function getScopeSeparator() |
||
191 | |||
192 | |||
193 | /** |
||
194 | * Check a provider response for errors. |
||
195 | * |
||
196 | * @throws IdentityProviderException |
||
197 | * @param ResponseInterface $response |
||
198 | * @param string $data Parsed response data |
||
199 | * @return void |
||
200 | 10 | */ |
|
201 | protected function checkResponse(ResponseInterface $response, $data) |
||
208 | |||
209 | /** |
||
210 | * Generate a user object from a successful user details request. |
||
211 | * |
||
212 | * @param array $response |
||
213 | * @param AccessToken $token |
||
214 | * @return KeycloakResourceOwner |
||
215 | 4 | */ |
|
216 | protected function createResourceOwner(array $response, AccessToken $token) |
||
220 | |||
221 | /** |
||
222 | * Requests and returns the resource owner of given access token. |
||
223 | * |
||
224 | * @param AccessToken $token |
||
225 | * @return KeycloakResourceOwner |
||
226 | 6 | * @throws EncryptionConfigurationException |
|
227 | */ |
||
228 | 6 | public function getResourceOwner(AccessToken $token) |
|
242 | 4 | ||
243 | /** |
||
244 | 4 | * Updates expected encryption algorithm of Keycloak instance. |
|
245 | * |
||
246 | 4 | * @param string $encryptionAlgorithm |
|
247 | * |
||
248 | * @return Keycloak |
||
249 | */ |
||
250 | public function setEncryptionAlgorithm($encryptionAlgorithm) |
||
256 | 4 | ||
257 | /** |
||
258 | 4 | * Updates expected encryption key of Keycloak instance. |
|
259 | * |
||
260 | 4 | * @param string $encryptionKey |
|
261 | * |
||
262 | * @return Keycloak |
||
263 | */ |
||
264 | public function setEncryptionKey($encryptionKey) |
||
270 | |||
271 | 4 | /** |
|
272 | * Updates expected encryption key of Keycloak instance to content of given |
||
273 | * file path. |
||
274 | 4 | * |
|
275 | 3 | * @param string $encryptionKeyPath |
|
276 | * |
||
277 | * @return Keycloak |
||
278 | */ |
||
279 | 4 | public function setEncryptionKeyPath($encryptionKeyPath) |
|
289 | 4 | ||
290 | /** |
||
291 | * Checks if provider is configured to use encryption. |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | public function usesEncryption() |
||
299 | |||
300 | /** |
||
301 | * Parses the response according to its content-type header. |
||
302 | * |
||
303 | * @throws UnexpectedValueException |
||
304 | * @param ResponseInterface $response |
||
305 | * @return array |
||
306 | */ |
||
307 | protected function parseResponse(ResponseInterface $response) |
||
325 | } |
||
326 |
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.