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 | /** |
||
51 | * Constructs an OAuth 2.0 service provider. |
||
52 | * |
||
53 | * @param array $options An array of options to set on this provider. |
||
54 | * Options include `clientId`, `clientSecret`, `redirectUri`, and `state`. |
||
55 | * Individual providers may introduce more options, as needed. |
||
56 | * @param array $collaborators An array of collaborators that may be used to |
||
57 | * override this provider's default behavior. Collaborators include |
||
58 | * `grantFactory`, `requestFactory`, `httpClient`, and `randomFactory`. |
||
59 | * Individual providers may introduce more collaborators, as needed. |
||
60 | */ |
||
61 | 28 | public function __construct(array $options = [], array $collaborators = []) |
|
62 | { |
||
63 | 28 | if (isset($options['encryptionKeyPath'])) { |
|
64 | 4 | $this->setEncryptionKeyPath($options['encryptionKeyPath']); |
|
65 | 4 | unset($options['encryptionKeyPath']); |
|
66 | 2 | } |
|
67 | 28 | parent::__construct($options, $collaborators); |
|
68 | 28 | } |
|
69 | |||
70 | /** |
||
71 | * Attempts to decrypt the given response. |
||
72 | * |
||
73 | * @param string|array|null $response |
||
74 | * |
||
75 | * @return string|array|null |
||
76 | */ |
||
77 | 6 | public function decryptResponse($response) |
|
78 | { |
||
79 | 6 | if (!is_string($response)) { |
|
80 | 2 | return $response; |
|
81 | } |
||
82 | |||
83 | 4 | if ($this->usesEncryption()) { |
|
84 | 2 | return json_decode( |
|
85 | 2 | json_encode( |
|
86 | 2 | JWT::decode( |
|
87 | 2 | $response, |
|
88 | 2 | $this->encryptionKey, |
|
89 | 2 | array($this->encryptionAlgorithm) |
|
90 | 1 | ) |
|
91 | 1 | ), |
|
92 | 1 | true |
|
93 | 1 | ); |
|
94 | } |
||
95 | |||
96 | 2 | throw EncryptionConfigurationException::undeterminedEncryption(); |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Get authorization url to begin OAuth flow |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | 6 | public function getBaseAuthorizationUrl() |
|
108 | |||
109 | /** |
||
110 | * Get access token url to retrieve token |
||
111 | * |
||
112 | * @param array $params |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 12 | public function getBaseAccessTokenUrl(array $params) |
|
120 | |||
121 | /** |
||
122 | * Get provider url to fetch user details |
||
123 | * |
||
124 | * @param AccessToken $token |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 6 | public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
132 | |||
133 | /** |
||
134 | * Builds the logout URL. |
||
135 | * |
||
136 | * @param array $options |
||
137 | * @return string Authorization URL |
||
138 | */ |
||
139 | 2 | public function getLogoutUrl(array $options = []) |
|
146 | |||
147 | /** |
||
148 | * Get logout url to logout of session token |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 2 | private function getBaseLogoutUrl() |
|
156 | |||
157 | /** |
||
158 | * Creates base url from provider configuration. |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 20 | protected function getBaseUrlWithRealm() |
|
166 | |||
167 | /** |
||
168 | * Get the default scopes used by this provider. |
||
169 | * |
||
170 | * This should not be a complete list of all scopes, but the minimum |
||
171 | * required for the provider user interface! |
||
172 | * |
||
173 | * @return string[] |
||
174 | */ |
||
175 | 6 | protected function getDefaultScopes() |
|
179 | |||
180 | /** |
||
181 | * Returns the string that should be used to separate scopes when building |
||
182 | * the URL for requesting an access token. |
||
183 | * |
||
184 | * @return string Scope separator, defaults to ',' |
||
185 | */ |
||
186 | 8 | protected function getScopeSeparator() |
|
190 | |||
191 | /** |
||
192 | * Check a provider response for errors. |
||
193 | * |
||
194 | * @throws IdentityProviderException |
||
195 | * @param ResponseInterface $response |
||
196 | * @param string $data Parsed response data |
||
197 | * @return void |
||
198 | */ |
||
199 | 10 | protected function checkResponse(ResponseInterface $response, $data) |
|
206 | |||
207 | /** |
||
208 | * Generate a user object from a successful user details request. |
||
209 | * |
||
210 | * @param array $response |
||
211 | * @param AccessToken $token |
||
212 | * @return KeycloakResourceOwner |
||
213 | */ |
||
214 | 4 | protected function createResourceOwner(array $response, AccessToken $token) |
|
218 | |||
219 | /** |
||
220 | * Requests and returns the resource owner of given access token. |
||
221 | * |
||
222 | * @param AccessToken $token |
||
223 | * @return KeycloakResourceOwner |
||
224 | */ |
||
225 | 6 | public function getResourceOwner(AccessToken $token) |
|
233 | |||
234 | /** |
||
235 | * Updates expected encryption algorithm of Keycloak instance. |
||
236 | * |
||
237 | * @param string $encryptionAlgorithm |
||
238 | * |
||
239 | * @return Keycloak |
||
240 | */ |
||
241 | 4 | public function setEncryptionAlgorithm($encryptionAlgorithm) |
|
247 | |||
248 | /** |
||
249 | * Updates expected encryption key of Keycloak instance. |
||
250 | * |
||
251 | * @param string $encryptionKey |
||
252 | * |
||
253 | * @return Keycloak |
||
254 | */ |
||
255 | 4 | public function setEncryptionKey($encryptionKey) |
|
261 | |||
262 | /** |
||
263 | * Updates expected encryption key of Keycloak instance to content of given |
||
264 | * file path. |
||
265 | * |
||
266 | * @param string $encryptionKeyPath |
||
267 | * |
||
268 | * @return Keycloak |
||
269 | */ |
||
270 | 4 | public function setEncryptionKeyPath($encryptionKeyPath) |
|
280 | |||
281 | /** |
||
282 | * Checks if provider is configured to use encryption. |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | 4 | public function usesEncryption() |
|
290 | } |
||
291 |
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.