| 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\CredentialRecord; |
| 11 |
use Webauthn\Exception\AuthenticatorResponseVerificationException; |
| 12 |
use Webauthn\PublicKeyCredentialCreationOptions; |
| 13 |
use Webauthn\PublicKeyCredentialRequestOptions; |
| 14 |
use Webauthn\PublicKeyCredentialSource; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
final readonly class CheckUserWasPresent implements CeremonyStep |
| 28 |
{ |
| 29 |
public function __construct( |
| 30 |
private bool $requireUserPresence = true |
| 31 |
) { |
| 32 |
} |
| 33 |
|
| 34 |
public function process( |
| 35 |
CredentialRecord $credentialRecord, |
| 36 |
AuthenticatorAssertionResponse|AuthenticatorAttestationResponse $authenticatorResponse, |
| 37 |
PublicKeyCredentialRequestOptions|PublicKeyCredentialCreationOptions $publicKeyCredentialOptions, |
| 38 |
?string $userHandle, |
| 39 |
string $host |
| 40 |
): void { |
| 41 |
if ($credentialRecord instanceof PublicKeyCredentialSource) { |
| 42 |
trigger_deprecation( |
| 43 |
'web-auth/webauthn-lib', |
| 44 |
'5.3', |
| 45 |
'Passing a PublicKeyCredentialSource to "%s::process()" is deprecated, pass a CredentialRecord instead.', |
| 46 |
self::class |
| 47 |
); |
| 48 |
} |
| 49 |
if (! $this->isUserPresenceRequired($publicKeyCredentialOptions)) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$authData = $authenticatorResponse instanceof AuthenticatorAssertionResponse |
| 54 |
? $authenticatorResponse->authenticatorData |
| 55 |
: $authenticatorResponse->attestationObject->authData; |
| 56 |
|
| 57 |
$authData->isUserPresent() || throw AuthenticatorResponseVerificationException::create( |
| 58 |
'User was not present' |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
private function isUserPresenceRequired( |
| 63 |
PublicKeyCredentialRequestOptions|PublicKeyCredentialCreationOptions $publicKeyCredentialOptions |
| 64 |
): bool { |
| 65 |
if ($publicKeyCredentialOptions instanceof PublicKeyCredentialCreationOptions |
| 66 |
&& $publicKeyCredentialOptions->mediation === PublicKeyCredentialCreationOptions::MEDIATION_CONDITIONAL) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
return $this->requireUserPresence; |
| 71 |
} |
| 72 |
} |