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