| 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 ParagonIE\ConstantTime\Base64UrlSafe; |
| 10 |
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
| 11 |
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
| 12 |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 13 |
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
| 14 |
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
| 15 |
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 16 |
use Symfony\Component\Uid\Uuid; |
| 17 |
use Webauthn\Exception\InvalidDataException; |
| 18 |
use Webauthn\PublicKeyCredentialSource; |
| 19 |
use Webauthn\TrustPath\TrustPath; |
| 20 |
use Webauthn\Util\Base64; |
| 21 |
|
| 22 |
final class PublicKeyCredentialSourceDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface |
| 23 |
{ |
| 24 |
use NormalizerAwareTrait; |
| 25 |
use DenormalizerAwareTrait; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 31 |
{ |
| 32 |
|
| 33 |
$keys = ['publicKeyCredentialId', 'credentialPublicKey', 'userHandle']; |
| 34 |
foreach ($keys as $key) { |
| 35 |
array_key_exists($key, $data) || throw InvalidDataException::create($data, 'Missing ' . $key); |
| 36 |
$data[$key] = Base64::decode($data[$key]); |
| 37 |
} |
| 38 |
|
| 39 |
return PublicKeyCredentialSource::create( |
| 40 |
$data['publicKeyCredentialId'], |
| 41 |
$data['type'], |
| 42 |
$data['transports'], |
| 43 |
$data['attestationType'], |
| 44 |
$this->denormalizer->denormalize($data['trustPath'], TrustPath::class, $format, $context), |
| 45 |
Uuid::fromString($data['aaguid']), |
| 46 |
$data['credentialPublicKey'], |
| 47 |
$data['userHandle'], |
| 48 |
$data['counter'], |
| 49 |
$data['otherUI'] ?? null, |
| 50 |
$data['backupEligible'] ?? null, |
| 51 |
$data['backupStatus'] ?? null, |
| 52 |
$data['uvInitialized'] ?? null, |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
public function supportsDenormalization( |
| 57 |
mixed $data, |
| 58 |
string $type, |
| 59 |
?string $format = null, |
| 60 |
array $context = [] |
| 61 |
): bool { |
| 62 |
return $type === PublicKeyCredentialSource::class; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function getSupportedTypes(?string $format): array |
| 69 |
{ |
| 70 |
return [ |
| 71 |
PublicKeyCredentialSource::class => true, |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function normalize(mixed $data, ?string $format = null, array $context = []): array |
| 79 |
{ |
| 80 |
assert($data instanceof PublicKeyCredentialSource); |
| 81 |
$result = [ |
| 82 |
'publicKeyCredentialId' => Base64UrlSafe::encodeUnpadded($data->publicKeyCredentialId), |
| 83 |
'type' => $data->type, |
| 84 |
'transports' => $data->transports, |
| 85 |
'attestationType' => $data->attestationType, |
| 86 |
'trustPath' => $this->normalizer->normalize($data->trustPath, $format, $context), |
| 87 |
'aaguid' => $this->normalizer->normalize($data->aaguid, $format, $context), |
| 88 |
'credentialPublicKey' => Base64UrlSafe::encodeUnpadded($data->credentialPublicKey), |
| 89 |
'userHandle' => Base64UrlSafe::encodeUnpadded($data->userHandle), |
| 90 |
'counter' => $data->counter, |
| 91 |
'otherUI' => $data->otherUI, |
| 92 |
'backupEligible' => $data->backupEligible, |
| 93 |
'backupStatus' => $data->backupStatus, |
| 94 |
'uvInitialized' => $data->uvInitialized, |
| 95 |
]; |
| 96 |
|
| 97 |
return array_filter($result, static fn ($value): bool => $value !== null); |
| 98 |
} |
| 99 |
|
| 100 |
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 101 |
{ |
| 102 |
return $data instanceof PublicKeyCredentialSource; |
| 103 |
} |
| 104 |
} |