1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\Server\Tests\Stub; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Server\Model\AuthCode\AuthCode; |
17
|
|
|
use OAuth2Framework\Component\Server\Model\AuthCode\AuthCodeId; |
18
|
|
|
use OAuth2Framework\Component\Server\Model\AuthCode\AuthCodeRepositoryInterface; |
19
|
|
|
use OAuth2Framework\Component\Server\Model\Client\ClientId; |
20
|
|
|
use OAuth2Framework\Component\Server\Model\DataBag\DataBag; |
21
|
|
|
use OAuth2Framework\Component\Server\Model\Event\EventStoreInterface; |
22
|
|
|
use OAuth2Framework\Component\Server\Model\ResourceServer\ResourceServerId; |
23
|
|
|
use OAuth2Framework\Component\Server\Model\UserAccount\UserAccountId; |
24
|
|
|
use SimpleBus\Message\Recorder\RecordsMessages; |
25
|
|
|
|
26
|
|
|
final class AuthCodeRepository implements AuthCodeRepositoryInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RecordsMessages |
30
|
|
|
*/ |
31
|
|
|
private $eventRecorder; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var EventStoreInterface |
35
|
|
|
*/ |
36
|
|
|
private $eventStore; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $lifetime; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* AuthCodeRepository constructor. |
45
|
|
|
* |
46
|
|
|
* @param EventStoreInterface $eventStore |
47
|
|
|
* @param RecordsMessages $eventRecorder |
48
|
|
|
* @param string $lifetime |
49
|
|
|
*/ |
50
|
|
|
public function __construct(EventStoreInterface $eventStore, RecordsMessages $eventRecorder, string $lifetime) |
51
|
|
|
{ |
52
|
|
|
$this->eventStore = $eventStore; |
53
|
|
|
$this->eventRecorder = $eventRecorder; |
54
|
|
|
$this->lifetime = $lifetime; |
55
|
|
|
$this->createAndSaveAuthCode( |
56
|
|
|
AuthCodeId::create('VALID_AUTH_CODE'), |
57
|
|
|
ClientId::create('client1'), |
58
|
|
|
UserAccountId::create('john.1'), |
59
|
|
|
[], |
60
|
|
|
'https://www.example.com/callback', |
61
|
|
|
new \DateTimeImmutable('now +1 day'), |
62
|
|
|
DataBag::createFromArray([]), |
63
|
|
|
DataBag::createFromArray([]), |
64
|
|
|
['openid', 'email', 'phone', 'address'], |
65
|
|
|
false |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->createAndSaveAuthCode( |
69
|
|
|
AuthCodeId::create('EXPIRED_AUTH_CODE'), |
70
|
|
|
ClientId::create('client1'), |
71
|
|
|
UserAccountId::create('john.1'), |
72
|
|
|
[], |
73
|
|
|
'https://www.example.com/callback', |
74
|
|
|
new \DateTimeImmutable('now -1 day'), |
75
|
|
|
DataBag::createFromArray([]), |
76
|
|
|
DataBag::createFromArray([]), |
77
|
|
|
['openid', 'email', 'phone', 'address'], |
78
|
|
|
false |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$this->createRevokedAndSaveAuthCode( |
82
|
|
|
AuthCodeId::create('REVOKED_AUTH_CODE'), |
83
|
|
|
ClientId::create('client1'), |
84
|
|
|
UserAccountId::create('john.1'), |
85
|
|
|
[], |
86
|
|
|
'https://www.example.com/callback', |
87
|
|
|
new \DateTimeImmutable('now +1 day'), |
88
|
|
|
DataBag::createFromArray([]), |
89
|
|
|
DataBag::createFromArray([]), |
90
|
|
|
['openid', 'email', 'phone', 'address'], |
91
|
|
|
false |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->createUsedAndSaveAuthCode( |
95
|
|
|
AuthCodeId::create('USED_AUTH_CODE'), |
96
|
|
|
ClientId::create('client1'), |
97
|
|
|
UserAccountId::create('john.1'), |
98
|
|
|
[], |
99
|
|
|
'https://www.example.com/callback', |
100
|
|
|
new \DateTimeImmutable('now +1 day'), |
101
|
|
|
DataBag::createFromArray([]), |
102
|
|
|
DataBag::createFromArray([]), |
103
|
|
|
['openid', 'email', 'phone', 'address'], |
104
|
|
|
false |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$this->createAndSaveAuthCode( |
108
|
|
|
AuthCodeId::create('AUTH_CODE_WITH_CODE_VERIFIER_PLAIN'), |
109
|
|
|
ClientId::create('client1'), |
110
|
|
|
UserAccountId::create('john.1'), |
111
|
|
|
[ |
112
|
|
|
'code_challenge' => 'E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM', |
113
|
|
|
'code_challenge_method' => 'plain', |
114
|
|
|
], |
115
|
|
|
'https://www.example.com/callback', |
116
|
|
|
new \DateTimeImmutable('now +1 day'), |
117
|
|
|
DataBag::createFromArray([]), |
118
|
|
|
DataBag::createFromArray([]), |
119
|
|
|
['openid', 'email', 'phone', 'address'], |
120
|
|
|
false |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$this->createAndSaveAuthCode( |
124
|
|
|
AuthCodeId::create('AUTH_CODE_WITH_CODE_VERIFIER_S256'), |
125
|
|
|
ClientId::create('client1'), |
126
|
|
|
UserAccountId::create('john.1'), |
127
|
|
|
[ |
128
|
|
|
'code_challenge' => 'DSmbHrVIcI0EU05-BQxCe1bt-hXRNjejSEvdYbq_g4Q', |
129
|
|
|
'code_challenge_method' => 'S256', |
130
|
|
|
], |
131
|
|
|
'https://www.example.com/callback', |
132
|
|
|
new \DateTimeImmutable('now +1 day'), |
133
|
|
|
DataBag::createFromArray([]), |
134
|
|
|
DataBag::createFromArray([]), |
135
|
|
|
['openid', 'email', 'phone', 'address'], |
136
|
|
|
false |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function create(ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, DataBag $parameters, DataBag $metadatas, array $scopes, bool $withRefreshToken, ?ResourceServerId $resourceServerId, ?\DateTimeImmutable $expiresAt): AuthCode |
144
|
|
|
{ |
145
|
|
|
if (null === $expiresAt) { |
146
|
|
|
$expiresAt = new \DateTimeImmutable($this->lifetime); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$authCode = AuthCode::createEmpty(); |
150
|
|
|
$authCode = $authCode->create( |
151
|
|
|
AuthCodeId::create(bin2hex(random_bytes(50))), |
152
|
|
|
$clientId, |
153
|
|
|
$userAccountId, |
154
|
|
|
$queryParameters, |
155
|
|
|
$redirectUri, |
156
|
|
|
$expiresAt, |
157
|
|
|
$parameters, |
158
|
|
|
$metadatas, |
159
|
|
|
$scopes, |
160
|
|
|
$withRefreshToken, |
161
|
|
|
$resourceServerId |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
return $authCode; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param AuthCode $authCode |
169
|
|
|
*/ |
170
|
|
|
public function save(AuthCode $authCode) |
171
|
|
|
{ |
172
|
|
|
$events = $authCode->recordedMessages(); |
173
|
|
|
foreach ($events as $event) { |
174
|
|
|
$this->eventStore->save($event); |
175
|
|
|
$this->eventRecorder->record($event); |
176
|
|
|
} |
177
|
|
|
$authCode->eraseMessages(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function find(AuthCodeId $authCodeId): ?AuthCode |
184
|
|
|
{ |
185
|
|
|
$authCode = null; |
186
|
|
|
$events = $this->eventStore->getEvents($authCodeId); |
187
|
|
|
if (!empty($events)) { |
188
|
|
|
$authCode = AuthCode::createEmpty(); |
189
|
|
|
foreach ($events as $event) { |
190
|
|
|
$authCode = $authCode->apply($event); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $authCode; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param AuthCodeId $authCodeId |
199
|
|
|
* @param UserAccountId $userAccountId |
200
|
|
|
* @param ClientId $clientId |
201
|
|
|
* @param array $queryParameters |
202
|
|
|
* @param string $redirectUri |
203
|
|
|
* @param DataBag $parameters |
204
|
|
|
* @param DataBag $metadatas |
205
|
|
|
* @param array $scopes |
206
|
|
|
* @param \DateTimeImmutable|null $expiresAt |
207
|
|
|
* @param bool $withRefreshToken |
208
|
|
|
* @param ResourceServerId|null $resourceServerId |
209
|
|
|
*/ |
210
|
|
|
private function createAndSaveAuthCode(AuthCodeId $authCodeId, ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, \DateTimeImmutable $expiresAt = null, DataBag $parameters, DataBag $metadatas, array $scopes = [], bool $withRefreshToken = false, ResourceServerId $resourceServerId = null) |
211
|
|
|
{ |
212
|
|
|
$authCode = AuthCode::createEmpty(); |
213
|
|
|
$authCode = $authCode->create( |
214
|
|
|
$authCodeId, |
215
|
|
|
$clientId, |
216
|
|
|
$userAccountId, |
217
|
|
|
$queryParameters, |
218
|
|
|
$redirectUri, |
219
|
|
|
$expiresAt, |
220
|
|
|
$parameters, |
221
|
|
|
$metadatas, |
222
|
|
|
$scopes, |
223
|
|
|
$withRefreshToken, |
224
|
|
|
$resourceServerId |
225
|
|
|
); |
226
|
|
|
$events = $authCode->recordedMessages(); |
227
|
|
|
foreach ($events as $event) { |
228
|
|
|
$this->eventStore->save($event); |
229
|
|
|
} |
230
|
|
|
$authCode->eraseMessages(); |
231
|
|
|
$this->save($authCode); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param AuthCodeId $authCodeId |
236
|
|
|
* @param UserAccountId $userAccountId |
237
|
|
|
* @param ClientId $clientId |
238
|
|
|
* @param array $queryParameters |
239
|
|
|
* @param string $redirectUri |
240
|
|
|
* @param DataBag $parameters |
241
|
|
|
* @param DataBag $metadatas |
242
|
|
|
* @param array $scopes |
243
|
|
|
* @param \DateTimeImmutable|null $expiresAt |
244
|
|
|
* @param bool $withRefreshToken |
245
|
|
|
* @param ResourceServerId|null $resourceServerId |
246
|
|
|
*/ |
247
|
|
|
private function createRevokedAndSaveAuthCode(AuthCodeId $authCodeId, ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, \DateTimeImmutable $expiresAt = null, DataBag $parameters, DataBag $metadatas, array $scopes = [], bool $withRefreshToken = false, ResourceServerId $resourceServerId = null) |
248
|
|
|
{ |
249
|
|
|
$authCode = AuthCode::createEmpty(); |
250
|
|
|
$authCode = $authCode->create( |
251
|
|
|
$authCodeId, |
252
|
|
|
$clientId, |
253
|
|
|
$userAccountId, |
254
|
|
|
$queryParameters, |
255
|
|
|
$redirectUri, |
256
|
|
|
$expiresAt, |
257
|
|
|
$parameters, |
258
|
|
|
$metadatas, |
259
|
|
|
$scopes, |
260
|
|
|
$withRefreshToken, |
261
|
|
|
$resourceServerId |
262
|
|
|
); |
263
|
|
|
$authCode = $authCode->markAsRevoked(); |
264
|
|
|
$events = $authCode->recordedMessages(); |
265
|
|
|
foreach ($events as $event) { |
266
|
|
|
$this->eventStore->save($event); |
267
|
|
|
} |
268
|
|
|
$authCode->eraseMessages(); |
269
|
|
|
$this->save($authCode); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param AuthCodeId $authCodeId |
274
|
|
|
* @param UserAccountId $userAccountId |
275
|
|
|
* @param ClientId $clientId |
276
|
|
|
* @param array $queryParameters |
277
|
|
|
* @param string $redirectUri |
278
|
|
|
* @param DataBag $parameters |
279
|
|
|
* @param DataBag $metadatas |
280
|
|
|
* @param array $scopes |
281
|
|
|
* @param \DateTimeImmutable|null $expiresAt |
282
|
|
|
* @param bool $withRefreshToken |
283
|
|
|
* @param ResourceServerId|null $resourceServerId |
284
|
|
|
*/ |
285
|
|
|
private function createUsedAndSaveAuthCode(AuthCodeId $authCodeId, ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, \DateTimeImmutable $expiresAt = null, DataBag $parameters, DataBag $metadatas, array $scopes = [], bool $withRefreshToken = false, ResourceServerId $resourceServerId = null) |
286
|
|
|
{ |
287
|
|
|
$authCode = AuthCode::createEmpty(); |
288
|
|
|
$authCode = $authCode->create( |
289
|
|
|
$authCodeId, |
290
|
|
|
$clientId, |
291
|
|
|
$userAccountId, |
292
|
|
|
$queryParameters, |
293
|
|
|
$redirectUri, |
294
|
|
|
$expiresAt, |
295
|
|
|
$parameters, |
296
|
|
|
$metadatas, |
297
|
|
|
$scopes, |
298
|
|
|
$withRefreshToken, |
299
|
|
|
$resourceServerId |
300
|
|
|
); |
301
|
|
|
$authCode = $authCode->markAsUsed(); |
302
|
|
|
$events = $authCode->recordedMessages(); |
303
|
|
|
foreach ($events as $event) { |
304
|
|
|
$this->eventStore->save($event); |
305
|
|
|
} |
306
|
|
|
$authCode->eraseMessages(); |
307
|
|
|
$this->save($authCode); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|