| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\Counter; |
| 6 |
|
| 7 |
use Psr\Log\LoggerInterface; |
| 8 |
use Psr\Log\NullLogger; |
| 9 |
use function trigger_deprecation; |
| 10 |
use Webauthn\CredentialRecord; |
| 11 |
use Webauthn\Exception\CounterException; |
| 12 |
use Webauthn\MetadataService\CanLogData; |
| 13 |
use Webauthn\PublicKeyCredentialSource; |
| 14 |
|
| 15 |
final class ThrowExceptionIfInvalid implements CounterChecker, CanLogData |
| 16 |
{ |
| 17 |
public function __construct( |
| 18 |
private LoggerInterface $logger = new NullLogger() |
| 19 |
) { |
| 20 |
} |
| 21 |
|
| 22 |
public function setLogger(LoggerInterface $logger): void |
| 23 |
{ |
| 24 |
$this->logger = $logger; |
| 25 |
} |
| 26 |
|
| 27 |
public function check(CredentialRecord $credentialRecord, int $currentCounter): void |
| 28 |
{ |
| 29 |
if ($credentialRecord instanceof PublicKeyCredentialSource) { |
| 30 |
trigger_deprecation( |
| 31 |
'web-auth/webauthn-lib', |
| 32 |
'5.3', |
| 33 |
'Passing a PublicKeyCredentialSource to "%s::check()" is deprecated, pass a CredentialRecord instead.', |
| 34 |
self::class |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
try { |
| 39 |
$currentCounter > $credentialRecord->counter || throw CounterException::create( |
| 40 |
$currentCounter, |
| 41 |
$credentialRecord->counter, |
| 42 |
'Invalid counter.' |
| 43 |
); |
| 44 |
} catch (CounterException $throwable) { |
| 45 |
$this->logger->error('The counter is invalid', [ |
| 46 |
'current' => $currentCounter, |
| 47 |
'new' => $credentialRecord->counter, |
| 48 |
]); |
| 49 |
throw $throwable; |
| 50 |
} |
| 51 |
} |
| 52 |
} |