1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stevenmaguire\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Firebase\JWT\JWT; |
7
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
8
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
9
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
10
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Stevenmaguire\OAuth2\Client\Provider\Exception\EncryptionConfigurationException; |
13
|
|
|
|
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
|
|
|
} |
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
|
|
|
) |
91
|
|
|
), |
92
|
2 |
|
true |
93
|
|
|
); |
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() |
105
|
|
|
{ |
106
|
6 |
|
return $this->getBaseUrlWithRealm().'/protocol/openid-connect/auth'; |
107
|
|
|
} |
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) |
117
|
|
|
{ |
118
|
12 |
|
return $this->getBaseUrlWithRealm().'/protocol/openid-connect/token'; |
119
|
|
|
} |
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) |
129
|
|
|
{ |
130
|
6 |
|
return $this->getBaseUrlWithRealm().'/protocol/openid-connect/userinfo'; |
131
|
|
|
} |
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 = []) |
140
|
|
|
{ |
141
|
2 |
|
$base = $this->getBaseLogoutUrl(); |
142
|
2 |
|
$params = $this->getAuthorizationParameters($options); |
143
|
2 |
|
$query = $this->getAuthorizationQuery($params); |
144
|
2 |
|
return $this->appendQuery($base, $query); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get logout url to logout of session token |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
2 |
|
private function getBaseLogoutUrl() |
153
|
|
|
{ |
154
|
2 |
|
return $this->getBaseUrlWithRealm() . '/protocol/openid-connect/logout'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Creates base url from provider configuration. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
20 |
|
protected function getBaseUrlWithRealm() |
163
|
|
|
{ |
164
|
20 |
|
return $this->authServerUrl.'/realms/'.$this->realm; |
165
|
|
|
} |
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() |
176
|
|
|
{ |
177
|
6 |
|
return ['name', 'email']; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Check a provider response for errors. |
182
|
|
|
* |
183
|
|
|
* @throws IdentityProviderException |
184
|
|
|
* @param ResponseInterface $response |
185
|
|
|
* @param string $data Parsed response data |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
10 |
|
protected function checkResponse(ResponseInterface $response, $data) |
189
|
|
|
{ |
190
|
10 |
|
if (!empty($data['error'])) { |
191
|
2 |
|
$error = $data['error'].': '.$data['error_description']; |
192
|
2 |
|
throw new IdentityProviderException($error, 0, $data); |
193
|
|
|
} |
194
|
8 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Generate a user object from a successful user details request. |
198
|
|
|
* |
199
|
|
|
* @param array $response |
200
|
|
|
* @param AccessToken $token |
201
|
|
|
* @return KeycloakResourceOwner |
202
|
|
|
*/ |
203
|
4 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
204
|
|
|
{ |
205
|
4 |
|
return new KeycloakResourceOwner($response); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Requests and returns the resource owner of given access token. |
210
|
|
|
* |
211
|
|
|
* @param AccessToken $token |
212
|
|
|
* @return KeycloakResourceOwner |
213
|
|
|
*/ |
214
|
6 |
|
public function getResourceOwner(AccessToken $token) |
215
|
|
|
{ |
216
|
6 |
|
$response = $this->fetchResourceOwnerDetails($token); |
217
|
|
|
|
218
|
6 |
|
$response = $this->decryptResponse($response); |
219
|
|
|
|
220
|
4 |
|
return $this->createResourceOwner($response, $token); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Updates expected encryption algorithm of Keycloak instance. |
225
|
|
|
* |
226
|
|
|
* @param string $encryptionAlgorithm |
227
|
|
|
* |
228
|
|
|
* @return Keycloak |
229
|
|
|
*/ |
230
|
4 |
|
public function setEncryptionAlgorithm($encryptionAlgorithm) |
231
|
|
|
{ |
232
|
4 |
|
$this->encryptionAlgorithm = $encryptionAlgorithm; |
233
|
|
|
|
234
|
4 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Updates expected encryption key of Keycloak instance. |
239
|
|
|
* |
240
|
|
|
* @param string $encryptionKey |
241
|
|
|
* |
242
|
|
|
* @return Keycloak |
243
|
|
|
*/ |
244
|
4 |
|
public function setEncryptionKey($encryptionKey) |
245
|
|
|
{ |
246
|
4 |
|
$this->encryptionKey = $encryptionKey; |
247
|
|
|
|
248
|
4 |
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Updates expected encryption key of Keycloak instance to content of given |
253
|
|
|
* file path. |
254
|
|
|
* |
255
|
|
|
* @param string $encryptionKeyPath |
256
|
|
|
* |
257
|
|
|
* @return Keycloak |
258
|
|
|
*/ |
259
|
4 |
|
public function setEncryptionKeyPath($encryptionKeyPath) |
260
|
|
|
{ |
261
|
|
|
try { |
262
|
4 |
|
$this->encryptionKey = file_get_contents($encryptionKeyPath); |
263
|
2 |
|
} catch (Exception $e) { |
264
|
|
|
// Not sure how to handle this yet. |
265
|
|
|
} |
266
|
|
|
|
267
|
4 |
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Checks if provider is configured to use encryption. |
272
|
|
|
* |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
4 |
|
public function usesEncryption() |
276
|
|
|
{ |
277
|
4 |
|
return (bool) $this->encryptionAlgorithm && $this->encryptionKey; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
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.