Debian Codegraph @

lib/3rdParty/composer/facile-it/php-openid-client/src/Authorization/AuthRequest.php

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