| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn; |
| 6 |
|
| 7 |
use function in_array; |
| 8 |
use InvalidArgumentException; |
| 9 |
use Webauthn\AuthenticationExtensions\AuthenticationExtensions; |
| 10 |
use Webauthn\Exception\InvalidDataException; |
| 11 |
|
| 12 |
final class PublicKeyCredentialCreationOptions extends PublicKeyCredentialOptions |
| 13 |
{ |
| 14 |
public const ATTESTATION_CONVEYANCE_PREFERENCE_DEFAULT = null; |
| 15 |
|
| 16 |
public const ATTESTATION_CONVEYANCE_PREFERENCE_NONE = 'none'; |
| 17 |
|
| 18 |
public const ATTESTATION_CONVEYANCE_PREFERENCE_INDIRECT = 'indirect'; |
| 19 |
|
| 20 |
public const ATTESTATION_CONVEYANCE_PREFERENCE_DIRECT = 'direct'; |
| 21 |
|
| 22 |
public const ATTESTATION_CONVEYANCE_PREFERENCE_ENTERPRISE = 'enterprise'; |
| 23 |
|
| 24 |
public const ATTESTATION_CONVEYANCE_PREFERENCES = [ |
| 25 |
self::ATTESTATION_CONVEYANCE_PREFERENCE_DEFAULT, |
| 26 |
self::ATTESTATION_CONVEYANCE_PREFERENCE_NONE, |
| 27 |
self::ATTESTATION_CONVEYANCE_PREFERENCE_INDIRECT, |
| 28 |
self::ATTESTATION_CONVEYANCE_PREFERENCE_DIRECT, |
| 29 |
self::ATTESTATION_CONVEYANCE_PREFERENCE_ENTERPRISE, |
| 30 |
]; |
| 31 |
|
| 32 |
public const MEDIATION_DEFAULT = 'default'; |
| 33 |
|
| 34 |
public const MEDIATION_CONDITIONAL = 'conditional'; |
| 35 |
|
| 36 |
public const MEDIATIONS = [self::MEDIATION_DEFAULT, self::MEDIATION_CONDITIONAL]; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
e. |
| 41 |
|
| 42 |
t. |
| 43 |
): |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public null|string $mediation = null; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( |
| 58 |
public readonly PublicKeyCredentialRpEntity $rp, |
| 59 |
public readonly PublicKeyCredentialUserEntity $user, |
| 60 |
string $challenge, |
| 61 |
public array $pubKeyCredParams = [], |
| 62 |
public null|AuthenticatorSelectionCriteria $authenticatorSelection = null, |
| 63 |
public null|string $attestation = null, |
| 64 |
public array $excludeCredentials = [], |
| 65 |
null|int $timeout = null, |
| 66 |
null|AuthenticationExtensions $extensions = null, |
| 67 |
array $hints = [], |
| 68 |
null|string $mediation = null, |
| 69 |
) { |
| 70 |
foreach ($pubKeyCredParams as $pubKeyCredParam) { |
| 71 |
$pubKeyCredParam instanceof PublicKeyCredentialParameters || throw new InvalidArgumentException( |
| 72 |
'Invalid type for $pubKeyCredParams' |
| 73 |
); |
| 74 |
} |
| 75 |
foreach ($excludeCredentials as $excludeCredential) { |
| 76 |
$excludeCredential instanceof PublicKeyCredentialDescriptor || throw new InvalidArgumentException( |
| 77 |
'Invalid type for $excludeCredentials' |
| 78 |
); |
| 79 |
} |
| 80 |
in_array($attestation, self::ATTESTATION_CONVEYANCE_PREFERENCES, true) || throw InvalidDataException::create( |
| 81 |
$attestation, |
| 82 |
'Invalid attestation conveyance mode' |
| 83 |
); |
| 84 |
$mediation === null || in_array($mediation, self::MEDIATIONS, true) || throw InvalidDataException::create( |
| 85 |
$mediation, |
| 86 |
'Invalid mediation requirement' |
| 87 |
); |
| 88 |
|
| 89 |
$this->mediation = $mediation; |
| 90 |
|
| 91 |
parent::__construct($challenge, $timeout, $extensions, $hints); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static function create( |
| 101 |
PublicKeyCredentialRpEntity $rp, |
| 102 |
PublicKeyCredentialUserEntity $user, |
| 103 |
string $challenge, |
| 104 |
array $pubKeyCredParams = [], |
| 105 |
null|AuthenticatorSelectionCriteria $authenticatorSelection = null, |
| 106 |
null|string $attestation = null, |
| 107 |
array $excludeCredentials = [], |
| 108 |
null|int $timeout = null, |
| 109 |
null|AuthenticationExtensions $extensions = null, |
| 110 |
array $hints = [], |
| 111 |
null|string $mediation = null, |
| 112 |
): self { |
| 113 |
return new self( |
| 114 |
$rp, |
| 115 |
$user, |
| 116 |
$challenge, |
| 117 |
$pubKeyCredParams, |
| 118 |
$authenticatorSelection, |
| 119 |
$attestation, |
| 120 |
$excludeCredentials, |
| 121 |
$timeout, |
| 122 |
$extensions, |
| 123 |
$hints, |
| 124 |
$mediation, |
| 125 |
); |
| 126 |
} |
| 127 |
} |