1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TMV\OpenIdClient\Authorization; |
6
|
|
|
|
7
|
|
|
use function array_diff; |
8
|
|
|
use function array_diff_key; |
9
|
|
|
use function array_flip; |
10
|
|
|
use function array_keys; |
11
|
|
|
use function array_merge; |
12
|
|
|
use function count; |
13
|
|
|
use function implode; |
14
|
|
|
use TMV\OpenIdClient\Exception\InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
final class AuthRequest implements AuthRequestInterface |
17
|
|
|
{ |
18
|
|
|
/** @var array<string, mixed> */ |
19
|
|
|
private $params; |
20
|
|
|
|
21
|
|
|
/** @var string[] */ |
22
|
|
|
private static $requiredKeys = [ |
23
|
|
|
'client_id', |
24
|
|
|
'redirect_uri', |
25
|
|
|
]; |
26
|
|
|
|
27
|
21 |
|
public function __construct( |
28
|
|
|
string $clientId, |
29
|
|
|
string $redirectUri, |
30
|
|
|
array $params = [] |
31
|
|
|
) { |
32
|
|
|
$defaults = [ |
33
|
21 |
|
'scope' => 'openid', |
34
|
|
|
'response_type' => 'code', |
35
|
|
|
'response_mode' => 'query', |
36
|
|
|
]; |
37
|
|
|
/** @var array<string, mixed> $merged */ |
38
|
21 |
|
$merged = array_merge($defaults, $params); |
39
|
|
|
|
40
|
21 |
|
$this->params = $merged; |
41
|
21 |
|
$this->params['client_id'] = $clientId; |
42
|
21 |
|
$this->params['redirect_uri'] = $redirectUri; |
43
|
21 |
|
} |
44
|
|
|
|
45
|
3 |
|
public static function fromParams(array $params): self |
46
|
|
|
{ |
47
|
3 |
|
$missingKeys = array_diff(self::$requiredKeys, array_keys($params)); |
48
|
3 |
|
if (0 !== count($missingKeys)) { |
49
|
|
|
throw new InvalidArgumentException(implode(', ', $missingKeys) . ' keys not provided'); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
return new self( |
53
|
3 |
|
$params['client_id'], |
54
|
3 |
|
$params['redirect_uri'], |
55
|
|
|
$params |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* OpenID Connect requests MUST contain the openid scope value. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
2 |
|
public function getScope(): string |
65
|
|
|
{ |
66
|
2 |
|
return $this->params['scope']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* OAuth 2.0 Response Type value that determines the authorization processing flow to be used, |
71
|
|
|
* including what parameters are returned from the endpoints used. When using the Authorization Code Flow, |
72
|
|
|
* this value is code. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
1 |
|
public function getResponseType(): string |
77
|
|
|
{ |
78
|
1 |
|
return $this->params['response_type']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* OAuth 2.0 Client Identifier valid at the Authorization Server. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
2 |
|
public function getClientId(): string |
87
|
|
|
{ |
88
|
2 |
|
return $this->params['client_id']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Redirection URI to which the response will be sent. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
2 |
|
public function getRedirectUri(): string |
97
|
|
|
{ |
98
|
2 |
|
return $this->params['redirect_uri']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Opaque value used to maintain state between the request and the callback. |
103
|
|
|
* |
104
|
|
|
* @return null|string |
105
|
|
|
*/ |
106
|
1 |
|
public function getState(): ?string |
107
|
|
|
{ |
108
|
1 |
|
return $this->params['state'] ?? null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Informs the Authorization Server of the mechanism to be used for returning parameters from |
113
|
|
|
* the Authorization Endpoint. |
114
|
|
|
* |
115
|
|
|
* @return string|null |
116
|
|
|
*/ |
117
|
1 |
|
public function getResponseMode(): ?string |
118
|
|
|
{ |
119
|
1 |
|
return $this->params['response_mode'] ?? null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* String value used to associate a Client session with an ID Token, and to mitigate replay attacks. |
124
|
|
|
* |
125
|
|
|
* @return null|string |
126
|
|
|
*/ |
127
|
1 |
|
public function getNonce(): ?string |
128
|
|
|
{ |
129
|
1 |
|
return $this->params['nonce'] ?? null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* ASCII string value that specifies how the Authorization Server displays the authentication and consent |
134
|
|
|
* user interface pages to the End-User. |
135
|
|
|
* |
136
|
|
|
* The defined values are: |
137
|
|
|
* - page |
138
|
|
|
* - popup |
139
|
|
|
* - touch |
140
|
|
|
* - wrap |
141
|
|
|
* |
142
|
|
|
* @return string|null |
143
|
|
|
*/ |
144
|
1 |
|
public function getDisplay(): ?string |
145
|
|
|
{ |
146
|
1 |
|
return $this->params['display'] ?? null; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Case sensitive list of ASCII string values that specifies whether the Authorization Server prompts |
151
|
|
|
* the End-User for reauthentication and consent. |
152
|
|
|
* |
153
|
|
|
* The defined values are: |
154
|
|
|
* - none |
155
|
|
|
* - login |
156
|
|
|
* - consent |
157
|
|
|
* - select_account |
158
|
|
|
* |
159
|
|
|
* @return null|string |
160
|
|
|
*/ |
161
|
1 |
|
public function getPrompt(): ?string |
162
|
|
|
{ |
163
|
1 |
|
return $this->params['prompt'] ?? null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Maximum Authentication Age. Specifies the allowable elapsed time in seconds since the last time the End-User |
168
|
|
|
* was actively authenticated by the OP. |
169
|
|
|
* |
170
|
|
|
* @return int|null |
171
|
|
|
*/ |
172
|
1 |
|
public function getMaxAge(): ?int |
173
|
|
|
{ |
174
|
1 |
|
return $this->params['max_age'] ?? null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* End-User's preferred languages and scripts for the user interface, represented as a space-separated list |
179
|
|
|
* of BCP47 [RFC5646] language tag values, ordered by preference. |
180
|
|
|
* |
181
|
|
|
* @return null|string |
182
|
|
|
*/ |
183
|
1 |
|
public function getUiLocales(): ?string |
184
|
|
|
{ |
185
|
1 |
|
return $this->params['ui_locales'] ?? null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* ID Token previously issued by the Authorization Server being passed as a hint about the End-User's current or |
190
|
|
|
* past authenticated session with the Client. |
191
|
|
|
* |
192
|
|
|
* @return string|null |
193
|
|
|
*/ |
194
|
1 |
|
public function getIdTokenHint(): ?string |
195
|
|
|
{ |
196
|
1 |
|
return $this->params['id_token_hint'] ?? null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Hint to the Authorization Server about the login identifier the End-User might use to log in (if necessary). |
201
|
|
|
* |
202
|
|
|
* @return string|null |
203
|
|
|
*/ |
204
|
1 |
|
public function getLoginHint(): ?string |
205
|
|
|
{ |
206
|
1 |
|
return $this->params['login_hint'] ?? null; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Requested Authentication Context Class Reference values. |
211
|
|
|
* |
212
|
|
|
* @return null|string |
213
|
|
|
*/ |
214
|
1 |
|
public function getAcrValues(): ?string |
215
|
|
|
{ |
216
|
1 |
|
return $this->params['acr_values'] ?? null; |
217
|
|
|
} |
218
|
|
|
|
219
|
2 |
|
public function getRequest(): ?string |
220
|
|
|
{ |
221
|
2 |
|
return $this->params['request'] ?? null; |
222
|
|
|
} |
223
|
|
|
|
224
|
1 |
|
public function getCodeChallenge(): ?string |
225
|
|
|
{ |
226
|
1 |
|
return $this->params['code_challenge'] ?? null; |
227
|
|
|
} |
228
|
|
|
|
229
|
1 |
|
public function getCodeChallengeMethod(): ?string |
230
|
|
|
{ |
231
|
1 |
|
return $this->params['code_challenge_method'] ?? null; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Add other params and return a new instance. |
236
|
|
|
* |
237
|
|
|
* @param array<string, mixed> $params |
238
|
|
|
* |
239
|
|
|
* @return AuthRequestInterface |
240
|
|
|
*/ |
241
|
1 |
|
public function withParams(array $params): AuthRequestInterface |
242
|
|
|
{ |
243
|
1 |
|
$instance = clone $this; |
244
|
1 |
|
$instance->params = array_merge($instance->params, $params); |
245
|
|
|
|
246
|
1 |
|
if (0 === count(array_diff_key($instance->params, array_flip(self::$requiredKeys)))) { |
247
|
|
|
throw new InvalidArgumentException(implode(', ', self::$requiredKeys) . ' should be provided'); |
248
|
|
|
} |
249
|
|
|
|
250
|
1 |
|
return $instance; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Create params ready to use. |
255
|
|
|
* |
256
|
|
|
* @return array<string, mixed> |
257
|
|
|
*/ |
258
|
2 |
|
public function createParams(): array |
259
|
|
|
{ |
260
|
2 |
|
return $this->params; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return array<string, mixed> |
265
|
|
|
*/ |
266
|
1 |
|
public function jsonSerialize(): array |
267
|
|
|
{ |
268
|
1 |
|
return $this->createParams(); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|