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
|
|
|
* New server instance. |
71
|
|
|
* |
72
|
|
|
* @param ClientRepositoryInterface $clientRepository |
73
|
|
|
* @param AccessTokenRepositoryInterface $accessTokenRepository |
74
|
|
|
* @param ScopeRepositoryInterface $scopeRepository |
75
|
|
|
* @param CryptKey|string $privateKey |
76
|
|
|
* @param CryptKey|string $publicKey |
77
|
|
|
* @param null|ResponseTypeInterface $responseType |
78
|
|
|
*/ |
79
|
|
|
public function __construct( |
80
|
|
|
ClientRepositoryInterface $clientRepository, |
81
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository, |
82
|
|
|
ScopeRepositoryInterface $scopeRepository, |
83
|
|
|
$privateKey, |
84
|
|
|
$publicKey, |
85
|
|
|
ResponseTypeInterface $responseType = null |
86
|
|
|
) { |
87
|
|
|
$this->clientRepository = $clientRepository; |
88
|
|
|
$this->accessTokenRepository = $accessTokenRepository; |
89
|
|
|
$this->scopeRepository = $scopeRepository; |
90
|
|
|
|
91
|
|
|
if ($privateKey instanceof CryptKey === false) { |
92
|
|
|
$privateKey = new CryptKey($privateKey); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
$this->privateKey = $privateKey; |
95
|
|
|
|
96
|
|
|
if ($publicKey instanceof CryptKey === false) { |
97
|
|
|
$publicKey = new CryptKey($publicKey); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
$this->publicKey = $publicKey; |
100
|
|
|
|
101
|
|
|
$this->responseType = $responseType; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Enable a grant type on the server. |
106
|
|
|
* |
107
|
|
|
* @param GrantTypeInterface $grantType |
108
|
|
|
* @param null|\DateInterval $accessTokenTTL |
109
|
|
|
*/ |
110
|
|
|
public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null) |
111
|
|
|
{ |
112
|
|
|
if ($accessTokenTTL instanceof \DateInterval === false) { |
113
|
|
|
$accessTokenTTL = new \DateInterval('PT1H'); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$grantType->setAccessTokenRepository($this->accessTokenRepository); |
117
|
|
|
$grantType->setClientRepository($this->clientRepository); |
118
|
|
|
$grantType->setScopeRepository($this->scopeRepository); |
119
|
|
|
$grantType->setPrivateKey($this->privateKey); |
120
|
|
|
$grantType->setPublicKey($this->publicKey); |
121
|
|
|
$grantType->setEmitter($this->getEmitter()); |
122
|
|
|
|
123
|
|
|
$this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType; |
124
|
|
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Validate an authorization request |
129
|
|
|
* |
130
|
|
|
* @param ServerRequestInterface $request |
131
|
|
|
* |
132
|
|
|
* @throws OAuthServerException |
133
|
|
|
* |
134
|
|
|
* @return AuthorizationRequest |
135
|
|
|
*/ |
136
|
|
|
public function validateAuthorizationRequest(ServerRequestInterface $request) |
137
|
|
|
{ |
138
|
|
|
foreach ($this->enabledGrantTypes as $grantType) { |
139
|
|
|
if ($grantType->canRespondToAuthorizationRequest($request)) { |
140
|
|
|
return $grantType->validateAuthorizationRequest($request); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
throw OAuthServerException::unsupportedGrantType(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Complete an authorization request |
149
|
|
|
* |
150
|
|
|
* @param AuthorizationRequest $authRequest |
151
|
|
|
* @param ResponseInterface $response |
152
|
|
|
* |
153
|
|
|
* @return ResponseInterface |
154
|
|
|
*/ |
155
|
|
|
public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response) |
156
|
|
|
{ |
157
|
|
|
return $this->enabledGrantTypes[$authRequest->getGrantTypeId()] |
158
|
|
|
->completeAuthorizationRequest($authRequest) |
159
|
|
|
->generateHttpResponse($response); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return an access token response. |
164
|
|
|
* |
165
|
|
|
* @param ServerRequestInterface $request |
166
|
|
|
* @param ResponseInterface $response |
167
|
|
|
* |
168
|
|
|
* @throws OAuthServerException |
169
|
|
|
* |
170
|
|
|
* @return ResponseInterface |
171
|
|
|
*/ |
172
|
|
|
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response) |
173
|
|
|
{ |
174
|
|
|
foreach ($this->enabledGrantTypes as $grantType) { |
175
|
|
|
if ($grantType->canRespondToAccessTokenRequest($request)) { |
176
|
|
|
$tokenResponse = $grantType->respondToAccessTokenRequest( |
177
|
|
|
$request, |
178
|
|
|
$this->getResponseType(), |
179
|
|
|
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
if ($tokenResponse instanceof ResponseTypeInterface) { |
183
|
|
|
return $tokenResponse->generateHttpResponse($response); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
throw OAuthServerException::unsupportedGrantType(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the token type that grants will return in the HTTP response. |
193
|
|
|
* |
194
|
|
|
* @return ResponseTypeInterface |
195
|
|
|
*/ |
196
|
|
|
protected function getResponseType() |
197
|
|
|
{ |
198
|
|
|
if ($this->responseType instanceof ResponseTypeInterface === false) { |
199
|
|
|
$this->responseType = new BearerTokenResponse(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->responseType->setPrivateKey($this->privateKey); |
203
|
|
|
|
204
|
|
|
return $this->responseType; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|