| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\Denormalizer; |
| 6 |
|
| 7 |
use CBOR\Decoder; |
| 8 |
use CBOR\Normalizable; |
| 9 |
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
| 10 |
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
| 11 |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 12 |
use Webauthn\AttestationStatement\AttestationObject; |
| 13 |
use Webauthn\AttestationStatement\AttestationStatement; |
| 14 |
use Webauthn\AuthenticatorData; |
| 15 |
use Webauthn\Exception\InvalidDataException; |
| 16 |
use Webauthn\StringStream; |
| 17 |
|
| 18 |
final class AttestationObjectDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface |
| 19 |
{ |
| 20 |
use DenormalizerAwareTrait; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 26 |
{ |
| 27 |
|
| 28 |
$stream = new StringStream($data); |
| 29 |
$parsed = Decoder::create()->decode($stream); |
| 30 |
|
| 31 |
$parsed instanceof Normalizable || throw InvalidDataException::create( |
| 32 |
$parsed, |
| 33 |
'Invalid attestation object. Unexpected object.' |
| 34 |
); |
| 35 |
|
| 36 |
$attestationObject = $parsed->normalize(); |
| 37 |
$stream->isEOF() || throw InvalidDataException::create( |
| 38 |
null, |
| 39 |
'Invalid attestation object. Presence of extra bytes.' |
| 40 |
); |
| 41 |
$stream->close(); |
| 42 |
$authData = $attestationObject['authData'] ?? throw InvalidDataException::create( |
| 43 |
$attestationObject, |
| 44 |
'Invalid attestation object. Missing "authData" field.' |
| 45 |
); |
| 46 |
|
| 47 |
return AttestationObject::create( |
| 48 |
$data, |
| 49 |
$this->denormalizer->denormalize($attestationObject, AttestationStatement::class, $format, $context), |
| 50 |
$this->denormalizer->denormalize($authData, AuthenticatorData::class, $format, $context), |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
public function supportsDenormalization( |
| 55 |
mixed $data, |
| 56 |
string $type, |
| 57 |
?string $format = null, |
| 58 |
array $context = [] |
| 59 |
): bool { |
| 60 |
return $type === AttestationObject::class; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function getSupportedTypes(?string $format): array |
| 67 |
{ |
| 68 |
return [ |
| 69 |
AttestationObject::class => true, |
| 70 |
]; |
| 71 |
} |
| 72 |
} |