Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/Denormalizer/PublicKeyCredentialOptionsDenormalizer.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn\Denormalizer;
6
7 use function array_key_exists;
8 use function assert;
9 use function count;
10 use function in_array;
11 use ParagonIE\ConstantTime\Base64UrlSafe;
12 use Symfony\Component\Serializer\Exception\BadMethodCallException;
13 use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
14 use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
15 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
16 use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
17 use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
18 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19 use Webauthn\AuthenticationExtensions\AuthenticationExtensions;
20 use Webauthn\AuthenticatorSelectionCriteria;
21 use Webauthn\PublicKeyCredentialCreationOptions;
22 use Webauthn\PublicKeyCredentialDescriptor;
23 use Webauthn\PublicKeyCredentialParameters;
24 use Webauthn\PublicKeyCredentialRequestOptions;
25 use Webauthn\PublicKeyCredentialRpEntity;
26 use Webauthn\PublicKeyCredentialUserEntity;
27
28 final class PublicKeyCredentialOptionsDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface
29 {
30 use DenormalizerAwareTrait;
31 use NormalizerAwareTrait;
32
33 /**
34 * @throws BadMethodCallException
35 */
36 public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
37 {
38 /** @var array{challenge?: string, rp?: array<string, mixed>, user?: array<string, mixed>, pubKeyCredParams?: list<array<string, mixed>>, authenticatorSelection?: array<string, mixed>, attestation?: string, excludeCredentials?: list<array{id: string, type?: string, transports?: list<string>}>, allowCredentials?: list<array{id: string, type?: string, transports?: list<string>}>, timeout?: int<1, max>, extensions?: array<string, mixed>, hints?: list<string>, rpId?: string, userVerification?: string} $data */
39 if (array_key_exists('challenge', $data)) {
40 $data['challenge'] = Base64UrlSafe::decodeNoPadding($data['challenge']);
41 }
42
43 foreach (['allowCredentials', 'excludeCredentials'] as $key) {
44 if (array_key_exists($key, $data)) {
45 /** @var list<array{id: string, type?: string, transports?: list<string>}> $credentials */
46 $credentials = $data[$key] ?? [];
47 foreach ($credentials as $item => $allowCredential) {
48 $data[$key][$item]['id'] = Base64UrlSafe::decodeNoPadding($allowCredential['id']);
49 }
50 }
51 }
52 if ($type === PublicKeyCredentialCreationOptions::class) {
53 /** @var PublicKeyCredentialRpEntity $rp */
54 $rp = ! isset($data['rp']) ? PublicKeyCredentialRpEntity::create() : $this->denormalizer->denormalize(
55 $data['rp'],
56 PublicKeyCredentialRpEntity::class,
57 $format,
58 $context
59 );
60 /** @var PublicKeyCredentialUserEntity $user */
61 $user = $this->denormalizer->denormalize(
62 $data['user'] ?? [],
63 PublicKeyCredentialUserEntity::class,
64 $format,
65 $context
66 );
67 /** @var list<PublicKeyCredentialParameters> $pubKeyCredParams */
68 $pubKeyCredParams = ! isset($data['pubKeyCredParams']) ? [] : $this->denormalizer->denormalize(
69 $data['pubKeyCredParams'],
70 PublicKeyCredentialParameters::class . '[]',
71 $format,
72 $context
73 );
74 /** @var ?AuthenticatorSelectionCriteria $authenticatorSelection */
75 $authenticatorSelection = ! isset($data['authenticatorSelection']) ? null : $this->denormalizer->denormalize(
76 $data['authenticatorSelection'],
77 AuthenticatorSelectionCriteria::class,
78 $format,
79 $context
80 );
81 /** @var list<PublicKeyCredentialDescriptor> $excludeCredentials */
82 $excludeCredentials = ! isset($data['excludeCredentials']) ? [] : $this->denormalizer->denormalize(
83 $data['excludeCredentials'],
84 PublicKeyCredentialDescriptor::class . '[]',
85 $format,
86 $context
87 );
88 /** @var ?AuthenticationExtensions $extensions */
89 $extensions = ! isset($data['extensions']) ? null : $this->denormalizer->denormalize(
90 $data['extensions'],
91 AuthenticationExtensions::class,
92 $format,
93 $context
94 );
95
96 return PublicKeyCredentialCreationOptions::create(
97 $rp,
98 $user,
99 $data['challenge'] ?? '',
100 $pubKeyCredParams,
101 $authenticatorSelection,
102 $data['attestation'] ?? null,
103 $excludeCredentials,
104 $data['timeout'] ?? null,
105 $extensions,
106 $data['hints'] ?? [],
107 );
108 }
109 if ($type === PublicKeyCredentialRequestOptions::class) {
110 /** @var list<PublicKeyCredentialDescriptor> $allowCredentials */
111 $allowCredentials = ! isset($data['allowCredentials']) ? [] : $this->denormalizer->denormalize(
112 $data['allowCredentials'],
113 PublicKeyCredentialDescriptor::class . '[]',
114 $format,
115 $context
116 );
117 /** @var ?AuthenticationExtensions $extensions */
118 $extensions = ! isset($data['extensions']) ? null : $this->denormalizer->denormalize(
119 $data['extensions'],
120 AuthenticationExtensions::class,
121 $format,
122 $context
123 );
124
125 return PublicKeyCredentialRequestOptions::create(
126 $data['challenge'] ?? '',
127 $data['rpId'] ?? null,
128 $allowCredentials,
129 $data['userVerification'] ?? null,
130 $data['timeout'] ?? null,
131 $extensions,
132 $data['hints'] ?? [],
133 );
134 }
135 throw new BadMethodCallException('Unsupported type');
136 }
137
138 public function supportsDenormalization(
139 mixed $data,
140 string $type,
141 ?string $format = null,
142 array $context = []
143 ): bool {
144 return in_array(
145 $type,
146 [PublicKeyCredentialCreationOptions::class, PublicKeyCredentialRequestOptions::class],
147 true
148 );
149 }
150
151 public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
152 {
153 return $data instanceof PublicKeyCredentialCreationOptions || $data instanceof PublicKeyCredentialRequestOptions;
154 }
155
156 /**
157 * @return array<class-string, bool>
158 */
159 public function getSupportedTypes(?string $format): array
160 {
161 return [
162 PublicKeyCredentialCreationOptions::class => true,
163 PublicKeyCredentialRequestOptions::class => true,
164 ];
165 }
166
167 /**
168 * @return array<string, mixed>
169 */
170 public function normalize(mixed $object, ?string $format = null, array $context = []): array
171 {
172 assert(
173 $object instanceof PublicKeyCredentialCreationOptions || $object instanceof PublicKeyCredentialRequestOptions
174 );
175 $json = [
176 'challenge' => Base64UrlSafe::encodeUnpadded($object->challenge),
177 'timeout' => $object->timeout,
178 'extensions' => $object->extensions->count() === 0 ? null : $this->normalizer->normalize(
179 $object->extensions,
180 $format,
181 $context
182 ),
183 'hints' => count($object->hints) === 0 ? null : $object->hints,
184 ];
185
186 if ($object instanceof PublicKeyCredentialCreationOptions) {
187 $json = [
188 ...$json,
189 'rp' => $this->normalizer->normalize($object->rp, $format, $context),
190 'user' => $this->normalizer->normalize($object->user, $format, $context),
191 'pubKeyCredParams' => $this->normalizer->normalize(
192 $object->pubKeyCredParams,
193 PublicKeyCredentialParameters::class . '[]',
194 $context
195 ),
196 'authenticatorSelection' => $object->authenticatorSelection === null ? null : $this->normalizer->normalize(
197 $object->authenticatorSelection,
198 $format,
199 $context
200 ),
201 'attestation' => $object->attestation,
202 'excludeCredentials' => $this->normalizer->normalize($object->excludeCredentials, $format, $context),
203 ];
204 }
205 if ($object instanceof PublicKeyCredentialRequestOptions) {
206 $json = [
207 ...$json,
208 'rpId' => $object->rpId,
209 'allowCredentials' => $this->normalizer->normalize($object->allowCredentials, $format, $context),
210 'userVerification' => $object->userVerification,
211 ];
212 }
213
214 return array_filter($json, static fn ($value): bool => $value !== null);
215 }
216 }