| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Webauthn\CeremonyStep; |
| 6 | |
| 7 | use function trigger_deprecation; |
| 8 | use Webauthn\AuthenticatorAssertionResponse; |
| 9 | use Webauthn\AuthenticatorAttestationResponse; |
| 10 | use Webauthn\Counter\CounterChecker; |
| 11 | use Webauthn\CredentialRecord; |
| 12 | use Webauthn\PublicKeyCredentialCreationOptions; |
| 13 | use Webauthn\PublicKeyCredentialRequestOptions; |
| 14 | use Webauthn\PublicKeyCredentialSource; |
| 15 | |
| 16 | final readonly class CheckCounter implements CeremonyStep |
| 17 | { |
| 18 | public function __construct( |
| 19 | private CounterChecker $counterChecker |
| 20 | ) { |
| 21 | } |
| 22 | |
| 23 | public function process( |
| 24 | CredentialRecord $credentialRecord, |
| 25 | AuthenticatorAssertionResponse|AuthenticatorAttestationResponse $authenticatorResponse, |
| 26 | PublicKeyCredentialRequestOptions|PublicKeyCredentialCreationOptions $publicKeyCredentialOptions, |
| 27 | ?string $userHandle, |
| 28 | string $host |
| 29 | ): void { |
| 30 | if ($credentialRecord instanceof PublicKeyCredentialSource) { |
| 31 | trigger_deprecation( |
| 32 | 'web-auth/webauthn-lib', |
| 33 | '5.3', |
| 34 | 'Passing a PublicKeyCredentialSource to "%s::process()" is deprecated, pass a CredentialRecord instead.', |
| 35 | self::class |
| 36 | ); |
| 37 | } |
| 38 | $authData = $authenticatorResponse instanceof AuthenticatorAssertionResponse ? $authenticatorResponse->authenticatorData : $authenticatorResponse->attestationObject->authData; |
| 39 | $storedCounter = $credentialRecord->counter; |
| 40 | $responseCounter = $authData->signCount; |
| 41 | if ($responseCounter !== 0 || $storedCounter !== 0) { |
| 42 | $this->counterChecker->check($credentialRecord, $responseCounter); |
| 43 | } |
| 44 | $credentialRecord->counter = $responseCounter; |
| 45 | } |
| 46 | } |