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