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