Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/CeremonyStep/CheckUserWasPresent.php

raw symbols SCIP
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 * Conditional check for user presence.
18 *
19 * The User Presence (UP) bit MUST be set unless the ceremony explicitly opts out via the
20 * Conditional Create flow (e.g. SimpleWebAuthn `useAutoRegister: true`). The opt-out is
21 * controlled at runtime by `PublicKeyCredentialCreationOptions::$mediation`, which the
22 * options builder sets from the configured policy. The constructor flag remains a static
23 * fallback used by `CeremonyStepManagerFactory::conditionalCreateCeremony()`.
24 *
25 * @see https://github.com/w3c/webauthn/wiki/Explainer:-Conditional-Create
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 }