| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\Denormalizer; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use ParagonIE\ConstantTime\Base64UrlSafe; |
| 9 |
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
| 10 |
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
| 11 |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 12 |
use Webauthn\AuthenticatorResponse; |
| 13 |
use Webauthn\Exception\InvalidDataException; |
| 14 |
use Webauthn\PublicKeyCredential; |
| 15 |
use Webauthn\Util\Base64; |
| 16 |
|
| 17 |
final class PublicKeyCredentialDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface |
| 18 |
{ |
| 19 |
use DenormalizerAwareTrait; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 25 |
{ |
| 26 |
|
| 27 |
if (! array_key_exists('id', $data)) { |
| 28 |
return $data; |
| 29 |
} |
| 30 |
$id = Base64UrlSafe::decodeNoPadding($data['id']); |
| 31 |
$rawId = Base64::decode($data['rawId']); |
| 32 |
hash_equals($id, $rawId) || throw InvalidDataException::create($data, 'Invalid ID'); |
| 33 |
$data['rawId'] = $rawId; |
| 34 |
|
| 35 |
|
| 36 |
$response = $this->denormalizer->denormalize( |
| 37 |
$data['response'], |
| 38 |
AuthenticatorResponse::class, |
| 39 |
$format, |
| 40 |
$context |
| 41 |
); |
| 42 |
|
| 43 |
return PublicKeyCredential::create($data['type'], $data['rawId'], $response); |
| 44 |
} |
| 45 |
|
| 46 |
public function supportsDenormalization( |
| 47 |
mixed $data, |
| 48 |
string $type, |
| 49 |
?string $format = null, |
| 50 |
array $context = [] |
| 51 |
): bool { |
| 52 |
return $type === PublicKeyCredential::class; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function getSupportedTypes(?string $format): array |
| 59 |
{ |
| 60 |
return [ |
| 61 |
PublicKeyCredential::class => true, |
| 62 |
]; |
| 63 |
} |
| 64 |
} |