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