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