1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Alex Bilbie <[email protected]> |
4
|
|
|
* @copyright Copyright (c) Alex Bilbie |
5
|
|
|
* @license http://mit-license.org/ |
6
|
|
|
* |
7
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace League\OAuth2\Server\Grant; |
11
|
|
|
|
12
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
13
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
14
|
|
|
use League\OAuth2\Server\Entities\UserEntityInterface; |
15
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
16
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; |
17
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; |
18
|
|
|
use League\OAuth2\Server\RequestEvent; |
19
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest; |
20
|
|
|
use League\OAuth2\Server\ResponseTypes\RedirectResponse; |
21
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
23
|
|
|
|
24
|
|
|
class AuthCodeGrant extends AbstractAuthorizeGrant |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \DateInterval |
28
|
|
|
*/ |
29
|
|
|
private $authCodeTTL; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $enableCodeExchangeProof = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param AuthCodeRepositoryInterface $authCodeRepository |
38
|
|
|
* @param RefreshTokenRepositoryInterface $refreshTokenRepository |
39
|
|
|
* @param \DateInterval $authCodeTTL |
40
|
|
|
*/ |
41
|
41 |
|
public function __construct( |
42
|
|
|
AuthCodeRepositoryInterface $authCodeRepository, |
43
|
|
|
RefreshTokenRepositoryInterface $refreshTokenRepository, |
44
|
|
|
\DateInterval $authCodeTTL |
45
|
|
|
) { |
46
|
41 |
|
$this->setAuthCodeRepository($authCodeRepository); |
47
|
41 |
|
$this->setRefreshTokenRepository($refreshTokenRepository); |
48
|
41 |
|
$this->authCodeTTL = $authCodeTTL; |
49
|
41 |
|
$this->refreshTokenTTL = new \DateInterval('P1M'); |
50
|
41 |
|
} |
51
|
|
|
|
52
|
13 |
|
public function enableCodeExchangeProof() |
53
|
|
|
{ |
54
|
13 |
|
$this->enableCodeExchangeProof = true; |
55
|
13 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Respond to an access token request. |
59
|
|
|
* |
60
|
|
|
* @param ServerRequestInterface $request |
61
|
|
|
* @param ResponseTypeInterface $responseType |
62
|
|
|
* @param \DateInterval $accessTokenTTL |
63
|
|
|
* |
64
|
|
|
* @throws OAuthServerException |
65
|
|
|
* |
66
|
|
|
* @return ResponseTypeInterface |
67
|
|
|
*/ |
68
|
18 |
|
public function respondToAccessTokenRequest( |
69
|
|
|
ServerRequestInterface $request, |
70
|
|
|
ResponseTypeInterface $responseType, |
71
|
|
|
\DateInterval $accessTokenTTL |
72
|
|
|
) { |
73
|
|
|
// Validate request |
74
|
18 |
|
$client = $this->validateClient($request); |
75
|
18 |
|
$encryptedAuthCode = $this->getRequestParameter('code', $request, null); |
76
|
|
|
|
77
|
18 |
|
if ($encryptedAuthCode === null) { |
78
|
1 |
|
throw OAuthServerException::invalidRequest('code'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Validate the authorization code |
82
|
|
|
try { |
83
|
17 |
|
$authCodePayload = json_decode($this->decrypt($encryptedAuthCode)); |
84
|
16 |
|
if (time() > $authCodePayload->expire_time) { |
85
|
1 |
|
throw OAuthServerException::invalidRequest('code', 'Authorization code has expired'); |
86
|
|
|
} |
87
|
|
|
|
88
|
15 |
|
if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) { |
89
|
1 |
|
throw OAuthServerException::invalidRequest('code', 'Authorization code has been revoked'); |
90
|
|
|
} |
91
|
|
|
|
92
|
14 |
|
if ($authCodePayload->client_id !== $client->getIdentifier()) { |
93
|
1 |
|
throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// The redirect URI is required in this request |
97
|
13 |
|
$redirectUri = $this->getRequestParameter('redirect_uri', $request, null); |
98
|
13 |
|
if (empty($authCodePayload->redirect_uri) === false && $redirectUri === null) { |
99
|
1 |
|
throw OAuthServerException::invalidRequest('redirect_uri'); |
100
|
|
|
} |
101
|
|
|
|
102
|
12 |
|
if ($authCodePayload->redirect_uri !== $redirectUri) { |
103
|
1 |
|
throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI'); |
104
|
|
|
} |
105
|
|
|
|
106
|
11 |
|
$scopes = []; |
107
|
11 |
|
foreach ($authCodePayload->scopes as $scopeId) { |
108
|
11 |
|
$scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId); |
109
|
|
|
|
110
|
11 |
|
if ($scope instanceof ScopeEntityInterface === false) { |
111
|
|
|
// @codeCoverageIgnoreStart |
112
|
|
|
throw OAuthServerException::invalidScope($scopeId); |
113
|
|
|
// @codeCoverageIgnoreEnd |
114
|
|
|
} |
115
|
|
|
|
116
|
11 |
|
$scopes[] = $scope; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Finalize the requested scopes |
120
|
11 |
|
$scopes = $this->scopeRepository->finalizeScopes( |
121
|
11 |
|
$scopes, |
122
|
11 |
|
$this->getIdentifier(), |
123
|
11 |
|
$client, |
124
|
11 |
|
$authCodePayload->user_id |
125
|
|
|
); |
126
|
6 |
|
} catch (\LogicException $e) { |
127
|
1 |
|
throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Validate code challenge |
131
|
11 |
|
if ($this->enableCodeExchangeProof === true) { |
132
|
7 |
|
$codeVerifier = $this->getRequestParameter('code_verifier', $request, null); |
133
|
7 |
|
if ($codeVerifier === null) { |
134
|
1 |
|
throw OAuthServerException::invalidRequest('code_verifier'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Validate code_verifier according to RFC-7636 |
138
|
|
|
// @see: https://tools.ietf.org/html/rfc7636#section-4.1 |
139
|
6 |
|
if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeVerifier) !== 1) { |
140
|
3 |
|
throw OAuthServerException::invalidRequest( |
141
|
3 |
|
'code_verifier', |
142
|
3 |
|
'Code Verifier must follow the specifications of RFC-7636.' |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
3 |
|
switch ($authCodePayload->code_challenge_method) { |
147
|
3 |
|
case 'plain': |
148
|
2 |
|
if (hash_equals($codeVerifier, $authCodePayload->code_challenge) === false) { |
149
|
1 |
|
throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
break; |
153
|
1 |
|
case 'S256': |
154
|
|
|
if ( |
155
|
1 |
|
hash_equals( |
156
|
1 |
|
strtr(rtrim(base64_encode(hash('sha256', $codeVerifier, true)), '='), '+/', '-_'), |
157
|
1 |
|
$authCodePayload->code_challenge |
158
|
1 |
|
) === false |
159
|
|
|
) { |
160
|
|
|
throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
161
|
|
|
} |
162
|
|
|
// @codeCoverageIgnoreStart |
163
|
|
|
break; |
164
|
|
|
default: |
165
|
|
|
throw OAuthServerException::serverError( |
166
|
|
|
sprintf( |
167
|
|
|
'Unsupported code challenge method `%s`', |
168
|
|
|
$authCodePayload->code_challenge_method |
169
|
|
|
) |
170
|
|
|
); |
171
|
|
|
// @codeCoverageIgnoreEnd |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Issue and persist access + refresh tokens |
176
|
6 |
|
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); |
177
|
6 |
|
$refreshToken = $this->issueRefreshToken($accessToken); |
178
|
|
|
|
179
|
|
|
// Send events to emitter |
180
|
4 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); |
181
|
4 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); |
182
|
|
|
|
183
|
|
|
// Inject tokens into response type |
184
|
4 |
|
$responseType->setAccessToken($accessToken); |
|
|
|
|
185
|
4 |
|
$responseType->setRefreshToken($refreshToken); |
|
|
|
|
186
|
|
|
|
187
|
|
|
// Revoke used auth code |
188
|
4 |
|
$this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id); |
189
|
|
|
|
190
|
4 |
|
return $responseType; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Return the grant identifier that can be used in matching up requests. |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
33 |
|
public function getIdentifier() |
199
|
|
|
{ |
200
|
33 |
|
return 'authorization_code'; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
3 |
|
public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
207
|
|
|
{ |
208
|
|
|
return ( |
209
|
3 |
|
array_key_exists('response_type', $request->getQueryParams()) |
210
|
3 |
|
&& $request->getQueryParams()['response_type'] === 'code' |
211
|
3 |
|
&& isset($request->getQueryParams()['client_id']) |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
14 |
|
public function validateAuthorizationRequest(ServerRequestInterface $request) |
219
|
|
|
{ |
220
|
14 |
|
$clientId = $this->getQueryStringParameter( |
221
|
14 |
|
'client_id', |
222
|
14 |
|
$request, |
223
|
14 |
|
$this->getServerParameter('PHP_AUTH_USER', $request) |
224
|
|
|
); |
225
|
|
|
|
226
|
14 |
|
if (is_null($clientId)) { |
227
|
1 |
|
throw OAuthServerException::invalidRequest('client_id'); |
228
|
|
|
} |
229
|
|
|
|
230
|
13 |
|
$client = $this->clientRepository->getClientEntity( |
231
|
13 |
|
$clientId, |
232
|
13 |
|
$this->getIdentifier(), |
233
|
13 |
|
null, |
234
|
13 |
|
false |
235
|
|
|
); |
236
|
|
|
|
237
|
13 |
|
if ($client instanceof ClientEntityInterface === false) { |
238
|
1 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
239
|
1 |
|
throw OAuthServerException::invalidClient(); |
240
|
|
|
} |
241
|
|
|
|
242
|
12 |
|
$redirectUri = $this->getQueryStringParameter('redirect_uri', $request); |
243
|
|
|
|
244
|
12 |
|
if ($redirectUri !== null) { |
245
|
10 |
|
$this->validateRedirectUri($redirectUri, $client, $request); |
246
|
2 |
|
} elseif (is_array($client->getRedirectUri()) && count($client->getRedirectUri()) !== 1 |
247
|
2 |
|
|| empty($client->getRedirectUri())) { |
248
|
1 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
249
|
1 |
|
throw OAuthServerException::invalidClient(); |
250
|
|
|
} else { |
251
|
1 |
|
$redirectUri = is_array($client->getRedirectUri()) |
252
|
|
|
? $client->getRedirectUri()[0] |
253
|
1 |
|
: $client->getRedirectUri(); |
254
|
|
|
} |
255
|
|
|
|
256
|
9 |
|
$scopes = $this->validateScopes( |
257
|
9 |
|
$this->getQueryStringParameter('scope', $request, $this->defaultScope), |
258
|
9 |
|
$redirectUri |
259
|
|
|
); |
260
|
|
|
|
261
|
9 |
|
$stateParameter = $this->getQueryStringParameter('state', $request); |
262
|
|
|
|
263
|
9 |
|
$authorizationRequest = new AuthorizationRequest(); |
264
|
9 |
|
$authorizationRequest->setGrantTypeId($this->getIdentifier()); |
265
|
9 |
|
$authorizationRequest->setClient($client); |
266
|
9 |
|
$authorizationRequest->setRedirectUri($redirectUri); |
267
|
|
|
|
268
|
9 |
|
if ($stateParameter !== null) { |
269
|
|
|
$authorizationRequest->setState($stateParameter); |
270
|
|
|
} |
271
|
|
|
|
272
|
9 |
|
$authorizationRequest->setScopes($scopes); |
273
|
|
|
|
274
|
9 |
|
if ($this->enableCodeExchangeProof === true) { |
275
|
6 |
|
$codeChallenge = $this->getQueryStringParameter('code_challenge', $request); |
276
|
6 |
|
if ($codeChallenge === null) { |
277
|
1 |
|
throw OAuthServerException::invalidRequest('code_challenge'); |
278
|
|
|
} |
279
|
|
|
|
280
|
5 |
|
$codeChallengeMethod = $this->getQueryStringParameter('code_challenge_method', $request, 'plain'); |
281
|
|
|
|
282
|
5 |
|
if (in_array($codeChallengeMethod, ['plain', 'S256'], true) === false) { |
283
|
1 |
|
throw OAuthServerException::invalidRequest( |
284
|
1 |
|
'code_challenge_method', |
285
|
1 |
|
'Code challenge method must be `plain` or `S256`' |
286
|
|
|
); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
// Validate code_challenge according to RFC-7636 |
290
|
|
|
// @see: https://tools.ietf.org/html/rfc7636#section-4.2 |
291
|
4 |
|
if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeChallenge) !== 1) { |
292
|
3 |
|
throw OAuthServerException::invalidRequest( |
293
|
3 |
|
'code_challenged', |
294
|
3 |
|
'Code challenge must follow the specifications of RFC-7636.' |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
|
298
|
1 |
|
$authorizationRequest->setCodeChallenge($codeChallenge); |
299
|
1 |
|
$authorizationRequest->setCodeChallengeMethod($codeChallengeMethod); |
300
|
|
|
} |
301
|
|
|
|
302
|
4 |
|
return $authorizationRequest; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* {@inheritdoc} |
307
|
|
|
*/ |
308
|
7 |
|
public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
309
|
|
|
{ |
310
|
7 |
|
if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) { |
311
|
1 |
|
throw new \LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest'); |
312
|
|
|
} |
313
|
|
|
|
314
|
6 |
|
$finalRedirectUri = ($authorizationRequest->getRedirectUri() === null) |
315
|
6 |
|
? is_array($authorizationRequest->getClient()->getRedirectUri()) |
316
|
|
|
? $authorizationRequest->getClient()->getRedirectUri()[0] |
317
|
6 |
|
: $authorizationRequest->getClient()->getRedirectUri() |
318
|
6 |
|
: $authorizationRequest->getRedirectUri(); |
319
|
|
|
|
320
|
|
|
// The user approved the client, redirect them back with an auth code |
321
|
6 |
|
if ($authorizationRequest->isAuthorizationApproved() === true) { |
322
|
5 |
|
$authCode = $this->issueAuthCode( |
323
|
5 |
|
$this->authCodeTTL, |
324
|
5 |
|
$authorizationRequest->getClient(), |
325
|
5 |
|
$authorizationRequest->getUser()->getIdentifier(), |
326
|
5 |
|
$authorizationRequest->getRedirectUri(), |
327
|
5 |
|
$authorizationRequest->getScopes() |
328
|
|
|
); |
329
|
|
|
|
330
|
|
|
$payload = [ |
331
|
3 |
|
'client_id' => $authCode->getClient()->getIdentifier(), |
332
|
3 |
|
'redirect_uri' => $authCode->getRedirectUri(), |
333
|
3 |
|
'auth_code_id' => $authCode->getIdentifier(), |
334
|
3 |
|
'scopes' => $authCode->getScopes(), |
335
|
3 |
|
'user_id' => $authCode->getUserIdentifier(), |
336
|
3 |
|
'expire_time' => (new \DateTime())->add($this->authCodeTTL)->format('U'), |
337
|
3 |
|
'code_challenge' => $authorizationRequest->getCodeChallenge(), |
338
|
3 |
|
'code_challenge_method' => $authorizationRequest->getCodeChallengeMethod(), |
339
|
|
|
]; |
340
|
|
|
|
341
|
3 |
|
$response = new RedirectResponse(); |
342
|
3 |
|
$response->setRedirectUri( |
343
|
3 |
|
$this->makeRedirectUri( |
344
|
3 |
|
$finalRedirectUri, |
345
|
|
|
[ |
346
|
3 |
|
'code' => $this->encrypt( |
347
|
3 |
|
json_encode( |
348
|
3 |
|
$payload |
349
|
|
|
) |
350
|
|
|
), |
351
|
3 |
|
'state' => $authorizationRequest->getState(), |
352
|
|
|
] |
353
|
|
|
) |
354
|
|
|
); |
355
|
|
|
|
356
|
3 |
|
return $response; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
// The user denied the client, redirect them back with an error |
360
|
1 |
|
throw OAuthServerException::accessDenied( |
361
|
1 |
|
'The user denied the request', |
362
|
1 |
|
$this->makeRedirectUri( |
363
|
1 |
|
$finalRedirectUri, |
364
|
|
|
[ |
365
|
1 |
|
'state' => $authorizationRequest->getState(), |
366
|
|
|
] |
367
|
|
|
) |
368
|
|
|
); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: