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