|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* OAuth 2.0 Abstract grant. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alex Bilbie <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) Alex Bilbie |
|
7
|
|
|
* @license http://mit-license.org/ |
|
8
|
|
|
* |
|
9
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace League\OAuth2\Server\Grant; |
|
12
|
|
|
|
|
13
|
|
|
use DateInterval; |
|
14
|
|
|
use DateTimeImmutable; |
|
15
|
|
|
use Error; |
|
16
|
|
|
use Exception; |
|
17
|
|
|
use League\Event\EmitterAwareTrait; |
|
18
|
|
|
use League\OAuth2\Server\CryptKey; |
|
19
|
|
|
use League\OAuth2\Server\CryptTrait; |
|
20
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
|
21
|
|
|
use League\OAuth2\Server\Entities\AuthCodeEntityInterface; |
|
22
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
23
|
|
|
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; |
|
24
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
|
25
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
|
26
|
|
|
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException; |
|
27
|
|
|
use League\OAuth2\Server\RedirectUriValidators\RedirectUriValidator; |
|
28
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
|
29
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; |
|
30
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; |
|
31
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; |
|
32
|
|
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; |
|
33
|
|
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface; |
|
34
|
|
|
use League\OAuth2\Server\RequestEvent; |
|
35
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest; |
|
36
|
|
|
use LogicException; |
|
37
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
38
|
|
|
use TypeError; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Abstract grant class. |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract class AbstractGrant implements GrantTypeInterface |
|
44
|
|
|
{ |
|
45
|
|
|
use EmitterAwareTrait, CryptTrait; |
|
46
|
|
|
|
|
47
|
|
|
const SCOPE_DELIMITER_STRING = ' '; |
|
48
|
|
|
|
|
49
|
|
|
const MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS = 10; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var ClientRepositoryInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $clientRepository; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var AccessTokenRepositoryInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $accessTokenRepository; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var ScopeRepositoryInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $scopeRepository; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var AuthCodeRepositoryInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $authCodeRepository; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var RefreshTokenRepositoryInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $refreshTokenRepository; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var UserRepositoryInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $userRepository; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var DateInterval |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $refreshTokenTTL; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var CryptKey |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $privateKey; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var string |
|
93
|
|
|
*/ |
|
94
|
|
|
protected $defaultScope; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @var bool |
|
98
|
|
|
*/ |
|
99
|
81 |
|
protected $revokeRefreshTokens; |
|
100
|
|
|
|
|
101
|
81 |
|
/** |
|
102
|
81 |
|
* @param ClientRepositoryInterface $clientRepository |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setClientRepository(ClientRepositoryInterface $clientRepository) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->clientRepository = $clientRepository; |
|
107
|
46 |
|
} |
|
108
|
|
|
|
|
109
|
46 |
|
/** |
|
110
|
46 |
|
* @param AccessTokenRepositoryInterface $accessTokenRepository |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->accessTokenRepository = $accessTokenRepository; |
|
115
|
45 |
|
} |
|
116
|
|
|
|
|
117
|
45 |
|
/** |
|
118
|
45 |
|
* @param ScopeRepositoryInterface $scopeRepository |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->scopeRepository = $scopeRepository; |
|
123
|
67 |
|
} |
|
124
|
|
|
|
|
125
|
67 |
|
/** |
|
126
|
67 |
|
* @param RefreshTokenRepositoryInterface $refreshTokenRepository |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->refreshTokenRepository = $refreshTokenRepository; |
|
131
|
50 |
|
} |
|
132
|
|
|
|
|
133
|
50 |
|
/** |
|
134
|
50 |
|
* @param AuthCodeRepositoryInterface $authCodeRepository |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->authCodeRepository = $authCodeRepository; |
|
139
|
6 |
|
} |
|
140
|
|
|
|
|
141
|
6 |
|
/** |
|
142
|
6 |
|
* @param UserRepositoryInterface $userRepository |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setUserRepository(UserRepositoryInterface $userRepository) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->userRepository = $userRepository; |
|
147
|
2 |
|
} |
|
148
|
|
|
|
|
149
|
2 |
|
/** |
|
150
|
2 |
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setRefreshTokenTTL(DateInterval $refreshTokenTTL) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->refreshTokenTTL = $refreshTokenTTL; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
35 |
|
/** |
|
158
|
|
|
* Set the private key |
|
159
|
35 |
|
* |
|
160
|
35 |
|
* @param CryptKey $key |
|
161
|
|
|
*/ |
|
162
|
|
|
public function setPrivateKey(CryptKey $key) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->privateKey = $key; |
|
165
|
20 |
|
} |
|
166
|
|
|
|
|
167
|
20 |
|
/** |
|
168
|
20 |
|
* @param string $scope |
|
169
|
|
|
*/ |
|
170
|
|
|
public function setDefaultScope($scope) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->defaultScope = $scope; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param bool $revokeRefreshTokens |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setRevokeRefreshTokens(bool $revokeRefreshTokens) |
|
179
|
44 |
|
{ |
|
180
|
|
|
$this->revokeRefreshTokens = $revokeRefreshTokens; |
|
181
|
44 |
|
} |
|
182
|
|
|
|
|
183
|
42 |
|
/** |
|
184
|
7 |
|
* Validate the client. |
|
185
|
|
|
* |
|
186
|
7 |
|
* @param ServerRequestInterface $request |
|
187
|
|
|
* |
|
188
|
|
|
* @throws OAuthServerException |
|
189
|
35 |
|
* |
|
190
|
|
|
* @return ClientEntityInterface |
|
191
|
|
|
*/ |
|
192
|
35 |
|
protected function validateClient(ServerRequestInterface $request) |
|
193
|
|
|
{ |
|
194
|
35 |
|
[$clientId, $clientSecret] = $this->getClientCredentials($request); |
|
195
|
17 |
|
|
|
196
|
1 |
|
if ($this->clientRepository->validateClient($clientId, $clientSecret, $this->getIdentifier()) === false) { |
|
197
|
|
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
198
|
|
|
|
|
199
|
16 |
|
throw OAuthServerException::invalidClient($request); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
32 |
|
$client = $this->getClientEntityOrFail($clientId, $request); |
|
203
|
|
|
|
|
204
|
|
|
// If a redirect URI is provided ensure it matches what is pre-registered |
|
205
|
|
|
$redirectUri = $this->getRequestParameter('redirect_uri', $request, null); |
|
206
|
|
|
|
|
207
|
|
|
if ($redirectUri !== null) { |
|
208
|
|
|
if (!\is_string($redirectUri)) { |
|
|
|
|
|
|
209
|
|
|
throw OAuthServerException::invalidRequest('redirect_uri'); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$this->validateRedirectUri($redirectUri, $client, $request); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return $client; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Wrapper around ClientRepository::getClientEntity() that ensures we emit |
|
220
|
66 |
|
* an event and throw an exception if the repo doesn't return a client |
|
221
|
|
|
* entity. |
|
222
|
66 |
|
* |
|
223
|
|
|
* This is a bit of defensive coding because the interface contract |
|
224
|
66 |
|
* doesn't actually enforce non-null returns/exception-on-no-client so |
|
225
|
4 |
|
* getClientEntity might return null. By contrast, this method will |
|
226
|
4 |
|
* always either return a ClientEntityInterface or throw. |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $clientId |
|
229
|
62 |
|
* @param ServerRequestInterface $request |
|
230
|
|
|
* |
|
231
|
|
|
* @return ClientEntityInterface |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function getClientEntityOrFail($clientId, ServerRequestInterface $request) |
|
234
|
|
|
{ |
|
235
|
|
|
$client = $this->clientRepository->getClientEntity($clientId); |
|
236
|
|
|
|
|
237
|
|
|
if ($client instanceof ClientEntityInterface === false) { |
|
238
|
|
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
239
|
|
|
throw OAuthServerException::invalidClient($request); |
|
240
|
56 |
|
} |
|
241
|
|
|
|
|
242
|
56 |
|
return $client; |
|
243
|
|
|
} |
|
244
|
56 |
|
|
|
245
|
|
|
/** |
|
246
|
56 |
|
* Gets the client credentials from the request from the request body or |
|
247
|
3 |
|
* the Http Basic Authorization header |
|
248
|
|
|
* |
|
249
|
|
|
* @param ServerRequestInterface $request |
|
250
|
53 |
|
* |
|
251
|
|
|
* @return array |
|
252
|
53 |
|
*/ |
|
253
|
1 |
|
protected function getClientCredentials(ServerRequestInterface $request) |
|
254
|
|
|
{ |
|
255
|
|
|
[$basicAuthUser, $basicAuthPassword] = $this->getBasicAuthCredentials($request); |
|
256
|
52 |
|
|
|
257
|
|
|
$clientId = $this->getRequestParameter('client_id', $request, $basicAuthUser); |
|
258
|
|
|
|
|
259
|
|
|
if (!\is_string($clientId)) { |
|
260
|
|
|
throw OAuthServerException::invalidRequest('client_id'); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$clientSecret = $this->getRequestParameter('client_secret', $request, $basicAuthPassword); |
|
264
|
|
|
|
|
265
|
|
|
if ($clientSecret !== null && !\is_string($clientSecret)) { |
|
|
|
|
|
|
266
|
|
|
throw OAuthServerException::invalidRequest('client_secret'); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
31 |
|
return [$clientId, $clientSecret]; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Validate redirectUri from the request. |
|
274
|
31 |
|
* If a redirect URI is provided ensure it matches what is pre-registered |
|
275
|
31 |
|
* |
|
276
|
6 |
|
* @param string $redirectUri |
|
277
|
6 |
|
* @param ClientEntityInterface $client |
|
278
|
|
|
* @param ServerRequestInterface $request |
|
279
|
25 |
|
* |
|
280
|
|
|
* @throws OAuthServerException |
|
281
|
|
|
*/ |
|
282
|
|
|
protected function validateRedirectUri( |
|
283
|
|
|
string $redirectUri, |
|
284
|
|
|
ClientEntityInterface $client, |
|
285
|
|
|
ServerRequestInterface $request |
|
286
|
|
|
) { |
|
287
|
|
|
$validator = new RedirectUriValidator($client->getRedirectUri()); |
|
288
|
|
|
if (!$validator->validateRedirectUri($redirectUri)) { |
|
289
|
|
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
290
|
|
|
throw OAuthServerException::invalidClient($request); |
|
291
|
40 |
|
} |
|
292
|
|
|
} |
|
293
|
40 |
|
|
|
294
|
5 |
|
/** |
|
295
|
35 |
|
* Validate scopes in the request. |
|
296
|
21 |
|
* |
|
297
|
|
|
* @param string|array $scopes |
|
298
|
|
|
* @param string $redirectUri |
|
299
|
40 |
|
* |
|
300
|
|
|
* @throws OAuthServerException |
|
301
|
|
|
* |
|
302
|
|
|
* @return ScopeEntityInterface[] |
|
303
|
40 |
|
*/ |
|
304
|
|
|
public function validateScopes($scopes, $redirectUri = null) |
|
305
|
40 |
|
{ |
|
306
|
35 |
|
if ($scopes === null) { |
|
|
|
|
|
|
307
|
|
|
$scopes = []; |
|
308
|
35 |
|
} elseif (\is_string($scopes)) { |
|
309
|
2 |
|
$scopes = $this->convertScopesQueryStringToArray($scopes); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
33 |
|
if (!\is_array($scopes)) { |
|
|
|
|
|
|
313
|
|
|
throw OAuthServerException::invalidRequest('scope'); |
|
314
|
|
|
} |
|
315
|
38 |
|
|
|
316
|
|
|
$validScopes = []; |
|
317
|
|
|
|
|
318
|
|
|
foreach ($scopes as $scopeItem) { |
|
319
|
|
|
$scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeItem); |
|
320
|
|
|
|
|
321
|
|
|
if ($scope instanceof ScopeEntityInterface === false) { |
|
322
|
|
|
throw OAuthServerException::invalidScope($scopeItem, $redirectUri); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
21 |
|
$validScopes[] = $scope; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
21 |
|
return $validScopes; |
|
329
|
21 |
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Converts a scopes query string to an array to easily iterate for validation. |
|
333
|
|
|
* |
|
334
|
|
|
* @param string $scopes |
|
335
|
|
|
* |
|
336
|
|
|
* @return array |
|
337
|
|
|
*/ |
|
338
|
|
|
private function convertScopesQueryStringToArray(string $scopes) |
|
339
|
|
|
{ |
|
340
|
|
|
return \array_filter(\explode(self::SCOPE_DELIMITER_STRING, \trim($scopes)), function ($scope) { |
|
341
|
56 |
|
return $scope !== ''; |
|
342
|
|
|
}); |
|
343
|
56 |
|
} |
|
344
|
|
|
|
|
345
|
56 |
|
/** |
|
346
|
|
|
* Retrieve request parameter. |
|
347
|
|
|
* |
|
348
|
|
|
* @param string $parameter |
|
349
|
|
|
* @param ServerRequestInterface $request |
|
350
|
|
|
* @param mixed $default |
|
351
|
|
|
* |
|
352
|
|
|
* @return null|string |
|
353
|
|
|
*/ |
|
354
|
|
|
protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
355
|
|
|
{ |
|
356
|
|
|
$requestParameters = (array) $request->getParsedBody(); |
|
357
|
|
|
|
|
358
|
|
|
return $requestParameters[$parameter] ?? $default; |
|
359
|
61 |
|
} |
|
360
|
|
|
|
|
361
|
61 |
|
/** |
|
362
|
52 |
|
* Retrieve HTTP Basic Auth credentials with the Authorization header |
|
363
|
|
|
* of a request. First index of the returned array is the username, |
|
364
|
|
|
* second is the password (so list() will work). If the header does |
|
365
|
9 |
|
* not exist, or is otherwise an invalid HTTP Basic header, return |
|
366
|
9 |
|
* [null, null]. |
|
367
|
3 |
|
* |
|
368
|
|
|
* @param ServerRequestInterface $request |
|
369
|
|
|
* |
|
370
|
6 |
|
* @return string[]|null[] |
|
371
|
1 |
|
*/ |
|
372
|
|
|
protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
373
|
|
|
{ |
|
374
|
5 |
|
if (!$request->hasHeader('Authorization')) { |
|
375
|
2 |
|
return [null, null]; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
3 |
|
$header = $request->getHeader('Authorization')[0]; |
|
379
|
|
|
if (\strpos($header, 'Basic ') !== 0) { |
|
380
|
|
|
return [null, null]; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
if (!($decoded = \base64_decode(\substr($header, 6)))) { |
|
384
|
|
|
return [null, null]; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
if (\strpos($decoded, ':') === false) { |
|
388
|
|
|
return [null, null]; // HTTP Basic header without colon isn't valid |
|
389
|
|
|
} |
|
390
|
24 |
|
|
|
391
|
|
|
return \explode(':', $decoded, 2); |
|
392
|
24 |
|
} |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* Retrieve query string parameter. |
|
396
|
|
|
* |
|
397
|
|
|
* @param string $parameter |
|
398
|
|
|
* @param ServerRequestInterface $request |
|
399
|
|
|
* @param mixed $default |
|
400
|
|
|
* |
|
401
|
|
|
* @return null|string |
|
402
|
|
|
*/ |
|
403
|
|
|
protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
404
|
1 |
|
{ |
|
405
|
|
|
return isset($request->getQueryParams()[$parameter]) ? $request->getQueryParams()[$parameter] : $default; |
|
406
|
1 |
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* Retrieve cookie parameter. |
|
410
|
|
|
* |
|
411
|
|
|
* @param string $parameter |
|
412
|
|
|
* @param ServerRequestInterface $request |
|
413
|
|
|
* @param mixed $default |
|
414
|
|
|
* |
|
415
|
|
|
* @return null|string |
|
416
|
|
|
*/ |
|
417
|
|
|
protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
418
|
23 |
|
{ |
|
419
|
|
|
return isset($request->getCookieParams()[$parameter]) ? $request->getCookieParams()[$parameter] : $default; |
|
420
|
23 |
|
} |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* Retrieve server parameter. |
|
424
|
|
|
* |
|
425
|
|
|
* @param string $parameter |
|
426
|
|
|
* @param ServerRequestInterface $request |
|
427
|
|
|
* @param mixed $default |
|
428
|
|
|
* |
|
429
|
|
|
* @return null|string |
|
430
|
|
|
*/ |
|
431
|
|
|
protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
432
|
|
|
{ |
|
433
|
|
|
return isset($request->getServerParams()[$parameter]) ? $request->getServerParams()[$parameter] : $default; |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
22 |
|
/** |
|
437
|
|
|
* Issue an access token. |
|
438
|
|
|
* |
|
439
|
|
|
* @param DateInterval $accessTokenTTL |
|
440
|
|
|
* @param ClientEntityInterface $client |
|
441
|
|
|
* @param string|null $userIdentifier |
|
442
|
22 |
|
* @param ScopeEntityInterface[] $scopes |
|
443
|
|
|
* |
|
444
|
22 |
|
* @throws OAuthServerException |
|
445
|
22 |
|
* @throws UniqueTokenIdentifierConstraintViolationException |
|
446
|
22 |
|
* |
|
447
|
|
|
* @return AccessTokenEntityInterface |
|
448
|
22 |
|
*/ |
|
449
|
22 |
|
protected function issueAccessToken( |
|
450
|
|
|
DateInterval $accessTokenTTL, |
|
451
|
22 |
|
ClientEntityInterface $client, |
|
452
|
|
|
$userIdentifier, |
|
453
|
20 |
|
array $scopes = [] |
|
454
|
3 |
|
) { |
|
455
|
2 |
|
$maxGenerationAttempts = self::MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS; |
|
456
|
1 |
|
|
|
457
|
|
|
$accessToken = $this->accessTokenRepository->getNewToken($client, $scopes, $userIdentifier); |
|
458
|
|
|
$accessToken->setExpiryDateTime((new DateTimeImmutable())->add($accessTokenTTL)); |
|
459
|
|
|
$accessToken->setPrivateKey($this->privateKey); |
|
460
|
|
|
|
|
461
|
|
|
while ($maxGenerationAttempts-- > 0) { |
|
462
|
|
|
$accessToken->setIdentifier($this->generateUniqueIdentifier()); |
|
463
|
|
|
try { |
|
464
|
|
|
$this->accessTokenRepository->persistNewAccessToken($accessToken); |
|
465
|
|
|
|
|
466
|
|
|
return $accessToken; |
|
467
|
|
|
} catch (UniqueTokenIdentifierConstraintViolationException $e) { |
|
468
|
|
|
if ($maxGenerationAttempts === 0) { |
|
469
|
|
|
throw $e; |
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
} |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
/** |
|
476
|
6 |
|
* Issue an auth code. |
|
477
|
|
|
* |
|
478
|
|
|
* @param DateInterval $authCodeTTL |
|
479
|
|
|
* @param ClientEntityInterface $client |
|
480
|
|
|
* @param string $userIdentifier |
|
481
|
|
|
* @param string|null $redirectUri |
|
482
|
|
|
* @param ScopeEntityInterface[] $scopes |
|
483
|
6 |
|
* |
|
484
|
|
|
* @throws OAuthServerException |
|
485
|
6 |
|
* @throws UniqueTokenIdentifierConstraintViolationException |
|
486
|
6 |
|
* |
|
487
|
6 |
|
* @return AuthCodeEntityInterface |
|
488
|
6 |
|
*/ |
|
489
|
|
|
protected function issueAuthCode( |
|
490
|
6 |
|
DateInterval $authCodeTTL, |
|
491
|
1 |
|
ClientEntityInterface $client, |
|
492
|
|
|
$userIdentifier, |
|
493
|
|
|
$redirectUri, |
|
494
|
6 |
|
array $scopes = [] |
|
495
|
1 |
|
) { |
|
496
|
|
|
$maxGenerationAttempts = self::MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS; |
|
497
|
|
|
|
|
498
|
6 |
|
$authCode = $this->authCodeRepository->getNewAuthCode(); |
|
499
|
6 |
|
$authCode->setExpiryDateTime((new DateTimeImmutable())->add($authCodeTTL)); |
|
500
|
|
|
$authCode->setClient($client); |
|
501
|
6 |
|
$authCode->setUserIdentifier($userIdentifier); |
|
502
|
|
|
|
|
503
|
4 |
|
if ($redirectUri !== null) { |
|
504
|
3 |
|
$authCode->setRedirectUri($redirectUri); |
|
505
|
2 |
|
} |
|
506
|
1 |
|
|
|
507
|
|
|
foreach ($scopes as $scope) { |
|
508
|
|
|
$authCode->addScope($scope); |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
while ($maxGenerationAttempts-- > 0) { |
|
512
|
|
|
$authCode->setIdentifier($this->generateUniqueIdentifier()); |
|
513
|
|
|
try { |
|
514
|
|
|
$this->authCodeRepository->persistNewAuthCode($authCode); |
|
515
|
|
|
|
|
516
|
|
|
return $authCode; |
|
517
|
|
|
} catch (UniqueTokenIdentifierConstraintViolationException $e) { |
|
518
|
|
|
if ($maxGenerationAttempts === 0) { |
|
519
|
|
|
throw $e; |
|
520
|
16 |
|
} |
|
521
|
|
|
} |
|
522
|
16 |
|
} |
|
523
|
|
|
} |
|
524
|
16 |
|
|
|
525
|
4 |
|
/** |
|
526
|
|
|
* @param AccessTokenEntityInterface $accessToken |
|
527
|
|
|
* |
|
528
|
12 |
|
* @throws OAuthServerException |
|
529
|
12 |
|
* @throws UniqueTokenIdentifierConstraintViolationException |
|
530
|
|
|
* |
|
531
|
12 |
|
* @return RefreshTokenEntityInterface|null |
|
532
|
|
|
*/ |
|
533
|
12 |
|
protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
534
|
12 |
|
{ |
|
535
|
|
|
$refreshToken = $this->refreshTokenRepository->getNewRefreshToken(); |
|
536
|
12 |
|
|
|
537
|
|
|
if ($refreshToken === null) { |
|
538
|
10 |
|
return null; |
|
539
|
3 |
|
} |
|
540
|
2 |
|
|
|
541
|
1 |
|
$refreshToken->setExpiryDateTime((new DateTimeImmutable())->add($this->refreshTokenTTL)); |
|
542
|
|
|
$refreshToken->setAccessToken($accessToken); |
|
543
|
|
|
|
|
544
|
|
|
$maxGenerationAttempts = self::MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS; |
|
545
|
|
|
|
|
546
|
|
|
while ($maxGenerationAttempts-- > 0) { |
|
547
|
|
|
$refreshToken->setIdentifier($this->generateUniqueIdentifier()); |
|
548
|
|
|
try { |
|
549
|
|
|
$this->refreshTokenRepository->persistNewRefreshToken($refreshToken); |
|
550
|
|
|
|
|
551
|
|
|
return $refreshToken; |
|
552
|
|
|
} catch (UniqueTokenIdentifierConstraintViolationException $e) { |
|
553
|
|
|
if ($maxGenerationAttempts === 0) { |
|
554
|
|
|
throw $e; |
|
555
|
|
|
} |
|
556
|
30 |
|
} |
|
557
|
|
|
} |
|
558
|
|
|
} |
|
559
|
30 |
|
|
|
560
|
|
|
/** |
|
561
|
|
|
* Generate a new unique identifier. |
|
562
|
|
|
* |
|
563
|
|
|
* @param int $length |
|
564
|
|
|
* |
|
565
|
|
|
* @throws OAuthServerException |
|
566
|
|
|
* |
|
567
|
|
|
* @return string |
|
568
|
|
|
*/ |
|
569
|
|
|
protected function generateUniqueIdentifier($length = 40) |
|
570
|
|
|
{ |
|
571
|
|
|
try { |
|
572
|
|
|
return \bin2hex(\random_bytes($length)); |
|
573
|
|
|
// @codeCoverageIgnoreStart |
|
574
|
|
|
} catch (TypeError $e) { |
|
575
|
5 |
|
throw OAuthServerException::serverError('An unexpected error has occurred', $e); |
|
576
|
|
|
} catch (Error $e) { |
|
577
|
5 |
|
throw OAuthServerException::serverError('An unexpected error has occurred', $e); |
|
578
|
|
|
} catch (Exception $e) { |
|
579
|
|
|
// If you get this message, the CSPRNG failed hard. |
|
580
|
5 |
|
throw OAuthServerException::serverError('Could not generate a random string', $e); |
|
581
|
5 |
|
} |
|
582
|
|
|
// @codeCoverageIgnoreEnd |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
/** |
|
586
|
|
|
* {@inheritdoc} |
|
587
|
|
|
*/ |
|
588
|
1 |
|
public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
589
|
|
|
{ |
|
590
|
1 |
|
$requestParameters = (array) $request->getParsedBody(); |
|
591
|
|
|
|
|
592
|
|
|
return ( |
|
593
|
|
|
\array_key_exists('grant_type', $requestParameters) |
|
594
|
|
|
&& $requestParameters['grant_type'] === $this->getIdentifier() |
|
595
|
|
|
); |
|
596
|
1 |
|
} |
|
597
|
|
|
|
|
598
|
1 |
|
/** |
|
599
|
|
|
* {@inheritdoc} |
|
600
|
|
|
*/ |
|
601
|
|
|
public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
602
|
|
|
{ |
|
603
|
|
|
return false; |
|
604
|
1 |
|
} |
|
605
|
|
|
|
|
606
|
1 |
|
/** |
|
607
|
|
|
* {@inheritdoc} |
|
608
|
|
|
*/ |
|
609
|
|
|
public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
610
|
|
|
{ |
|
611
|
|
|
throw new LogicException('This grant cannot validate an authorization request'); |
|
612
|
|
|
} |
|
613
|
|
|
|
|
614
|
|
|
/** |
|
615
|
|
|
* {@inheritdoc} |
|
616
|
|
|
*/ |
|
617
|
|
|
public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
618
|
|
|
{ |
|
619
|
|
|
throw new LogicException('This grant cannot complete an authorization request'); |
|
620
|
|
|
} |
|
621
|
|
|
} |
|
622
|
|
|
|