Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn\MetadataService\Service;
6
7 use ParagonIE\ConstantTime\Base64;
8 use Psr\EventDispatcher\EventDispatcherInterface;
9 use function sprintf;
10 use Symfony\Component\Serializer\Encoder\JsonEncoder;
11 use Symfony\Component\Serializer\SerializerInterface;
12 use Symfony\Contracts\HttpClient\HttpClientInterface;
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 DistantResourceMetadataService implements MetadataService, CanDispatchEvents
23 {
24 private ?MetadataStatement $statement = null;
25
26 private EventDispatcherInterface $dispatcher;
27
28 private readonly SerializerInterface $serializer;
29
30 /**
31 * @param array<string, string> $additionalHeaderParameters
32 */
33 public function __construct(
34 private readonly HttpClientInterface $httpClient,
35 private readonly string $uri,
36 private readonly bool $isBase64Encoded = false,
37 private readonly array $additionalHeaderParameters = [],
38 ?SerializerInterface $serializer = null,
39 ) {
40 $this->serializer = $serializer ?? (new WebauthnSerializerFactory(
41 AttestationStatementSupportManager::create()
42 ))->create();
43 $this->dispatcher = new NullEventDispatcher();
44 }
45
46 public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
47 {
48 $this->dispatcher = $eventDispatcher;
49 }
50
51 /**
52 * @param array<string, string> $additionalHeaderParameters
53 */
54 public static function create(
55 HttpClientInterface $httpClient,
56 string $uri,
57 bool $isBase64Encoded = false,
58 array $additionalHeaderParameters = [],
59 ?SerializerInterface $serializer = null
60 ): self {
61 return new self($httpClient, $uri, $isBase64Encoded, $additionalHeaderParameters, $serializer);
62 }
63
64 public function list(): iterable
65 {
66 $this->loadData();
67 $this->statement !== null || throw MetadataStatementLoadingException::create();
68 $aaguid = $this->statement->aaguid;
69 if ($aaguid === null) {
70 yield from [];
71 } else {
72 yield from [$aaguid];
73 }
74 }
75
76 public function has(string $aaguid): bool
77 {
78 $this->loadData();
79 $this->statement !== null || throw MetadataStatementLoadingException::create();
80
81 return $aaguid === $this->statement->aaguid;
82 }
83
84 public function get(string $aaguid): MetadataStatement
85 {
86 $this->loadData();
87 $this->statement !== null || throw MetadataStatementLoadingException::create();
88
89 if ($aaguid === $this->statement->aaguid) {
90 $this->dispatcher->dispatch(MetadataStatementFound::create($this->statement));
91
92 return $this->statement;
93 }
94
95 throw MissingMetadataStatementException::create($aaguid);
96 }
97
98 private function loadData(): void
99 {
100 if ($this->statement !== null) {
101 return;
102 }
103
104 $content = $this->fetch();
105 if ($this->isBase64Encoded) {
106 $content = Base64::decode($content, true);
107 }
108 $this->statement = $this->serializer->deserialize($content, MetadataStatement::class, JsonEncoder::FORMAT);
109 }
110
111 private function fetch(): string
112 {
113 $response = $this->httpClient->request('GET', $this->uri, [
114 'headers' => $this->additionalHeaderParameters,
115 ]);
116 $response->getStatusCode() === 200 || throw MetadataStatementLoadingException::create(sprintf(
117 'Unable to contact the server. Response code is %d',
118 $response->getStatusCode()
119 ));
120
121 $content = $response->getContent();
122 $content !== '' || throw MetadataStatementLoadingException::create(
123 'Unable to contact the server. The response has no content'
124 );
125
126 return $content;
127 }
128 }