| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\CeremonyStep; |
| 6 |
|
| 7 |
use function in_array; |
| 8 |
use function is_array; |
| 9 |
use function is_string; |
| 10 |
use function strlen; |
| 11 |
use function trigger_deprecation; |
| 12 |
use Webauthn\AuthenticationExtensions\AuthenticationExtensions; |
| 13 |
use Webauthn\AuthenticatorAssertionResponse; |
| 14 |
use Webauthn\AuthenticatorAttestationResponse; |
| 15 |
use Webauthn\CredentialRecord; |
| 16 |
use Webauthn\Exception\AuthenticatorResponseVerificationException; |
| 17 |
use Webauthn\PublicKeyCredentialCreationOptions; |
| 18 |
use Webauthn\PublicKeyCredentialRequestOptions; |
| 19 |
use Webauthn\PublicKeyCredentialSource; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
final readonly class CheckOrigin implements CeremonyStep |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function __construct( |
| 30 |
private array $securedRelyingPartyId |
| 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 |
$authData = $authenticatorResponse instanceof AuthenticatorAssertionResponse ? $authenticatorResponse->authenticatorData : $authenticatorResponse->attestationObject->authData; |
| 50 |
$C = $authenticatorResponse->clientDataJSON; |
| 51 |
$rpId = $publicKeyCredentialOptions->rpId ?? $publicKeyCredentialOptions->rp->id ?? $host; |
| 52 |
$facetId = $this->getFacetId($rpId, $publicKeyCredentialOptions->extensions, $authData->extensions); |
| 53 |
$parsedRelyingPartyId = parse_url($C->origin); |
| 54 |
is_array($parsedRelyingPartyId) || throw AuthenticatorResponseVerificationException::create( |
| 55 |
'Invalid origin' |
| 56 |
); |
| 57 |
if (! in_array($facetId, $this->securedRelyingPartyId, true)) { |
| 58 |
$scheme = $parsedRelyingPartyId['scheme'] ?? ''; |
| 59 |
$scheme === 'https' || throw AuthenticatorResponseVerificationException::create( |
| 60 |
'Invalid scheme. HTTPS required.' |
| 61 |
); |
| 62 |
} |
| 63 |
$clientDataRpId = $parsedRelyingPartyId['host'] ?? ''; |
| 64 |
$clientDataRpId !== '' || throw AuthenticatorResponseVerificationException::create('Invalid origin rpId.'); |
| 65 |
$rpIdLength = strlen($facetId); |
| 66 |
|
| 67 |
substr( |
| 68 |
'.' . $clientDataRpId, |
| 69 |
-($rpIdLength + 1) |
| 70 |
) === '.' . $facetId || throw AuthenticatorResponseVerificationException::create('rpId mismatch.'); |
| 71 |
} |
| 72 |
|
| 73 |
private function getFacetId( |
| 74 |
string $rpId, |
| 75 |
AuthenticationExtensions $AuthenticationExtensions, |
| 76 |
null|AuthenticationExtensions $authenticationExtensionsClientOutputs |
| 77 |
): string { |
| 78 |
if ($authenticationExtensionsClientOutputs === null || ! $AuthenticationExtensions->has( |
| 79 |
'appid' |
| 80 |
) || ! $authenticationExtensionsClientOutputs->has('appid')) { |
| 81 |
return $rpId; |
| 82 |
} |
| 83 |
$appId = $AuthenticationExtensions->get('appid') |
| 84 |
->value; |
| 85 |
$wasUsed = $authenticationExtensionsClientOutputs->get('appid') |
| 86 |
->value; |
| 87 |
if (! is_string($appId) || $wasUsed !== true) { |
| 88 |
return $rpId; |
| 89 |
} |
| 90 |
return $appId; |
| 91 |
} |
| 92 |
} |