| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\Denormalizer; |
| 6 |
|
| 7 |
use RuntimeException; |
| 8 |
use function sprintf; |
| 9 |
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; |
| 10 |
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; |
| 11 |
use Symfony\Component\PropertyInfo\PropertyInfoExtractor; |
| 12 |
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 13 |
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
| 14 |
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; |
| 15 |
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
| 16 |
use Symfony\Component\Serializer\Normalizer\UidNormalizer; |
| 17 |
use Symfony\Component\Serializer\Serializer; |
| 18 |
use Symfony\Component\Serializer\SerializerInterface; |
| 19 |
use Webauthn\AttestationStatement\AttestationStatementSupportManager; |
| 20 |
|
| 21 |
final readonly class WebauthnSerializerFactory |
| 22 |
{ |
| 23 |
private const PACKAGE_SYMFONY_PROPERTY_INFO = 'symfony/property-info'; |
| 24 |
|
| 25 |
private const PACKAGE_SYMFONY_SERIALIZER = 'symfony/serializer'; |
| 26 |
|
| 27 |
private const PACKAGE_PHPDOCUMENTOR_REFLECTION_DOCBLOCK = 'phpdocumentor/reflection-docblock'; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
private AttestationStatementSupportManager $attestationStatementSupportManager |
| 31 |
) { |
| 32 |
} |
| 33 |
|
| 34 |
public function create(): SerializerInterface |
| 35 |
{ |
| 36 |
foreach (self::getRequiredSerializerClasses() as $class => $package) { |
| 37 |
if (! class_exists($class)) { |
| 38 |
throw new RuntimeException(sprintf( |
| 39 |
'The class "%s" is required. Please install the package "%s" to use this feature.', |
| 40 |
$class, |
| 41 |
$package |
| 42 |
)); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
$denormalizers = [ |
| 47 |
new ExtensionDescriptorDenormalizer(), |
| 48 |
new VerificationMethodANDCombinationsDenormalizer(), |
| 49 |
new AuthenticationExtensionNormalizer(), |
| 50 |
new PublicKeyCredentialDescriptorNormalizer(), |
| 51 |
new AttestedCredentialDataNormalizer(), |
| 52 |
new UrlNormalizer(), |
| 53 |
new AttestationObjectDenormalizer(), |
| 54 |
new AttestationStatementDenormalizer($this->attestationStatementSupportManager), |
| 55 |
new AuthenticationExtensionsDenormalizer(), |
| 56 |
new AuthenticatorAssertionResponseDenormalizer(), |
| 57 |
new AuthenticatorAttestationResponseDenormalizer(), |
| 58 |
new AuthenticatorDataDenormalizer(), |
| 59 |
new AuthenticatorResponseDenormalizer(), |
| 60 |
new CollectedClientDataDenormalizer(), |
| 61 |
new PublicKeyCredentialDenormalizer(), |
| 62 |
new PublicKeyCredentialOptionsDenormalizer(), |
| 63 |
new CredentialRecordDenormalizer(), |
| 64 |
new PublicKeyCredentialSourceDenormalizer(), |
| 65 |
new PublicKeyCredentialRpEntityDenormalizer(), |
| 66 |
new PublicKeyCredentialUserEntityDenormalizer(), |
| 67 |
new SignalAllAcceptedCredentialsDenormalizer(), |
| 68 |
new SignalCurrentUserDetailsDenormalizer(), |
| 69 |
new SignalUnknownCredentialDenormalizer(), |
| 70 |
new TrustPathDenormalizer(), |
| 71 |
new UidNormalizer(), |
| 72 |
new ArrayDenormalizer(), |
| 73 |
new ObjectNormalizer( |
| 74 |
propertyTypeExtractor: new PropertyInfoExtractor(typeExtractors: [ |
| 75 |
new PhpDocExtractor(), |
| 76 |
new ReflectionExtractor(), |
| 77 |
]) |
| 78 |
), |
| 79 |
]; |
| 80 |
|
| 81 |
return new Serializer($denormalizers, [new JsonEncoder()]); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private static function getRequiredSerializerClasses(): array |
| 88 |
{ |
| 89 |
return [ |
| 90 |
UidNormalizer::class => self::PACKAGE_SYMFONY_SERIALIZER, |
| 91 |
ArrayDenormalizer::class => self::PACKAGE_SYMFONY_SERIALIZER, |
| 92 |
AbstractNormalizer::class => self::PACKAGE_SYMFONY_SERIALIZER, |
| 93 |
PropertyInfoExtractor::class => self::PACKAGE_SYMFONY_PROPERTY_INFO, |
| 94 |
PhpDocExtractor::class => self::PACKAGE_PHPDOCUMENTOR_REFLECTION_DOCBLOCK, |
| 95 |
ReflectionExtractor::class => self::PACKAGE_SYMFONY_PROPERTY_INFO, |
| 96 |
JsonEncoder::class => self::PACKAGE_SYMFONY_SERIALIZER, |
| 97 |
Serializer::class => self::PACKAGE_SYMFONY_SERIALIZER, |
| 98 |
]; |
| 99 |
} |
| 100 |
} |