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 Defuse\Crypto\Key; |
13
|
|
|
use Lcobucci\JWT\Parser; |
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\IntrospectionResponse; |
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 null|ResponseTypeInterface |
55
|
|
|
*/ |
56
|
|
|
protected $responseType; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var null|IntrospectionResponse |
60
|
|
|
*/ |
61
|
|
|
protected $introspectionResponseType; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var null|Introspector |
65
|
|
|
*/ |
66
|
|
|
protected $introspector; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var ClientRepositoryInterface |
70
|
|
|
*/ |
71
|
|
|
private $clientRepository; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var AccessTokenRepositoryInterface |
75
|
|
|
*/ |
76
|
|
|
private $accessTokenRepository; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var ScopeRepositoryInterface |
80
|
|
|
*/ |
81
|
|
|
private $scopeRepository; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var string|Key |
85
|
|
|
*/ |
86
|
|
|
private $encryptionKey; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
private $defaultScope = ''; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* New server instance. |
95
|
|
|
* |
96
|
|
|
* @param ClientRepositoryInterface $clientRepository |
97
|
|
|
* @param AccessTokenRepositoryInterface $accessTokenRepository |
98
|
|
|
* @param ScopeRepositoryInterface $scopeRepository |
99
|
|
|
* @param CryptKey|string $privateKey |
100
|
|
|
* @param string|Key $encryptionKey |
101
|
|
|
* @param null|ResponseTypeInterface $responseType |
102
|
|
|
*/ |
103
|
9 |
|
public function __construct( |
104
|
|
|
ClientRepositoryInterface $clientRepository, |
105
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository, |
106
|
|
|
ScopeRepositoryInterface $scopeRepository, |
107
|
|
|
$privateKey, |
108
|
|
|
$encryptionKey, |
109
|
|
|
ResponseTypeInterface $responseType = null |
110
|
|
|
) { |
111
|
9 |
|
$this->clientRepository = $clientRepository; |
112
|
9 |
|
$this->accessTokenRepository = $accessTokenRepository; |
113
|
9 |
|
$this->scopeRepository = $scopeRepository; |
114
|
|
|
|
115
|
9 |
|
if ($privateKey instanceof CryptKey === false) { |
116
|
9 |
|
$privateKey = new CryptKey($privateKey); |
117
|
|
|
} |
118
|
9 |
|
$this->privateKey = $privateKey; |
119
|
9 |
|
$this->encryptionKey = $encryptionKey; |
120
|
9 |
|
$this->responseType = $responseType; |
121
|
9 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Enable a grant type on the server. |
125
|
|
|
* |
126
|
|
|
* @param GrantTypeInterface $grantType |
127
|
|
|
* @param null|\DateInterval $accessTokenTTL |
128
|
|
|
*/ |
129
|
7 |
|
public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null) |
130
|
|
|
{ |
131
|
7 |
|
if ($accessTokenTTL instanceof \DateInterval === false) { |
132
|
4 |
|
$accessTokenTTL = new \DateInterval('PT1H'); |
133
|
|
|
} |
134
|
|
|
|
135
|
7 |
|
$grantType->setAccessTokenRepository($this->accessTokenRepository); |
136
|
7 |
|
$grantType->setClientRepository($this->clientRepository); |
137
|
7 |
|
$grantType->setScopeRepository($this->scopeRepository); |
138
|
7 |
|
$grantType->setDefaultScope($this->defaultScope); |
139
|
7 |
|
$grantType->setPrivateKey($this->privateKey); |
140
|
7 |
|
$grantType->setEmitter($this->getEmitter()); |
141
|
7 |
|
$grantType->setEncryptionKey($this->encryptionKey); |
142
|
|
|
|
143
|
7 |
|
$this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType; |
144
|
7 |
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL; |
145
|
7 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Validate an authorization request |
149
|
|
|
* |
150
|
|
|
* @param ServerRequestInterface $request |
151
|
|
|
* |
152
|
|
|
* @throws OAuthServerException |
153
|
|
|
* |
154
|
|
|
* @return AuthorizationRequest |
155
|
|
|
*/ |
156
|
3 |
|
public function validateAuthorizationRequest(ServerRequestInterface $request) |
157
|
|
|
{ |
158
|
3 |
|
foreach ($this->enabledGrantTypes as $grantType) { |
159
|
2 |
|
if ($grantType->canRespondToAuthorizationRequest($request)) { |
160
|
2 |
|
return $grantType->validateAuthorizationRequest($request); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
throw OAuthServerException::unsupportedGrantType(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Complete an authorization request |
169
|
|
|
* |
170
|
|
|
* @param AuthorizationRequest $authRequest |
171
|
|
|
* @param ResponseInterface $response |
172
|
|
|
* |
173
|
|
|
* @return ResponseInterface |
174
|
|
|
*/ |
175
|
1 |
|
public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response) |
176
|
|
|
{ |
177
|
1 |
|
return $this->enabledGrantTypes[$authRequest->getGrantTypeId()] |
178
|
1 |
|
->completeAuthorizationRequest($authRequest) |
179
|
1 |
|
->generateHttpResponse($response); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Return an access token response. |
184
|
|
|
* |
185
|
|
|
* @param ServerRequestInterface $request |
186
|
|
|
* @param ResponseInterface $response |
187
|
|
|
* |
188
|
|
|
* @throws OAuthServerException |
189
|
|
|
* |
190
|
|
|
* @return ResponseInterface |
191
|
|
|
*/ |
192
|
4 |
|
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response) |
193
|
|
|
{ |
194
|
4 |
|
foreach ($this->enabledGrantTypes as $grantType) { |
195
|
4 |
|
if (!$grantType->canRespondToAccessTokenRequest($request)) { |
196
|
1 |
|
continue; |
197
|
|
|
} |
198
|
3 |
|
$tokenResponse = $grantType->respondToAccessTokenRequest( |
199
|
3 |
|
$request, |
200
|
3 |
|
$this->getResponseType(), |
201
|
3 |
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] |
202
|
|
|
); |
203
|
|
|
|
204
|
2 |
|
if ($tokenResponse instanceof ResponseTypeInterface) { |
205
|
2 |
|
return $tokenResponse->generateHttpResponse($response); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
1 |
|
throw OAuthServerException::unsupportedGrantType(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param IntrospectionResponse $reponseType |
214
|
|
|
*/ |
215
|
|
|
public function setIntrospectionReponseType(IntrospectionResponse $reponseType) |
216
|
|
|
{ |
217
|
|
|
$this->introspectionResponseType = $reponseType; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get the introspection response |
222
|
|
|
* |
223
|
|
|
* @return IntrospectionResponse |
224
|
|
|
*/ |
225
|
|
|
protected function getIntrospectionResponseType() |
226
|
|
|
{ |
227
|
|
|
if ($this->introspectionResponseType instanceof IntrospectionResponse === false) { |
228
|
|
|
$this->introspectionResponseType = new IntrospectionResponse; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this->introspectionResponseType; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Return an introspection response. |
236
|
|
|
* |
237
|
|
|
* @param ServerRequestInterface $request |
238
|
|
|
* @param ResponseInterface $response |
239
|
|
|
* |
240
|
|
|
* @return ResponseInterface |
241
|
|
|
*/ |
242
|
|
|
public function respondToIntrospectionRequest(ServerRequestInterface $request, ResponseInterface $response) |
243
|
|
|
{ |
244
|
|
|
$introspector = $this->getIntrospector(); |
245
|
|
|
|
246
|
|
|
$introspectionResponse = $introspector->respondToIntrospectionRequest( |
247
|
|
|
$request, |
248
|
|
|
$this->getIntrospectionResponseType() |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
return $introspectionResponse->generateHttpResponse($response); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Return an introspection response. |
256
|
|
|
* |
257
|
|
|
* @param ServerRequestInterface $request |
258
|
|
|
*/ |
259
|
|
|
public function validateIntrospectionRequest(ServerRequestInterface $request) |
260
|
|
|
{ |
261
|
|
|
$introspector = $this->getIntrospector(); |
262
|
|
|
$introspector->validateIntrospectionRequest($request); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns the introspector |
267
|
|
|
* |
268
|
|
|
* @return Introspector |
269
|
|
|
*/ |
270
|
|
|
private function getIntrospector() |
271
|
|
|
{ |
272
|
|
|
if (!isset($this->introspector)) { |
273
|
|
|
$this->introspector = new Introspector($this->accessTokenRepository, $this->privateKey, new Parser); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $this->introspector; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get the token type that grants will return in the HTTP response. |
281
|
|
|
* |
282
|
|
|
* @return ResponseTypeInterface |
283
|
|
|
*/ |
284
|
4 |
|
protected function getResponseType() |
285
|
|
|
{ |
286
|
4 |
|
if ($this->responseType instanceof ResponseTypeInterface === false) { |
287
|
1 |
|
$this->responseType = new BearerTokenResponse(); |
288
|
|
|
} |
289
|
|
|
|
290
|
4 |
|
if ($this->responseType instanceof AbstractResponseType === true) { |
291
|
4 |
|
$this->responseType->setPrivateKey($this->privateKey); |
292
|
|
|
} |
293
|
4 |
|
$this->responseType->setEncryptionKey($this->encryptionKey); |
294
|
|
|
|
295
|
4 |
|
return $this->responseType; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Set the default scope for the authorization server. |
300
|
|
|
* |
301
|
|
|
* @param string $defaultScope |
302
|
|
|
*/ |
303
|
3 |
|
public function setDefaultScope($defaultScope) |
304
|
|
|
{ |
305
|
3 |
|
$this->defaultScope = $defaultScope; |
306
|
3 |
|
} |
307
|
|
|
} |
308
|
|
|
|