Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/MetadataService/Service/LocalResourceMetadataService.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn\MetadataService\Service;
6
7 use function assert;
8 use function file_get_contents;
9 use ParagonIE\ConstantTime\Base64;
10 use Psr\EventDispatcher\EventDispatcherInterface;
11 use Symfony\Component\Serializer\Encoder\JsonEncoder;
12 use Symfony\Component\Serializer\SerializerInterface;
13 use Webauthn\AttestationStatement\AttestationStatementSupportManager;
14 use Webauthn\Denormalizer\WebauthnSerializerFactory;
15 use Webauthn\Event\CanDispatchEvents;
16 use Webauthn\Event\MetadataStatementFound;
17 use Webauthn\Event\NullEventDispatcher;
18 use Webauthn\Exception\MetadataStatementLoadingException;
19 use Webauthn\Exception\MissingMetadataStatementException;
20 use Webauthn\MetadataService\Statement\MetadataStatement;
21
22 final class LocalResourceMetadataService implements MetadataService, CanDispatchEvents
23 {
24 private ?MetadataStatement $statement = null;
25
26 private EventDispatcherInterface $dispatcher;
27
28 private readonly SerializerInterface $serializer;
29
30 public function __construct(
31 private readonly string $filename,
32 private readonly bool $isBase64Encoded = false,
33 ?SerializerInterface $serializer = null,
34 ) {
35 $this->serializer = $serializer ?? (new WebauthnSerializerFactory(
36 AttestationStatementSupportManager::create()
37 ))->create();
38 $this->dispatcher = new NullEventDispatcher();
39 }
40
41 public static function create(
42 string $filename,
43 bool $isBase64Encoded = false,
44 ?SerializerInterface $serializer = null
45 ): self {
46 return new self($filename, $isBase64Encoded, $serializer);
47 }
48
49 public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
50 {
51 $this->dispatcher = $eventDispatcher;
52 }
53
54 public function list(): iterable
55 {
56 $this->loadData();
57 $this->statement !== null || throw MetadataStatementLoadingException::create();
58 $aaguid = $this->statement->aaguid;
59 if ($aaguid === null) {
60 yield from [];
61 } else {
62 yield from [$aaguid];
63 }
64 }
65
66 public function has(string $aaguid): bool
67 {
68 $this->loadData();
69 $this->statement !== null || throw MetadataStatementLoadingException::create();
70
71 return $aaguid === $this->statement->aaguid;
72 }
73
74 public function get(string $aaguid): MetadataStatement
75 {
76 $this->loadData();
77 $this->statement !== null || throw MetadataStatementLoadingException::create();
78
79 if ($aaguid === $this->statement->aaguid) {
80 $this->dispatcher->dispatch(MetadataStatementFound::create($this->statement));
81
82 return $this->statement;
83 }
84
85 throw MissingMetadataStatementException::create($aaguid);
86 }
87
88 private function loadData(): void
89 {
90 if ($this->statement !== null) {
91 return;
92 }
93
94 $content = file_get_contents($this->filename);
95 assert($content !== false, 'The file could not be read');
96 if ($this->isBase64Encoded) {
97 $content = Base64::decode($content, true);
98 }
99 $this->statement = $this->serializer->deserialize($content, MetadataStatement::class, JsonEncoder::FORMAT);
100 }
101 }