Credential Source Repository
Where the public keys and details are stored
The Credential Source can be stored the way you want. As the Webauthn\PublicKeyCredentialSource class can be converted into JSON, it could be stored in a filesystem.
In general, Symfony applications use Doctrine. That is why the bundle provides a way to use Doctrine as storage system.

The Doctrine Entity

Hereafter an example of an entity.
This is the most simple example. Feel free to add custom fields that fits on your needs e.g. created_at or is_revoked.
App/Entity/PublicKeyCredentialSource.php
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Entity;
6
7
use App\Repository\PublicKeyCredentialSourceRepository;
8
use Doctrine\ORM\Mapping\Entity;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\GeneratedValue;
11
use Doctrine\ORM\Mapping\Id;
12
use Doctrine\ORM\Mapping\Table;
13
use Symfony\Component\Uid\AbstractUid;
14
use Symfony\Component\Uid\Ulid;
15
use Webauthn\PublicKeyCredentialSource as BasePublicKeyCredentialSource;
16
use Webauthn\TrustPath\TrustPath;
17
18
#[Table(name: "public_key_credential_sources")]
19
#[Entity(repositoryClass: PublicKeyCredentialSourceRepository::class)]
20
class PublicKeyCredentialSource extends BasePublicKeyCredentialSource
21
{
22
#[Id]
23
#[Column(type: "ulid", unique: true)]
24
#[GeneratedValue(strategy: "NONE")]
25
private string $id;
26
27
public function __construct(
28
string $publicKeyCredentialId,
29
string $type,
30
array $transports,
31
string $attestationType,
32
TrustPath $trustPath,
33
AbstractUid $aaguid,
34
string $credentialPublicKey,
35
string $userHandle,
36
int $counter
37
)
38
{
39
$this->id = Ulid::generate();
40
parent::__construct($publicKeyCredentialId, $type, $transports, $attestationType, $trustPath, $aaguid, $credentialPublicKey, $userHandle, $counter);
41
}
42
43
public function getId(): string
44
{
45
return $this->id;
46
}
47
}
Copied!
Do not forget to update your database schema!

The Repository

To ease the integration into your application, the bundle provides a concrete class that you can extend.
In this following example, we extend that class and add a method to get all credentials for a specific user handle. Feel free to add your own methods.
We must override the method saveCredentialSource because we may receive Webauthn\PublicKeyCredentialSource objects instead of App\Entity\PublicKeyCredentialSource.
App/Repository/PublicKeyCredentialSourceRepository.php
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Entity\PublicKeyCredentialSource;
8
use Doctrine\Persistence\ManagerRegistry;
9
use Webauthn\Bundle\Repository\PublicKeyCredentialSourceRepository as BasePublicKeyCredentialSourceRepository;
10
use Webauthn\PublicKeyCredentialSource as BasePublicKeyCredentialSource;
11
12
final class PublicKeyCredentialSourceRepository extends BasePublicKeyCredentialSourceRepository
13
{
14
public function __construct(ManagerRegistry $registry)
15
{
16
parent::__construct($registry, PublicKeyCredentialSource::class);
17
}
18
19
public function saveCredentialSource(BasePublicKeyCredentialSource $publicKeyCredentialSource, bool $flush = true): void
20
{
21
if (!$publicKeyCredentialSource instanceof PublicKeyCredentialSource) {
22
$publicKeyCredentialSource = new PublicKeyCredentialSource(
23
$publicKeyCredentialSource->getPublicKeyCredentialId(),
24
$publicKeyCredentialSource->getType(),
25
$publicKeyCredentialSource->getTransports(),
26
$publicKeyCredentialSource->getAttestationType(),
27
$publicKeyCredentialSource->getTrustPath(),
28
$publicKeyCredentialSource->getAaguid(),
29
$publicKeyCredentialSource->getCredentialPublicKey(),
30
$publicKeyCredentialSource->getUserHandle(),
31
$publicKeyCredentialSource->getCounter()
32
);
33
}
34
parent::saveCredentialSource($publicKeyCredentialSource, $flush);
35
}
36
}
Copied!
This repository should be declared as a Symfony service.
With Symfony autowiring and autoconfiguration, this is usually done automatically
config/services.yaml
1
services:
2
App\Repository\PublicKeyCredentialSourceRepository: ~
Copied!
Export as PDF
Copy link
Edit on GitHub