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; |
11
|
|
|
|
12
|
|
|
use DateInterval; |
13
|
|
|
use Defuse\Crypto\Key; |
14
|
|
|
use League\Event\EmitterAwareInterface; |
15
|
|
|
use League\Event\EmitterAwareTrait; |
16
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
17
|
|
|
use League\OAuth2\Server\Grant\GrantTypeInterface; |
18
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
19
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; |
20
|
|
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; |
21
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest; |
22
|
|
|
use League\OAuth2\Server\ResponseTypes\AbstractResponseType; |
23
|
|
|
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse; |
24
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
26
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
27
|
|
|
|
28
|
|
|
class AuthorizationServer implements EmitterAwareInterface |
29
|
|
|
{ |
30
|
|
|
use EmitterAwareTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var GrantTypeInterface[] |
34
|
|
|
*/ |
35
|
|
|
protected $enabledGrantTypes = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var DateInterval[] |
39
|
|
|
*/ |
40
|
|
|
protected $grantTypeAccessTokenTTL = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var CryptKey |
44
|
|
|
*/ |
45
|
|
|
protected $privateKey; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var CryptKey |
49
|
|
|
*/ |
50
|
|
|
protected $publicKey; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var ResponseTypeInterface |
54
|
|
|
*/ |
55
|
|
|
protected $responseType; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ClientRepositoryInterface |
59
|
|
|
*/ |
60
|
|
|
private $clientRepository; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var AccessTokenRepositoryInterface |
64
|
|
|
*/ |
65
|
|
|
private $accessTokenRepository; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var ScopeRepositoryInterface |
69
|
|
|
*/ |
70
|
|
|
private $scopeRepository; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string|Key |
74
|
|
|
*/ |
75
|
|
|
private $encryptionKey; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var string |
79
|
|
|
*/ |
80
|
|
|
private $defaultScope = ''; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var bool |
84
|
|
|
*/ |
85
|
|
|
private $revokeRefreshTokens = true; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* New server instance. |
89
|
|
|
* |
90
|
|
|
* @param ClientRepositoryInterface $clientRepository |
91
|
|
|
* @param AccessTokenRepositoryInterface $accessTokenRepository |
92
|
11 |
|
* @param ScopeRepositoryInterface $scopeRepository |
93
|
|
|
* @param CryptKey|string $privateKey |
94
|
|
|
* @param string|Key $encryptionKey |
95
|
|
|
* @param null|ResponseTypeInterface $responseType |
96
|
|
|
*/ |
97
|
|
|
public function __construct( |
98
|
|
|
ClientRepositoryInterface $clientRepository, |
99
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository, |
100
|
11 |
|
ScopeRepositoryInterface $scopeRepository, |
101
|
11 |
|
$privateKey, |
102
|
11 |
|
$encryptionKey, |
103
|
|
|
ResponseTypeInterface $responseType = null |
104
|
11 |
|
) { |
105
|
11 |
|
$this->clientRepository = $clientRepository; |
106
|
|
|
$this->accessTokenRepository = $accessTokenRepository; |
107
|
|
|
$this->scopeRepository = $scopeRepository; |
108
|
11 |
|
|
109
|
11 |
|
if ($privateKey instanceof CryptKey === false) { |
110
|
|
|
$privateKey = new CryptKey($privateKey); |
111
|
11 |
|
} |
112
|
6 |
|
|
113
|
|
|
$this->privateKey = $privateKey; |
114
|
5 |
|
$this->encryptionKey = $encryptionKey; |
115
|
|
|
|
116
|
|
|
if ($responseType === null) { |
117
|
11 |
|
$responseType = new BearerTokenResponse(); |
118
|
11 |
|
} else { |
119
|
|
|
$responseType = clone $responseType; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->responseType = $responseType; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
7 |
|
* Enable a grant type on the server. |
127
|
|
|
* |
128
|
7 |
|
* @param GrantTypeInterface $grantType |
129
|
4 |
|
* @param null|DateInterval $accessTokenTTL |
130
|
|
|
*/ |
131
|
|
|
public function enableGrantType(GrantTypeInterface $grantType, DateInterval $accessTokenTTL = null) |
132
|
7 |
|
{ |
133
|
7 |
|
if ($accessTokenTTL === null) { |
134
|
7 |
|
$accessTokenTTL = new DateInterval('PT1H'); |
135
|
7 |
|
} |
136
|
7 |
|
|
137
|
7 |
|
$grantType->setAccessTokenRepository($this->accessTokenRepository); |
138
|
7 |
|
$grantType->setClientRepository($this->clientRepository); |
139
|
|
|
$grantType->setScopeRepository($this->scopeRepository); |
140
|
7 |
|
$grantType->setDefaultScope($this->defaultScope); |
141
|
7 |
|
$grantType->setPrivateKey($this->privateKey); |
142
|
7 |
|
$grantType->setEmitter($this->getEmitter()); |
143
|
|
|
$grantType->setEncryptionKey($this->encryptionKey); |
144
|
|
|
$grantType->setRevokeRefreshTokens($this->revokeRefreshTokens); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType; |
147
|
|
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Validate an authorization request |
152
|
|
|
* |
153
|
3 |
|
* @param ServerRequestInterface $request |
154
|
|
|
* |
155
|
3 |
|
* @throws OAuthServerException |
156
|
2 |
|
* |
157
|
2 |
|
* @return AuthorizationRequest |
158
|
|
|
*/ |
159
|
|
|
public function validateAuthorizationRequest(ServerRequestInterface $request) |
160
|
|
|
{ |
161
|
1 |
|
foreach ($this->enabledGrantTypes as $grantType) { |
162
|
|
|
if ($grantType->canRespondToAuthorizationRequest($request)) { |
163
|
|
|
return $grantType->validateAuthorizationRequest($request); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
throw OAuthServerException::unsupportedGrantType(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Complete an authorization request |
172
|
1 |
|
* |
173
|
|
|
* @param AuthorizationRequest $authRequest |
174
|
1 |
|
* @param ResponseInterface $response |
175
|
1 |
|
* |
176
|
1 |
|
* @return ResponseInterface |
177
|
|
|
*/ |
178
|
|
|
public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response) |
179
|
|
|
{ |
180
|
|
|
return $this->enabledGrantTypes[$authRequest->getGrantTypeId()] |
181
|
|
|
->completeAuthorizationRequest($authRequest) |
182
|
|
|
->generateHttpResponse($response); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Return an access token response. |
187
|
|
|
* |
188
|
|
|
* @param ServerRequestInterface $request |
189
|
4 |
|
* @param ResponseInterface $response |
190
|
|
|
* |
191
|
4 |
|
* @throws OAuthServerException |
192
|
4 |
|
* |
193
|
1 |
|
* @return ResponseInterface |
194
|
|
|
*/ |
195
|
3 |
|
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response) |
196
|
|
|
{ |
197
|
3 |
|
foreach ($this->enabledGrantTypes as $grantType) { |
198
|
3 |
|
if (!$grantType->canRespondToAccessTokenRequest($request)) { |
199
|
|
|
continue; |
200
|
|
|
} |
201
|
2 |
|
$tokenResponse = $grantType->respondToAccessTokenRequest( |
202
|
2 |
|
$request, |
203
|
|
|
$this->getResponseType(), |
204
|
|
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] |
205
|
|
|
); |
206
|
1 |
|
|
207
|
|
|
if ($tokenResponse instanceof ResponseTypeInterface) { |
208
|
|
|
return $tokenResponse->generateHttpResponse($response); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
throw OAuthServerException::unsupportedGrantType(); |
213
|
|
|
} |
214
|
6 |
|
|
215
|
|
|
/** |
216
|
6 |
|
* Get the token type that grants will return in the HTTP response. |
217
|
|
|
* |
218
|
6 |
|
* @return ResponseTypeInterface |
219
|
6 |
|
*/ |
220
|
|
|
protected function getResponseType() |
221
|
|
|
{ |
222
|
6 |
|
$responseType = clone $this->responseType; |
223
|
|
|
|
224
|
6 |
|
if ($responseType instanceof AbstractResponseType) { |
225
|
|
|
$responseType->setPrivateKey($this->privateKey); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$responseType->setEncryptionKey($this->encryptionKey); |
229
|
|
|
|
230
|
|
|
return $responseType; |
231
|
|
|
} |
232
|
3 |
|
|
233
|
|
|
/** |
234
|
3 |
|
* Set the default scope for the authorization server. |
235
|
3 |
|
* |
236
|
|
|
* @param string $defaultScope |
237
|
|
|
*/ |
238
|
|
|
public function setDefaultScope($defaultScope) |
239
|
|
|
{ |
240
|
|
|
$this->defaultScope = $defaultScope; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Sets wether to revoke refresh tokens or not (for all grant types). |
245
|
|
|
* |
246
|
|
|
* @param bool $revokeRefreshTokens |
247
|
|
|
*/ |
248
|
|
|
public function setRevokeRefreshTokens(bool $revokeRefreshTokens): void |
249
|
|
|
{ |
250
|
|
|
$this->revokeRefreshTokens = $revokeRefreshTokens; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|