Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SerializerTrait and deprecate certain methods #625

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,14 @@ parameters:
count: 1
path: src/symfony/src/DependencyInjection/WebauthnExtension.php

-
message: """
#^Fetching class constant class of deprecated class Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\PublicKeyCredentialDescriptorCollectionType\\:
since 4\\.9\\.0 and will be removed in 5\\.0\\.0\\.$#
"""
count: 1
path: src/symfony/src/DependencyInjection/WebauthnExtension.php

-
message: """
#^Fetching class constant class of deprecated interface Webauthn\\\\Bundle\\\\Repository\\\\PublicKeyCredentialUserEntityRepository\\:
Expand Down Expand Up @@ -1248,12 +1256,17 @@ parameters:
path: src/symfony/src/Doctrine/Type/AAGUIDDataType.php

-
message: "#^Cannot cast mixed to string\\.$#"
message: "#^Method Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\AttestedCredentialDataType\\:\\:convertToDatabaseValue\\(\\) should return string\\|null but returns mixed\\.$#"
count: 1
path: src/symfony/src/Doctrine/Type/AttestedCredentialDataType.php

-
message: "#^Parameter \\#1 \\$json of static method Webauthn\\\\AttestedCredentialData\\:\\:createFromArray\\(\\) expects array, mixed given\\.$#"
message: "#^Method Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\AttestedCredentialDataType\\:\\:convertToPHPValue\\(\\) should return Webauthn\\\\AttestedCredentialData\\|null but returns mixed\\.$#"
count: 1
path: src/symfony/src/Doctrine/Type/AttestedCredentialDataType.php

-
message: "#^Parameter \\#1 \\$data of method Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\AttestedCredentialDataType\\:\\:deserialize\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/symfony/src/Doctrine/Type/AttestedCredentialDataType.php

Expand Down Expand Up @@ -1298,17 +1311,32 @@ parameters:
path: src/symfony/src/Doctrine/Type/PublicKeyCredentialDescriptorCollection.php

-
message: "#^Parameter \\#1 \\$data of static method Webauthn\\\\PublicKeyCredentialDescriptor\\:\\:createFromString\\(\\) expects string, mixed given\\.$#"
message: "#^Method Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\PublicKeyCredentialDescriptorType\\:\\:convertToDatabaseValue\\(\\) should return string\\|null but returns mixed\\.$#"
count: 1
path: src/symfony/src/Doctrine/Type/PublicKeyCredentialDescriptorType.php

-
message: "#^Cannot cast mixed to string\\.$#"
message: "#^Method Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\PublicKeyCredentialDescriptorType\\:\\:convertToPHPValue\\(\\) should return Webauthn\\\\PublicKeyCredentialDescriptor\\|null but returns mixed\\.$#"
count: 1
path: src/symfony/src/Doctrine/Type/PublicKeyCredentialDescriptorType.php

-
message: "#^Parameter \\#1 \\$data of method Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\PublicKeyCredentialDescriptorType\\:\\:deserialize\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/symfony/src/Doctrine/Type/PublicKeyCredentialDescriptorType.php

-
message: "#^Method Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\TrustPathDataType\\:\\:convertToDatabaseValue\\(\\) should return string\\|null but returns mixed\\.$#"
count: 1
path: src/symfony/src/Doctrine/Type/TrustPathDataType.php

-
message: "#^Parameter \\#1 \\$data of static method Webauthn\\\\TrustPath\\\\TrustPathLoader\\:\\:loadTrustPath\\(\\) expects array, mixed given\\.$#"
message: "#^Method Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\TrustPathDataType\\:\\:convertToPHPValue\\(\\) should return Webauthn\\\\TrustPath\\\\TrustPath\\|null but returns mixed\\.$#"
count: 1
path: src/symfony/src/Doctrine/Type/TrustPathDataType.php

-
message: "#^Parameter \\#1 \\$data of method Webauthn\\\\Bundle\\\\Doctrine\\\\Type\\\\TrustPathDataType\\:\\:deserialize\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/symfony/src/Doctrine/Type/TrustPathDataType.php

Expand Down Expand Up @@ -3403,14 +3431,6 @@ parameters:
count: 1
path: src/webauthn/src/TrustPath/EcdaaKeyIdTrustPath.php

-
message: """
#^Instantiation of deprecated class Webauthn\\\\TrustPath\\\\EcdaaKeyIdTrustPath\\:
since 4\\.2\\.0 and will be removed in 5\\.0\\.0\\. The ECDAA Trust Anchor does no longer exist in Webauthn specification\\.$#
"""
count: 1
path: src/webauthn/src/TrustPath/TrustPathLoader.php

-
message: "#^Parameter \\#1 \\$certificates of static method Webauthn\\\\TrustPath\\\\CertificateTrustPath\\:\\:create\\(\\) expects array\\<string\\>, array given\\.$#"
count: 1
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<server name="SYMFONY_PHPUNIT_VERSION" value="10.1"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
<env name="APP_DEBUG" value="true"/>
<env name="DATABASE_URL" value="sqlite:///%kernel.project_dir%/var/data.db"/>
<server name="KERNEL_CLASS" value="Webauthn\Tests\Bundle\Functional\AppKernel"/>
<ini name="memory_limit" value="-1"/>
</php>
Expand Down
14 changes: 7 additions & 7 deletions src/symfony/src/Doctrine/Type/AttestedCredentialDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Webauthn\AttestedCredentialData;
use const JSON_THROW_ON_ERROR;

final class AttestedCredentialDataType extends Type
{
use SerializerTrait;

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null) {
if (! $value instanceof AttestedCredentialData) {
return $value;
}

return json_encode($value, JSON_THROW_ON_ERROR);
return $this->serialize($value);
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?AttestedCredentialData
{
if ($value === null || $value instanceof AttestedCredentialData) {
return $value;
}
$json = json_decode((string) $value, true, flags: JSON_THROW_ON_ERROR);

return AttestedCredentialData::createFromArray($json);
return $this->deserialize($value, AttestedCredentialData::class);
}

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getJsonTypeDeclarationSQL($fieldDeclaration);
return $platform->getJsonTypeDeclarationSQL($column);
}

public function getName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/**
* @implements IteratorAggregate<PublicKeyCredentialDescriptor>
* @internal
* @deprecated since 4.9.0 and will be removed in 5.0.0.
*/
final class PublicKeyCredentialDescriptorCollection implements JsonSerializable, Countable, IteratorAggregate
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use function is_string;
use const JSON_THROW_ON_ERROR;

/**
* @deprecated since 4.9.0 and will be removed in 5.0.0.
*/
final class PublicKeyCredentialDescriptorCollectionType extends Type
{
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
Expand Down Expand Up @@ -37,9 +40,9 @@ public function convertToPHPValue(
return PublicKeyCredentialDescriptorCollection::createFromString($value);
}

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getJsonTypeDeclarationSQL($fieldDeclaration);
return $platform->getJsonTypeDeclarationSQL($column);
}

public function getName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Webauthn\PublicKeyCredentialDescriptor;
use const JSON_THROW_ON_ERROR;

final class PublicKeyCredentialDescriptorType extends Type
{
use SerializerTrait;

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null) {
if (! $value instanceof PublicKeyCredentialDescriptor) {
return $value;
}

return json_encode($value, JSON_THROW_ON_ERROR);
return $this->serialize($value);
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?PublicKeyCredentialDescriptor
Expand All @@ -26,12 +27,12 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?PublicKe
return $value;
}

return PublicKeyCredentialDescriptor::createFromString($value);
return $this->deserialize($value, PublicKeyCredentialDescriptor::class);
}

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getJsonTypeDeclarationSQL($fieldDeclaration);
return $platform->getJsonTypeDeclarationSQL($column);
}

public function getName(): string
Expand Down
32 changes: 32 additions & 0 deletions src/symfony/src/Doctrine/Type/SerializerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Webauthn\Bundle\Doctrine\Type;

use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Webauthn\AttestationStatement\AttestationStatementSupportManager;
use Webauthn\Denormalizer\WebauthnSerializerFactory;
use const JSON_THROW_ON_ERROR;

trait SerializerTrait
{
protected function serialize(mixed $data): string
{
$serializer = (new WebauthnSerializerFactory(AttestationStatementSupportManager::create()))->create();

return $serializer->serialize($data, JsonEncoder::FORMAT, [
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
JsonEncode::OPTIONS => JSON_THROW_ON_ERROR,
]);
}

protected function deserialize(string $data, string $class): mixed
{
$serializer = (new WebauthnSerializerFactory(AttestationStatementSupportManager::create()))->create();

return $serializer->deserialize($data, $class, JsonEncoder::FORMAT);
}
}
15 changes: 7 additions & 8 deletions src/symfony/src/Doctrine/Type/TrustPathDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Webauthn\TrustPath\TrustPath;
use Webauthn\TrustPath\TrustPathLoader;
use const JSON_THROW_ON_ERROR;

final class TrustPathDataType extends Type
{
use SerializerTrait;

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null) {
if (! $value instanceof TrustPath) {
return $value;
}

return json_encode($value, JSON_THROW_ON_ERROR);
return $this->serialize($value);
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?TrustPath
{
if ($value === null || $value instanceof TrustPath) {
return $value;
}
$json = json_decode((string) $value, true, flags: JSON_THROW_ON_ERROR);

return TrustPathLoader::loadTrustPath($json);
return $this->deserialize($value, TrustPath::class);
}

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getJsonTypeDeclarationSQL($fieldDeclaration);
return $platform->getJsonTypeDeclarationSQL($column);
}

public function getName(): string
Expand Down
1 change: 1 addition & 0 deletions src/webauthn/src/AttestedCredentialData.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function getCredentialPublicKey(): ?string

/**
* @param mixed[] $json
* @deprecated since 4.9.0 and will be removed in 5.0.0. Please use the serializer instead.
*/
public static function createFromArray(array $json): self
{
Expand Down
4 changes: 4 additions & 0 deletions src/webauthn/src/Denormalizer/WebauthnSerializerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Webauthn\AttestationStatement\AttestationStatementSupportManager;
use Webauthn\MetadataService\Denormalizer\ExtensionDescriptorDenormalizer;
use Webauthn\MetadataService\Denormalizer\VerificationMethodANDCombinationsDenormalizer;

final class WebauthnSerializerFactory
{
Expand Down Expand Up @@ -42,6 +44,8 @@ public function create(): SerializerInterface
}

$denormalizers = [
new ExtensionDescriptorDenormalizer(),
new VerificationMethodANDCombinationsDenormalizer(),
new AuthenticationExtensionNormalizer(),
new PublicKeyCredentialDescriptorNormalizer(),
new AttestedCredentialDataNormalizer(),
Expand Down
4 changes: 4 additions & 0 deletions src/webauthn/src/PublicKeyCredentialDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public function getTransports(): array
return $this->transports;
}

/**
* @deprecated since 4.9.0 and will be removed in 5.0.0. Please use the serializer instead.
*/
public static function createFromString(string $data): self
{
$data = json_decode($data, true, flags: JSON_THROW_ON_ERROR);
Expand All @@ -88,6 +91,7 @@ public static function createFromString(string $data): self

/**
* @param mixed[] $json
* @deprecated since 4.9.0 and will be removed in 5.0.0. Please use the serializer instead.
*/
public static function createFromArray(array $json): self
{
Expand Down
6 changes: 6 additions & 0 deletions src/webauthn/src/PublicKeyCredentialDescriptorCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public function count(int $mode = COUNT_NORMAL): int
*/
public function jsonSerialize(): array
{
trigger_deprecation(
'web-auth/webauthn-bundle',
'4.9.0',
'The "%s" method is deprecated and will be removed in 5.0. Please use the serializer instead.',
__METHOD__
);
return $this->publicKeyCredentialDescriptors;
}

Expand Down
2 changes: 1 addition & 1 deletion src/webauthn/src/SimpleFakeCredentialGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private function generateCredentials(string $username): array
$transports = [
PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_USB,
PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_NFC,
PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_INTERNAL,
PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_BLE,
];
$credentials = [];
for ($i = 0; $i < random_int(1, 3); $i++) {
Expand Down
3 changes: 3 additions & 0 deletions src/webauthn/src/TrustPath/TrustPathLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use function is_array;
use function is_string;

/**
* @deprecated since 4.9.0 and will be removed in 5.0.0. Use the serializer instead
*/
final class TrustPathLoader
{
/**
Expand Down
10 changes: 10 additions & 0 deletions tests/symfony/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
parameters:
env(DATABASE_URL): ''

framework:
test: true
secret: 'test'
Expand Down Expand Up @@ -95,6 +98,13 @@ services:

doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci

url: '%env(resolve:DATABASE_URL)%'
orm:
enable_lazy_ghost_objects: true
Expand Down
19 changes: 0 additions & 19 deletions tests/symfony/functional/Entity/Credential.php

This file was deleted.

Loading