Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn;
6
7 use Psr\EventDispatcher\EventDispatcherInterface;
8 use Psr\Log\LoggerInterface;
9 use Psr\Log\NullLogger;
10 use Throwable;
11 use Webauthn\CeremonyStep\CeremonyStepManager;
12 use Webauthn\Event\AuthenticatorAttestationResponseValidationFailedEvent;
13 use Webauthn\Event\AuthenticatorAttestationResponseValidationSucceededEvent;
14 use Webauthn\Event\BackupEligibilityChangedEvent;
15 use Webauthn\Event\BackupStatusChangedEvent;
16 use Webauthn\Event\CanDispatchEvents;
17 use Webauthn\Event\NullEventDispatcher;
18 use Webauthn\Exception\AuthenticatorResponseVerificationException;
19 use Webauthn\MetadataService\CanLogData;
20
21 class AuthenticatorAttestationResponseValidator implements CanLogData, CanDispatchEvents
22 {
23 private LoggerInterface $logger;
24
25 private EventDispatcherInterface $eventDispatcher;
26
27 public function __construct(
28 private readonly CeremonyStepManager $ceremonyStepManager
29 ) {
30 $this->eventDispatcher = new NullEventDispatcher();
31 $this->logger = new NullLogger();
32 }
33
34 public static function create(CeremonyStepManager $ceremonyStepManager): self
35 {
36 return new self($ceremonyStepManager);
37 }
38
39 public function setLogger(LoggerInterface $logger): void
40 {
41 $this->logger = $logger;
42 }
43
44 public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
45 {
46 $this->eventDispatcher = $eventDispatcher;
47 }
48
49 /**
50 * @see https://www.w3.org/TR/webauthn/#registering-a-new-credential
51 */
52 public function check(
53 AuthenticatorAttestationResponse $authenticatorAttestationResponse,
54 PublicKeyCredentialCreationOptions $publicKeyCredentialCreationOptions,
55 string $host,
56 ): CredentialRecord {
57 try {
58 $this->logger->info('Checking the authenticator attestation response', [
59 'authenticatorAttestationResponse' => $authenticatorAttestationResponse,
60 'publicKeyCredentialCreationOptions' => $publicKeyCredentialCreationOptions,
61 'host' => $host,
62 ]);
63
64 $credentialRecord = $this->createCredentialRecord(
65 $authenticatorAttestationResponse,
66 $publicKeyCredentialCreationOptions
67 );
68
69 $this->ceremonyStepManager->process(
70 $credentialRecord,
71 $authenticatorAttestationResponse,
72 $publicKeyCredentialCreationOptions,
73 $publicKeyCredentialCreationOptions->user->id,
74 $host
75 );
76
77 // Store previous backup state values to detect changes (will be null for new credentials)
78 $previousBackupEligible = $credentialRecord->backupEligible;
79 $previousBackupStatus = $credentialRecord->backupStatus;
80
81 $credentialRecord->counter = $authenticatorAttestationResponse->attestationObject->authData->signCount;
82 $credentialRecord->backupEligible = $authenticatorAttestationResponse->attestationObject->authData->isBackupEligible();
83 $credentialRecord->backupStatus = $authenticatorAttestationResponse->attestationObject->authData->isBackedUp();
84 $credentialRecord->uvInitialized = $authenticatorAttestationResponse->attestationObject->authData->isUserVerified();
85
86 // Dispatch events if backup state changed
87 if ($previousBackupEligible !== $credentialRecord->backupEligible) {
88 $this->eventDispatcher->dispatch(
89 new BackupEligibilityChangedEvent(
90 $credentialRecord,
91 $previousBackupEligible,
92 $credentialRecord->backupEligible
93 )
94 );
95 }
96 if ($previousBackupStatus !== $credentialRecord->backupStatus) {
97 $this->eventDispatcher->dispatch(
98 new BackupStatusChangedEvent(
99 $credentialRecord,
100 $previousBackupStatus,
101 $credentialRecord->backupStatus
102 )
103 );
104 }
105
106 $this->logger->info('The attestation is valid');
107 $this->logger->debug('Credential Record', [
108 'credentialRecord' => $credentialRecord,
109 ]);
110 $this->eventDispatcher->dispatch(
111 $this->createAuthenticatorAttestationResponseValidationSucceededEvent(
112 $authenticatorAttestationResponse,
113 $publicKeyCredentialCreationOptions,
114 $host,
115 $credentialRecord
116 )
117 );
118 return $credentialRecord;
119 } catch (Throwable $throwable) {
120 $this->logger->error('An error occurred', [
121 'exception' => $throwable,
122 ]);
123 $this->eventDispatcher->dispatch(
124 $this->createAuthenticatorAttestationResponseValidationFailedEvent(
125 $authenticatorAttestationResponse,
126 $publicKeyCredentialCreationOptions,
127 $host,
128 $throwable
129 )
130 );
131 throw $throwable;
132 }
133 }
134
135 protected function createAuthenticatorAttestationResponseValidationSucceededEvent(
136 AuthenticatorAttestationResponse $authenticatorAttestationResponse,
137 PublicKeyCredentialCreationOptions $publicKeyCredentialCreationOptions,
138 string $host,
139 CredentialRecord $credentialRecord
140 ): AuthenticatorAttestationResponseValidationSucceededEvent {
141 return new AuthenticatorAttestationResponseValidationSucceededEvent(
142 $authenticatorAttestationResponse,
143 $publicKeyCredentialCreationOptions,
144 $host,
145 $credentialRecord
146 );
147 }
148
149 protected function createAuthenticatorAttestationResponseValidationFailedEvent(
150 AuthenticatorAttestationResponse $authenticatorAttestationResponse,
151 PublicKeyCredentialCreationOptions $publicKeyCredentialCreationOptions,
152 string $host,
153 Throwable $throwable
154 ): AuthenticatorAttestationResponseValidationFailedEvent {
155 return new AuthenticatorAttestationResponseValidationFailedEvent(
156 $authenticatorAttestationResponse,
157 $publicKeyCredentialCreationOptions,
158 $host,
159 $throwable
160 );
161 }
162
163 private function createCredentialRecord(
164 AuthenticatorAttestationResponse $authenticatorAttestationResponse,
165 PublicKeyCredentialCreationOptions $publicKeyCredentialCreationOptions,
166 ): CredentialRecord {
167 $attestationObject = $authenticatorAttestationResponse->attestationObject;
168 $attestedCredentialData = $attestationObject->authData->attestedCredentialData;
169 $attestedCredentialData !== null || throw AuthenticatorResponseVerificationException::create(
170 'Not attested credential data'
171 );
172 $credentialId = $attestedCredentialData->credentialId;
173 $credentialPublicKey = $attestedCredentialData->credentialPublicKey;
174 $credentialPublicKey !== null || throw AuthenticatorResponseVerificationException::create(
175 'Not credential public key available in the attested credential data'
176 );
177 $userHandle = $publicKeyCredentialCreationOptions->user->id;
178 $transports = $authenticatorAttestationResponse->transports;
179
180 return CredentialRecord::create(
181 $credentialId,
182 PublicKeyCredentialDescriptor::CREDENTIAL_TYPE_PUBLIC_KEY,
183 $transports,
184 $attestationObject->attStmt
185 ->type,
186 $attestationObject->attStmt
187 ->trustPath,
188 $attestedCredentialData->aaguid,
189 $credentialPublicKey,
190 $userHandle,
191 $attestationObject->authData
192 ->signCount,
193 );
194 }
195 }