Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/AttestationStatement/AttestationObjectLoader.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn\AttestationStatement;
6
7 use function array_key_exists;
8 use CBOR\Decoder;
9 use CBOR\Normalizable;
10 use function is_array;
11 use Psr\EventDispatcher\EventDispatcherInterface;
12 use Psr\Log\LoggerInterface;
13 use Psr\Log\NullLogger;
14 use Throwable;
15 use Webauthn\AuthenticatorDataLoader;
16 use Webauthn\Event\AttestationObjectLoaded;
17 use Webauthn\Event\CanDispatchEvents;
18 use Webauthn\Event\NullEventDispatcher;
19 use Webauthn\Exception\InvalidDataException;
20 use Webauthn\MetadataService\CanLogData;
21 use Webauthn\StringStream;
22 use Webauthn\Util\Base64;
23
24 class AttestationObjectLoader implements CanDispatchEvents, CanLogData
25 {
26 private LoggerInterface $logger;
27
28 private EventDispatcherInterface $dispatcher;
29
30 public function __construct(
31 private readonly AttestationStatementSupportManager $attestationStatementSupportManager
32 ) {
33 $this->logger = new NullLogger();
34 $this->dispatcher = new NullEventDispatcher();
35 }
36
37 public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
38 {
39 $this->dispatcher = $eventDispatcher;
40 }
41
42 public static function create(AttestationStatementSupportManager $attestationStatementSupportManager): self
43 {
44 return new self($attestationStatementSupportManager);
45 }
46
47 public function load(string $data): AttestationObject
48 {
49 try {
50 $this->logger->info('Trying to load the data', [
51 'data' => $data,
52 ]);
53 $decodedData = Base64::decode($data);
54 $stream = new StringStream($decodedData);
55 $parsed = Decoder::create()->decode($stream);
56
57 $this->logger->info('Loading the Attestation Statement');
58 $parsed instanceof Normalizable || throw InvalidDataException::create(
59 $parsed,
60 'Invalid attestation object. Unexpected object.'
61 );
62 $attestationObject = $parsed->normalize();
63 $stream->isEOF() || throw InvalidDataException::create(
64 null,
65 'Invalid attestation object. Presence of extra bytes.'
66 );
67 $stream->close();
68 is_array($attestationObject) || throw InvalidDataException::create(
69 $attestationObject,
70 'Invalid attestation object'
71 );
72 array_key_exists('authData', $attestationObject) || throw InvalidDataException::create(
73 $attestationObject,
74 'Invalid attestation object'
75 );
76 array_key_exists('fmt', $attestationObject) || throw InvalidDataException::create(
77 $attestationObject,
78 'Invalid attestation object'
79 );
80 array_key_exists('attStmt', $attestationObject) || throw InvalidDataException::create(
81 $attestationObject,
82 'Invalid attestation object'
83 );
84
85 /** @var array{fmt: string, authData: string, attStmt: array<string, mixed>} $attestationObject */
86 $attestationStatementSupport = $this->attestationStatementSupportManager->get($attestationObject['fmt']);
87 $attestationStatement = $attestationStatementSupport->load($attestationObject);
88 $this->logger->info('Attestation Statement loaded');
89 $this->logger->debug('Attestation Statement loaded', [
90 'attestationStatement' => $attestationStatement,
91 ]);
92 $authData = $attestationObject['authData'];
93 $authDataLoader = AuthenticatorDataLoader::create();
94 $authenticatorData = $authDataLoader->load($authData);
95
96 $attestationObject = AttestationObject::create($data, $attestationStatement, $authenticatorData);
97 $this->logger->info('Attestation Object loaded');
98 $this->logger->debug('Attestation Object', [
99 'ed' => $attestationObject,
100 ]);
101 $this->dispatcher->dispatch(AttestationObjectLoaded::create($attestationObject));
102
103 return $attestationObject;
104 } catch (Throwable $throwable) {
105 $this->logger->error('An error occurred', [
106 'exception' => $throwable,
107 ]);
108 throw $throwable;
109 }
110 }
111
112 public function setLogger(LoggerInterface $logger): void
113 {
114 $this->logger = $logger;
115 }
116 }