1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Alexandre |
5
|
|
|
* Date: 18/02/2018 |
6
|
|
|
* Time: 18:08 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OAuth2\Flows; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use OAuth2\Endpoints\AuthorizationEndpoint; |
13
|
|
|
use OAuth2\Endpoints\TokenEndpoint; |
14
|
|
|
use OAuth2\Exceptions\OAuthException; |
15
|
|
|
use OAuth2\GrantTypes\AbstractGrantType; |
16
|
|
|
use OAuth2\Storages\AccessTokenStorageInterface; |
17
|
|
|
use OAuth2\Storages\AuthorizationCodeStorageInterface; |
18
|
|
|
use OAuth2\Storages\RefreshTokenStorageInterface; |
19
|
|
|
|
20
|
|
|
class AuthorizationCodeFlow extends AbstractGrantType implements FlowInterface |
21
|
|
|
{ |
22
|
|
|
protected $authorizationCodeStorage; |
23
|
|
|
|
24
|
|
|
public function __construct(AuthorizationCodeStorageInterface $authorizationCodeStorage, |
25
|
|
|
AccessTokenStorageInterface $accessTokenStorage, |
26
|
|
|
RefreshTokenStorageInterface $refreshTokenStorage) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($accessTokenStorage, $refreshTokenStorage); |
29
|
|
|
$this->authorizationCodeStorage = $authorizationCodeStorage; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getResponseTypes(): array |
33
|
|
|
{ |
34
|
|
|
return ['code']; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function verifyAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData) |
38
|
|
|
{ |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array |
42
|
|
|
{ |
43
|
|
|
$authorizationCode = $this->authorizationCodeStorage->create( |
44
|
|
|
implode(' ', $authorizationEndpoint->getScopes()), |
45
|
|
|
$authorizationEndpoint->getClient()->getIdentifier(), |
46
|
|
|
$authorizationEndpoint->getResourceOwner()->getIdentifier(), |
47
|
|
|
$requestData['scope'] ?? null, |
48
|
|
|
$requestData['redirect_uri'] ?? null |
49
|
|
|
); |
50
|
|
|
$this->authorizationCodeStorage->save($authorizationCode); |
51
|
|
|
return ['code' => $authorizationCode->getCode()]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getDefaultResponseMode(): string |
55
|
|
|
{ |
56
|
|
|
return 'query'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getUnsupportedResponseModes(): array |
60
|
|
|
{ |
61
|
|
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getGrantTypes(): array |
65
|
|
|
{ |
66
|
|
|
return ['authorization_code']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param TokenEndpoint $tokenEndpoint |
71
|
|
|
* @param array $requestData |
72
|
|
|
* @return array |
73
|
|
|
* @throws OAuthException |
74
|
|
|
*/ |
75
|
|
|
public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array |
76
|
|
|
{ |
77
|
|
|
if (empty($requestData['code'])) { |
78
|
|
|
throw new OAuthException('invalid_request', |
79
|
|
|
'The request is missing the required parameter code.', |
80
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.4'); |
81
|
|
|
} |
82
|
|
|
$code = $requestData['code']; |
83
|
|
|
|
84
|
|
|
$authorizationCode = $this->authorizationCodeStorage->find($code); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* ensure that the authorization code was issued to the authenticated |
88
|
|
|
* confidential client, or if the client is public, ensure that the |
89
|
|
|
* code was issued to "client_id" in the request, |
90
|
|
|
*/ |
91
|
|
|
if (!$authorizationCode || $authorizationCode->getClientIdentifier() !== $tokenEndpoint->getClient()->getIdentifier()) { |
92
|
|
|
throw new OAuthException('invalid_grant', |
93
|
|
|
'The request includes the invalid parameter code.', |
94
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.4'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->authorizationCodeStorage->revoke($code); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* verify that the authorization code is valid |
101
|
|
|
*/ |
102
|
|
|
if ($this->authorizationCodeStorage->hasExpired($authorizationCode)) { |
103
|
|
|
throw new OAuthException('invalid_grant', |
104
|
|
|
'The request includes the invalid parameter code. The code has expired.', |
105
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.4'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* ensure that the "redirect_uri" parameter is present if the |
110
|
|
|
* "redirect_uri" parameter was included in the initial authorization |
111
|
|
|
* request as described in Section 4.1.1, and if included ensure that |
112
|
|
|
* their values are identical. |
113
|
|
|
*/ |
114
|
|
|
if ($authorizationCode->getRedirectUri()) { |
115
|
|
|
if (empty($requestData['redirect_uri'])) { |
116
|
|
|
throw new OAuthException('invalid_request', |
117
|
|
|
'The request is missing the required parameter redirect_uri', |
118
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.1'); |
119
|
|
|
} |
120
|
|
|
if ($requestData['redirect_uri'] !== $authorizationCode->getRedirectUri()) { |
121
|
|
|
throw new OAuthException('invalid_request', |
122
|
|
|
'The request includes the invalid parameter redirect_uri', |
123
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.1'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->issueTokens($authorizationCode->getScope(), |
128
|
|
|
$authorizationCode->getResourceOwnerIdentifier(), $authorizationCode->getCode()); |
129
|
|
|
} |
130
|
|
|
} |