Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/AuthenticatorAssertionResponseValidator.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 function trigger_deprecation;
12 use Webauthn\CeremonyStep\CeremonyStepManager;
13 use Webauthn\Event\AuthenticatorAssertionResponseValidationFailedEvent;
14 use Webauthn\Event\AuthenticatorAssertionResponseValidationSucceededEvent;
15 use Webauthn\Event\BackupEligibilityChangedEvent;
16 use Webauthn\Event\BackupStatusChangedEvent;
17 use Webauthn\Event\CanDispatchEvents;
18 use Webauthn\Event\NullEventDispatcher;
19 use Webauthn\Exception\AuthenticatorResponseVerificationException;
20 use Webauthn\MetadataService\CanLogData;
21
22 class AuthenticatorAssertionResponseValidator implements CanLogData, CanDispatchEvents
23 {
24 private LoggerInterface $logger;
25
26 private EventDispatcherInterface $eventDispatcher;
27
28 public function __construct(
29 private readonly CeremonyStepManager $ceremonyStepManager
30 ) {
31 $this->eventDispatcher = new NullEventDispatcher();
32 $this->logger = new NullLogger();
33 }
34
35 public static function create(CeremonyStepManager $ceremonyStepManager): self
36 {
37 return new self($ceremonyStepManager);
38 }
39
40 /**
41 * @see https://www.w3.org/TR/webauthn/#verifying-assertion
42 */
43 public function check(
44 CredentialRecord $credentialRecord,
45 AuthenticatorAssertionResponse $authenticatorAssertionResponse,
46 PublicKeyCredentialRequestOptions $publicKeyCredentialRequestOptions,
47 string $host,
48 ?string $userHandle,
49 ): CredentialRecord {
50 if ($credentialRecord instanceof PublicKeyCredentialSource) {
51 trigger_deprecation(
52 'web-auth/webauthn-lib',
53 '5.3',
54 'Passing a PublicKeyCredentialSource to "%s::check()" is deprecated, pass a CredentialRecord instead.',
55 self::class
56 );
57 }
58
59 try {
60 $this->logger->info('Checking the authenticator assertion response', [
61 'credentialRecord' => $credentialRecord,
62 'authenticatorAssertionResponse' => $authenticatorAssertionResponse,
63 'publicKeyCredentialRequestOptions' => $publicKeyCredentialRequestOptions,
64 'host' => $host,
65 'userHandle' => $userHandle,
66 ]);
67
68 $this->ceremonyStepManager->process(
69 $credentialRecord,
70 $authenticatorAssertionResponse,
71 $publicKeyCredentialRequestOptions,
72 $userHandle,
73 $host
74 );
75
76 // Store previous backup state values to detect changes
77 $previousBackupEligible = $credentialRecord->backupEligible;
78 $previousBackupStatus = $credentialRecord->backupStatus;
79
80 $credentialRecord->counter = $authenticatorAssertionResponse->authenticatorData->signCount; //26.1.
81 $credentialRecord->backupEligible = $authenticatorAssertionResponse->authenticatorData->isBackupEligible(); //26.2.
82 $credentialRecord->backupStatus = $authenticatorAssertionResponse->authenticatorData->isBackedUp(); //26.2.
83 if ($credentialRecord->uvInitialized === false) {
84 $credentialRecord->uvInitialized = $authenticatorAssertionResponse->authenticatorData->isUserVerified(); //26.3.
85 }
86
87 // Dispatch events if backup state changed
88 if ($previousBackupEligible !== $credentialRecord->backupEligible) {
89 $this->eventDispatcher->dispatch(
90 new BackupEligibilityChangedEvent(
91 $credentialRecord,
92 $previousBackupEligible,
93 $credentialRecord->backupEligible
94 )
95 );
96 }
97 if ($previousBackupStatus !== $credentialRecord->backupStatus) {
98 $this->eventDispatcher->dispatch(
99 new BackupStatusChangedEvent(
100 $credentialRecord,
101 $previousBackupStatus,
102 $credentialRecord->backupStatus
103 )
104 );
105 }
106 /*
107 * 26.3.
108 * OPTIONALLY, if response.attestationObject is present, update credentialRecord.attestationObject to the value of response.attestationObject and update credentialRecord.attestationClientDataJSON to the value of response.clientDataJSON.
109 */
110
111 //All good. We can continue.
112 $this->logger->info('The assertion is valid');
113 $this->logger->debug('Credential Record', [
114 'credentialRecord' => $credentialRecord,
115 ]);
116 $this->eventDispatcher->dispatch(
117 $this->createAuthenticatorAssertionResponseValidationSucceededEvent(
118 $authenticatorAssertionResponse,
119 $publicKeyCredentialRequestOptions,
120 $host,
121 $userHandle,
122 $credentialRecord
123 )
124 );
125 // 27.
126 return $credentialRecord;
127 } catch (AuthenticatorResponseVerificationException $throwable) {
128 $this->logger->error('An error occurred', [
129 'exception' => $throwable,
130 ]);
131 $this->eventDispatcher->dispatch(
132 $this->createAuthenticatorAssertionResponseValidationFailedEvent(
133 $credentialRecord,
134 $authenticatorAssertionResponse,
135 $publicKeyCredentialRequestOptions,
136 $host,
137 $userHandle,
138 $throwable
139 )
140 );
141 throw $throwable;
142 }
143 }
144
145 public function setLogger(LoggerInterface $logger): void
146 {
147 $this->logger = $logger;
148 }
149
150 public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
151 {
152 $this->eventDispatcher = $eventDispatcher;
153 }
154
155 protected function createAuthenticatorAssertionResponseValidationSucceededEvent(
156 AuthenticatorAssertionResponse $authenticatorAssertionResponse,
157 PublicKeyCredentialRequestOptions $publicKeyCredentialRequestOptions,
158 string $host,
159 ?string $userHandle,
160 CredentialRecord $credentialRecord
161 ): AuthenticatorAssertionResponseValidationSucceededEvent {
162 return new AuthenticatorAssertionResponseValidationSucceededEvent(
163 $authenticatorAssertionResponse,
164 $publicKeyCredentialRequestOptions,
165 $host,
166 $userHandle,
167 $credentialRecord
168 );
169 }
170
171 protected function createAuthenticatorAssertionResponseValidationFailedEvent(
172 CredentialRecord $credentialRecord,
173 AuthenticatorAssertionResponse $authenticatorAssertionResponse,
174 PublicKeyCredentialRequestOptions $publicKeyCredentialRequestOptions,
175 string $host,
176 ?string $userHandle,
177 Throwable $throwable
178 ): AuthenticatorAssertionResponseValidationFailedEvent {
179 return new AuthenticatorAssertionResponseValidationFailedEvent(
180 $credentialRecord,
181 $authenticatorAssertionResponse,
182 $publicKeyCredentialRequestOptions,
183 $host,
184 $userHandle,
185 $throwable
186 );
187 }
188 }