|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Alexandre |
|
5
|
|
|
* Date: 07/03/2018 |
|
6
|
|
|
* Time: 23:43 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OAuth2\Extensions\PKCE\Flows; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use OAuth2\Endpoints\AuthorizationEndpoint; |
|
13
|
|
|
use OAuth2\Endpoints\TokenEndpoint; |
|
14
|
|
|
use OAuth2\Exceptions\OAuthException; |
|
15
|
|
|
use OAuth2\Extensions\PKCE\Credentials\CodeChallenge; |
|
16
|
|
|
use OAuth2\Extensions\PKCE\Storages\AuthorizationCodeStorageInterface; |
|
17
|
|
|
use OAuth2\Roles\Clients\PublicClient; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AuthorizationCodeFlow |
|
21
|
|
|
* @package OAuth2\Extensions\PKCE\Flows |
|
22
|
|
|
* @rfc https://tools.ietf.org/html/rfc7636 |
|
23
|
|
|
*/ |
|
24
|
|
|
class AuthorizationCodeFlow extends \OAuth2\Flows\AuthorizationCodeFlow |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* AuthorizationCodeFlow constructor. |
|
28
|
|
|
* @param AuthorizationCodeStorageInterface $authorizationCodeStorage |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(AuthorizationCodeStorageInterface $authorizationCodeStorage) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct($authorizationCodeStorage); |
|
33
|
|
|
$this->authorizationCodeStorage = $authorizationCodeStorage; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param AuthorizationEndpoint $authorizationEndpoint |
|
38
|
|
|
* @param array $requestData |
|
39
|
|
|
* @return array |
|
40
|
|
|
* @throws OAuthException |
|
41
|
|
|
*/ |
|
42
|
|
|
function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$authorizationCode = $this->authorizationCodeStorage->create( |
|
45
|
|
|
implode(' ', $authorizationEndpoint->getScopes()), |
|
46
|
|
|
$authorizationEndpoint->getClient()->getIdentifier(), |
|
47
|
|
|
$authorizationEndpoint->getResourceOwner()->getIdentifier(), |
|
48
|
|
|
$requestData['scope'] ?? null, |
|
49
|
|
|
$requestData['redirect_uri'] ?? null |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
if (empty($requestData['code_challenge'])) { |
|
53
|
|
|
if ($authorizationEndpoint->getClient() instanceof PublicClient) { |
|
54
|
|
|
throw new OAuthException('invalid_request', |
|
55
|
|
|
'The request is missing the required parameter code_challenge for public clients', |
|
56
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.4'); |
|
57
|
|
|
} |
|
58
|
|
|
} else { |
|
59
|
|
|
$codeChallengeMethod = empty($requestData['code_challenge_method']) ? 'plain' : $requestData['code_challenge_method']; |
|
60
|
|
|
if(!in_array($codeChallengeMethod, ['plain', 'S256'])) { |
|
61
|
|
|
throw new OAuthException('invalid_request', |
|
62
|
|
|
'The request includes the invalid parameter code_challenge_method. Supported : plain, S256', |
|
63
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$codeChallenge = new CodeChallenge($requestData['code_challenge'], $codeChallengeMethod); |
|
67
|
|
|
$this->authorizationCodeStorage->associate($codeChallenge, $authorizationCode); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->authorizationCodeStorage->save($authorizationCode); |
|
71
|
|
|
|
|
72
|
|
|
return ['code' => $authorizationCode->getCode()]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array |
|
76
|
|
|
{ |
|
77
|
|
|
// TODO https://tools.ietf.org/html/rfc7636#section-4.2 |
|
78
|
|
|
|
|
79
|
|
|
return parent::handleAccessTokenRequest($tokenEndpoint, $requestData); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.