1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 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\AuthorizationEndpoint; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseMode; |
17
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
18
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
19
|
|
|
use OAuth2Framework\Component\Core\ResourceServer\ResourceServer; |
20
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccount; |
21
|
|
|
use OAuth2Framework\Component\Core\TokenType\TokenType; |
22
|
|
|
|
23
|
|
|
class Authorization |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $authorized; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Client |
32
|
|
|
*/ |
33
|
|
|
private $client; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var UserAccount|null |
37
|
|
|
*/ |
38
|
|
|
private $userAccount = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var DataBag |
42
|
|
|
*/ |
43
|
|
|
private $metadata; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var null|bool |
47
|
|
|
*/ |
48
|
|
|
private $userAccountFullyAuthenticated = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $data = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var null|array |
57
|
|
|
*/ |
58
|
|
|
private $claims = null; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var TokenType|null |
62
|
|
|
*/ |
63
|
|
|
private $tokenType = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var ResponseType] |
67
|
|
|
*/ |
68
|
|
|
private $responseType = null; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var ResponseMode|null |
72
|
|
|
*/ |
73
|
|
|
private $responseMode = null; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
private $queryParameters = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string|null |
82
|
|
|
*/ |
83
|
|
|
private $redirectUri = null; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var array |
87
|
|
|
*/ |
88
|
|
|
private $consentScreenOptions = []; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var array |
92
|
|
|
*/ |
93
|
|
|
private $responseParameters = []; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var array |
97
|
|
|
*/ |
98
|
|
|
private $responseHeaders = []; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var null|ResourceServer |
102
|
|
|
*/ |
103
|
|
|
private $resourceServer = null; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Authorization constructor. |
107
|
|
|
* |
108
|
|
|
* @param Client $client |
109
|
|
|
* @param array $queryParameters |
110
|
|
|
*/ |
111
|
|
|
private function __construct(Client $client, array $queryParameters) |
112
|
|
|
{ |
113
|
|
|
$this->client = $client; |
114
|
|
|
$this->queryParameters = $queryParameters; |
115
|
|
|
$this->metadata = DataBag::create([]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Client $client |
120
|
|
|
* @param array $queryParameters |
121
|
|
|
* |
122
|
|
|
* @return Authorization |
|
|
|
|
123
|
|
|
*/ |
124
|
|
|
public static function create(Client $client, array $queryParameters): self |
125
|
|
|
{ |
126
|
|
|
return new self($client, $queryParameters); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getQueryParams(): array |
133
|
|
|
{ |
134
|
|
|
return $this->queryParameters; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $param |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function hasQueryParam(string $param): bool |
143
|
|
|
{ |
144
|
|
|
return array_key_exists($param, $this->queryParameters); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $param |
149
|
|
|
* |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
public function getQueryParam(string $param) |
153
|
|
|
{ |
154
|
|
|
if (!$this->hasQueryParam($param)) { |
155
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid parameter "%s".', $param)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->queryParameters[$param]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return Client |
163
|
|
|
*/ |
164
|
|
|
public function getClient(): Client |
165
|
|
|
{ |
166
|
|
|
return $this->client; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param TokenType $tokenType |
171
|
|
|
* |
172
|
|
|
* @return Authorization |
173
|
|
|
*/ |
174
|
|
|
public function withTokenType(TokenType $tokenType): self |
175
|
|
|
{ |
176
|
|
|
$this->tokenType = $tokenType; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return null|TokenType |
183
|
|
|
*/ |
184
|
|
|
public function getTokenType(): ? TokenType |
185
|
|
|
{ |
186
|
|
|
return $this->tokenType; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param array $claims |
191
|
|
|
* |
192
|
|
|
* @return Authorization |
193
|
|
|
*/ |
194
|
|
|
public function withClaims(array $claims): self |
195
|
|
|
{ |
196
|
|
|
$this->claims = $claims; |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return null|array |
203
|
|
|
*/ |
204
|
|
|
public function getClaims(): ?array |
205
|
|
|
{ |
206
|
|
|
return $this->claims; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param ResponseType $responseType |
211
|
|
|
* |
212
|
|
|
* @return Authorization |
213
|
|
|
*/ |
214
|
|
|
public function withResponseType(ResponseType $responseType): self |
215
|
|
|
{ |
216
|
|
|
$this->responseType = $responseType; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return ResponseType |
223
|
|
|
*/ |
224
|
|
|
public function getResponseType(): ResponseType |
225
|
|
|
{ |
226
|
|
|
return $this->responseType; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param ResponseMode $responseMode |
231
|
|
|
* |
232
|
|
|
* @return Authorization |
233
|
|
|
*/ |
234
|
|
|
public function withResponseMode(ResponseMode $responseMode): self |
235
|
|
|
{ |
236
|
|
|
$this->responseMode = $responseMode; |
237
|
|
|
|
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return null|ResponseMode |
243
|
|
|
*/ |
244
|
|
|
public function getResponseMode(): ? ResponseMode |
245
|
|
|
{ |
246
|
|
|
return $this->responseMode; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param string $redirectUri |
251
|
|
|
* |
252
|
|
|
* @return Authorization |
253
|
|
|
*/ |
254
|
|
|
public function withRedirectUri(string $redirectUri): self |
255
|
|
|
{ |
256
|
|
|
$this->redirectUri = $redirectUri; |
257
|
|
|
|
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return null|string |
263
|
|
|
*/ |
264
|
|
|
public function getRedirectUri(): ? string |
265
|
|
|
{ |
266
|
|
|
return $this->redirectUri; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param UserAccount $userAccount |
271
|
|
|
* @param bool $isFullyAuthenticated |
272
|
|
|
* |
273
|
|
|
* @return Authorization |
274
|
|
|
*/ |
275
|
|
|
public function withUserAccount(UserAccount $userAccount, bool $isFullyAuthenticated): self |
276
|
|
|
{ |
277
|
|
|
$this->userAccount = $userAccount; |
278
|
|
|
$this->userAccountFullyAuthenticated = $isFullyAuthenticated; |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return null|UserAccount |
285
|
|
|
*/ |
286
|
|
|
public function getUserAccount(): ? UserAccount |
287
|
|
|
{ |
288
|
|
|
return $this->userAccount; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param string $responseParameter |
293
|
|
|
* @param mixed $value |
294
|
|
|
* |
295
|
|
|
* @return Authorization |
296
|
|
|
*/ |
297
|
|
|
public function withResponseParameter(string $responseParameter, $value): self |
298
|
|
|
{ |
299
|
|
|
$this->responseParameters[$responseParameter] = $value; |
300
|
|
|
|
301
|
|
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return array |
306
|
|
|
*/ |
307
|
|
|
public function getResponseParameters(): array |
308
|
|
|
{ |
309
|
|
|
return $this->responseParameters; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param string $param |
314
|
|
|
* |
315
|
|
|
* @return mixed |
316
|
|
|
*/ |
317
|
|
|
public function getResponseParameter(string $param) |
318
|
|
|
{ |
319
|
|
|
if (!$this->hasResponseParameter($param)) { |
320
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid response parameter "%s".', $param)); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $this->getResponseParameters()[$param]; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param string $param |
328
|
|
|
* |
329
|
|
|
* @return bool |
330
|
|
|
*/ |
331
|
|
|
public function hasResponseParameter(string $param): bool |
332
|
|
|
{ |
333
|
|
|
return array_key_exists($param, $this->getResponseParameters()); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param string $responseHeader |
338
|
|
|
* @param mixed $value |
339
|
|
|
* |
340
|
|
|
* @return Authorization |
341
|
|
|
*/ |
342
|
|
|
public function withResponseHeader(string $responseHeader, $value): self |
343
|
|
|
{ |
344
|
|
|
$this->responseHeaders[$responseHeader] = $value; |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @return array |
351
|
|
|
*/ |
352
|
|
|
public function getResponseHeaders(): array |
353
|
|
|
{ |
354
|
|
|
return $this->responseHeaders; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @return bool|null |
359
|
|
|
*/ |
360
|
|
|
public function isUserAccountFullyAuthenticated(): ? bool |
361
|
|
|
{ |
362
|
|
|
return $this->userAccountFullyAuthenticated; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @return string[] |
367
|
|
|
*/ |
368
|
|
|
public function getPrompt(): array |
369
|
|
|
{ |
370
|
|
|
if (!$this->hasQueryParam('prompt')) { |
371
|
|
|
return []; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
return explode(' ', $this->getQueryParam('prompt')); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @return bool |
379
|
|
|
*/ |
380
|
|
|
public function hasUiLocales(): bool |
381
|
|
|
{ |
382
|
|
|
return $this->hasQueryParam('ui_locales'); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @return string[] |
387
|
|
|
*/ |
388
|
|
|
public function getUiLocales(): array |
389
|
|
|
{ |
390
|
|
|
return $this->hasQueryParam('ui_locales') ? explode(' ', $this->getQueryParam('ui_locales')) : []; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @param string $prompt |
395
|
|
|
* |
396
|
|
|
* @return bool |
397
|
|
|
*/ |
398
|
|
|
public function hasPrompt(string $prompt): bool |
399
|
|
|
{ |
400
|
|
|
return in_array($prompt, $this->getPrompt()); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @return bool |
405
|
|
|
*/ |
406
|
|
|
public function isAuthorized(): bool |
407
|
|
|
{ |
408
|
|
|
return $this->authorized; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @return Authorization |
413
|
|
|
*/ |
414
|
|
|
public function allow(): self |
415
|
|
|
{ |
416
|
|
|
$this->authorized = true; |
417
|
|
|
|
418
|
|
|
return $this; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @return Authorization |
423
|
|
|
*/ |
424
|
|
|
public function deny(): self |
425
|
|
|
{ |
426
|
|
|
$this->authorized = false; |
427
|
|
|
|
428
|
|
|
return $this; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param string $key |
433
|
|
|
* |
434
|
|
|
* @return bool |
435
|
|
|
*/ |
436
|
|
|
public function hasData(string $key): bool |
437
|
|
|
{ |
438
|
|
|
return array_key_exists($key, $this->data); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @param string $key |
443
|
|
|
* |
444
|
|
|
* @return mixed |
445
|
|
|
*/ |
446
|
|
|
public function getData(string $key) |
447
|
|
|
{ |
448
|
|
|
if (!$this->hasData($key)) { |
449
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid data "%s".', $key)); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
return $this->data[$key]; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* @param string $key |
457
|
|
|
* @param mixed $data |
458
|
|
|
* |
459
|
|
|
* @return Authorization |
460
|
|
|
*/ |
461
|
|
|
public function withData(string $key, $data): self |
462
|
|
|
{ |
463
|
|
|
$this->data[$key] = $data; |
464
|
|
|
|
465
|
|
|
return $this; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @return null|ResourceServer |
470
|
|
|
*/ |
471
|
|
|
public function getResourceServer(): ? ResourceServer |
472
|
|
|
{ |
473
|
|
|
return $this->resourceServer; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* @param ResourceServer $resourceServer |
478
|
|
|
* |
479
|
|
|
* @return Authorization |
480
|
|
|
*/ |
481
|
|
|
public function withResourceServer(ResourceServer $resourceServer): self |
482
|
|
|
{ |
483
|
|
|
$this->resourceServer = $resourceServer; |
484
|
|
|
|
485
|
|
|
return $this; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* @param string $option |
490
|
|
|
* @param mixed $value |
491
|
|
|
* |
492
|
|
|
* @return Authorization |
493
|
|
|
*/ |
494
|
|
|
public function withConsentScreenOption(string $option, $value): self |
495
|
|
|
{ |
496
|
|
|
$this->consentScreenOptions[$option] = $value; |
497
|
|
|
|
498
|
|
|
return $this; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @param string $option |
503
|
|
|
* |
504
|
|
|
* @return Authorization |
505
|
|
|
*/ |
506
|
|
|
public function withoutConsentScreenOption(string $option): self |
507
|
|
|
{ |
508
|
|
|
if (!array_key_exists($option, $this->consentScreenOptions)) { |
509
|
|
|
return $this; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
unset($this->consentScreenOptions[$option]); |
513
|
|
|
|
514
|
|
|
return $this; |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
public function hasScope(): bool |
518
|
|
|
{ |
519
|
|
|
return $this->hasQueryParam('scope'); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* @return string |
524
|
|
|
*/ |
525
|
|
|
public function getScope(): string |
526
|
|
|
{ |
527
|
|
|
return $this->getQueryParam('scope'); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* @param string $key |
532
|
|
|
* @param mixed|null $data |
533
|
|
|
* |
534
|
|
|
* @return Authorization |
535
|
|
|
*/ |
536
|
|
|
public function withMetadata(string $key, $data): self |
537
|
|
|
{ |
538
|
|
|
$this->metadata = $this->metadata->with($key, $data); |
539
|
|
|
|
540
|
|
|
return $this; |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* @param string $key |
545
|
|
|
* |
546
|
|
|
* @return bool |
547
|
|
|
*/ |
548
|
|
|
public function hasMetadata(string $key): bool |
549
|
|
|
{ |
550
|
|
|
return $this->metadata->has($key); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* @param string $key |
555
|
|
|
* |
556
|
|
|
* @return bool |
557
|
|
|
*/ |
558
|
|
|
public function getMetadata(string $key): bool |
559
|
|
|
{ |
560
|
|
|
if (!$this->hasMetadata($key)) { |
561
|
|
|
throw new \InvalidArgumentException(sprintf('The metadata "%s" does not exist.', $key)); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
return $this->metadata->get($key); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
public function getMetadatas(): DataBag |
568
|
|
|
{ |
569
|
|
|
return $this->metadata; |
570
|
|
|
} |
571
|
|
|
} |
572
|
|
|
|