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 League\Event\EmitterAwareInterface; |
14
|
|
|
use League\Event\EmitterAwareTrait; |
15
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
16
|
|
|
use League\OAuth2\Server\Grant\GrantTypeInterface; |
17
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
18
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; |
19
|
|
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; |
20
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest; |
21
|
|
|
use League\OAuth2\Server\ResponseTypes\AbstractResponseType; |
22
|
|
|
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse; |
23
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
25
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
26
|
|
|
use Lcobucci\JWT\Parser; |
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 null|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
|
|
|
* New server instance. |
84
|
|
|
* |
85
|
|
|
* @param ClientRepositoryInterface $clientRepository |
86
|
|
|
* @param AccessTokenRepositoryInterface $accessTokenRepository |
87
|
|
|
* @param ScopeRepositoryInterface $scopeRepository |
88
|
|
|
* @param CryptKey|string $privateKey |
89
|
|
|
* @param string|Key $encryptionKey |
90
|
|
|
* @param null|ResponseTypeInterface $responseType |
91
|
|
|
*/ |
92
|
9 |
|
public function __construct( |
93
|
|
|
ClientRepositoryInterface $clientRepository, |
94
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository, |
95
|
|
|
ScopeRepositoryInterface $scopeRepository, |
96
|
|
|
$privateKey, |
97
|
|
|
$encryptionKey, |
98
|
|
|
ResponseTypeInterface $responseType = null |
99
|
|
|
) { |
100
|
9 |
|
$this->clientRepository = $clientRepository; |
101
|
9 |
|
$this->accessTokenRepository = $accessTokenRepository; |
102
|
9 |
|
$this->scopeRepository = $scopeRepository; |
103
|
|
|
|
104
|
9 |
|
if ($privateKey instanceof CryptKey === false) { |
105
|
9 |
|
$privateKey = new CryptKey($privateKey); |
106
|
|
|
} |
107
|
9 |
|
$this->privateKey = $privateKey; |
108
|
9 |
|
$this->encryptionKey = $encryptionKey; |
109
|
9 |
|
$this->responseType = $responseType; |
110
|
9 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Enable a grant type on the server. |
114
|
|
|
* |
115
|
|
|
* @param GrantTypeInterface $grantType |
116
|
|
|
* @param null|\DateInterval $accessTokenTTL |
117
|
|
|
*/ |
118
|
7 |
|
public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null) |
119
|
|
|
{ |
120
|
7 |
|
if ($accessTokenTTL instanceof \DateInterval === false) { |
121
|
4 |
|
$accessTokenTTL = new \DateInterval('PT1H'); |
122
|
|
|
} |
123
|
|
|
|
124
|
7 |
|
$grantType->setAccessTokenRepository($this->accessTokenRepository); |
125
|
7 |
|
$grantType->setClientRepository($this->clientRepository); |
126
|
7 |
|
$grantType->setScopeRepository($this->scopeRepository); |
127
|
7 |
|
$grantType->setDefaultScope($this->defaultScope); |
128
|
7 |
|
$grantType->setPrivateKey($this->privateKey); |
129
|
7 |
|
$grantType->setEmitter($this->getEmitter()); |
130
|
7 |
|
$grantType->setEncryptionKey($this->encryptionKey); |
131
|
|
|
|
132
|
7 |
|
$this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType; |
133
|
7 |
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL; |
134
|
7 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Validate an authorization request |
138
|
|
|
* |
139
|
|
|
* @param ServerRequestInterface $request |
140
|
|
|
* |
141
|
|
|
* @throws OAuthServerException |
142
|
|
|
* |
143
|
|
|
* @return AuthorizationRequest |
144
|
|
|
*/ |
145
|
3 |
|
public function validateAuthorizationRequest(ServerRequestInterface $request) |
146
|
|
|
{ |
147
|
3 |
|
foreach ($this->enabledGrantTypes as $grantType) { |
148
|
2 |
|
if ($grantType->canRespondToAuthorizationRequest($request)) { |
149
|
2 |
|
return $grantType->validateAuthorizationRequest($request); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
throw OAuthServerException::unsupportedGrantType(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Complete an authorization request |
158
|
|
|
* |
159
|
|
|
* @param AuthorizationRequest $authRequest |
160
|
|
|
* @param ResponseInterface $response |
161
|
|
|
* |
162
|
|
|
* @return ResponseInterface |
163
|
|
|
*/ |
164
|
1 |
|
public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response) |
165
|
|
|
{ |
166
|
1 |
|
return $this->enabledGrantTypes[$authRequest->getGrantTypeId()] |
167
|
1 |
|
->completeAuthorizationRequest($authRequest) |
168
|
1 |
|
->generateHttpResponse($response); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return an access token response. |
173
|
|
|
* |
174
|
|
|
* @param ServerRequestInterface $request |
175
|
|
|
* @param ResponseInterface $response |
176
|
|
|
* |
177
|
|
|
* @throws OAuthServerException |
178
|
|
|
* |
179
|
|
|
* @return ResponseInterface |
180
|
|
|
*/ |
181
|
4 |
|
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response) |
182
|
|
|
{ |
183
|
4 |
|
foreach ($this->enabledGrantTypes as $grantType) { |
184
|
4 |
|
if (!$grantType->canRespondToAccessTokenRequest($request)) { |
185
|
1 |
|
continue; |
186
|
|
|
} |
187
|
3 |
|
$tokenResponse = $grantType->respondToAccessTokenRequest( |
188
|
3 |
|
$request, |
189
|
3 |
|
$this->getResponseType(), |
190
|
3 |
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] |
191
|
|
|
); |
192
|
|
|
|
193
|
2 |
|
if ($tokenResponse instanceof ResponseTypeInterface) { |
194
|
2 |
|
return $tokenResponse->generateHttpResponse($response); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
throw OAuthServerException::unsupportedGrantType(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Return an introspection response. |
203
|
|
|
* |
204
|
|
|
* @param ServerRequestInterface $request |
205
|
|
|
* @param ResponseInterface $response |
206
|
|
|
* |
207
|
|
|
* @return ResponseInterface |
208
|
|
|
*/ |
209
|
|
|
public function respondToIntrospectionRequest(ServerRequestInterface $request, ResponseInterface $response) |
210
|
|
|
{ |
211
|
|
|
$introspector = new Introspector($this->accessTokenRepository, $this->privateKey, new Parser); |
212
|
|
|
$introspectionResponse = $introspector->respondToIntrospectionRequest($request); |
213
|
|
|
|
214
|
|
|
return $introspectionResponse->generateHttpResponse($response); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the token type that grants will return in the HTTP response. |
219
|
|
|
* |
220
|
|
|
* @return ResponseTypeInterface |
221
|
|
|
*/ |
222
|
4 |
|
protected function getResponseType() |
223
|
|
|
{ |
224
|
4 |
|
if ($this->responseType instanceof ResponseTypeInterface === false) { |
225
|
1 |
|
$this->responseType = new BearerTokenResponse(); |
226
|
|
|
} |
227
|
|
|
|
228
|
4 |
|
if ($this->responseType instanceof AbstractResponseType === true) { |
229
|
4 |
|
$this->responseType->setPrivateKey($this->privateKey); |
230
|
|
|
} |
231
|
4 |
|
$this->responseType->setEncryptionKey($this->encryptionKey); |
232
|
|
|
|
233
|
4 |
|
return $this->responseType; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Set the default scope for the authorization server. |
238
|
|
|
* |
239
|
|
|
* @param string $defaultScope |
240
|
|
|
*/ |
241
|
3 |
|
public function setDefaultScope($defaultScope) |
242
|
|
|
{ |
243
|
3 |
|
$this->defaultScope = $defaultScope; |
244
|
3 |
|
} |
245
|
|
|
} |
246
|
|
|
|