| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* The MIT License (MIT) |
| 7 |
* |
| 8 |
* Copyright (c) 2014-2021 Spomky-Labs |
| 9 |
* |
| 10 |
* This software may be modified and distributed under the terms |
| 11 |
* of the MIT license. See the LICENSE file for details. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Webauthn; |
| 15 |
|
| 16 |
use Assert\Assertion; |
| 17 |
use Base64Url\Base64Url; |
| 18 |
use function count; |
| 19 |
use function Safe\json_decode; |
| 20 |
use Webauthn\AuthenticationExtensions\AuthenticationExtensionsClientInputs; |
| 21 |
|
| 22 |
class PublicKeyCredentialCreationOptions extends PublicKeyCredentialOptions |
| 23 |
{ |
| 24 |
public const ATTESTATION_CONVEYANCE_PREFERENCE_NONE = 'none'; |
| 25 |
public const ATTESTATION_CONVEYANCE_PREFERENCE_INDIRECT = 'indirect'; |
| 26 |
public const ATTESTATION_CONVEYANCE_PREFERENCE_DIRECT = 'direct'; |
| 27 |
public const ATTESTATION_CONVEYANCE_PREFERENCE_ENTERPRISE = 'enterprise'; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var PublicKeyCredentialRpEntity |
| 31 |
*/ |
| 32 |
private $rp; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var PublicKeyCredentialUserEntity |
| 36 |
*/ |
| 37 |
private $user; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var PublicKeyCredentialParameters[] |
| 41 |
*/ |
| 42 |
private $pubKeyCredParams = []; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var PublicKeyCredentialDescriptor[] |
| 46 |
*/ |
| 47 |
private $excludeCredentials = []; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var AuthenticatorSelectionCriteria |
| 51 |
*/ |
| 52 |
private $authenticatorSelection; |
| 53 |
|
| 54 |
/** |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
private $attestation; |
| 58 |
|
| 59 |
/** |
| 60 |
* @param PublicKeyCredentialParameters[] $pubKeyCredParams |
| 61 |
* @param PublicKeyCredentialDescriptor[] $excludeCredentials |
| 62 |
*/ |
| 63 |
public function __construct(PublicKeyCredentialRpEntity $rp, PublicKeyCredentialUserEntity $user, string $challenge, array $pubKeyCredParams, ?int $timeout = null, array $excludeCredentials = [], ?AuthenticatorSelectionCriteria $authenticatorSelection = null, ?string $attestation = null, ?AuthenticationExtensionsClientInputs $extensions = null) |
| 64 |
{ |
| 65 |
if (0 !== count($excludeCredentials)) { |
| 66 |
@trigger_error('The argument "excludeCredentials" is deprecated since version 3.3 and will be removed in 4.0. Please use the method "excludeCredentials" or "excludeCredential".', E_USER_DEPRECATED); |
| 67 |
} |
| 68 |
if (null !== $authenticatorSelection) { |
| 69 |
@trigger_error('The argument "authenticatorSelection" is deprecated since version 3.3 and will be removed in 4.0. Please use the method "setAuthenticatorSelection".', E_USER_DEPRECATED); |
| 70 |
} |
| 71 |
if (null !== $attestation) { |
| 72 |
@trigger_error('The argument "attestation" is deprecated since version 3.3 and will be removed in 4.0. Please use the method "setAttestation".', E_USER_DEPRECATED); |
| 73 |
} |
| 74 |
parent::__construct($challenge, $timeout, $extensions); |
| 75 |
$this->rp = $rp; |
| 76 |
$this->user = $user; |
| 77 |
$this->pubKeyCredParams = $pubKeyCredParams; |
| 78 |
$this->authenticatorSelection = $authenticatorSelection ?? new AuthenticatorSelectionCriteria(); |
| 79 |
$this->attestation = $attestation ?? self::ATTESTATION_CONVEYANCE_PREFERENCE_NONE; |
| 80 |
$this->excludeCredentials($excludeCredentials) |
| 81 |
; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param PublicKeyCredentialParameters[] $pubKeyCredParams |
| 86 |
*/ |
| 87 |
public static function create(PublicKeyCredentialRpEntity $rp, PublicKeyCredentialUserEntity $user, string $challenge, array $pubKeyCredParams): self |
| 88 |
{ |
| 89 |
return new self($rp, $user, $challenge, $pubKeyCredParams); |
| 90 |
} |
| 91 |
|
| 92 |
public function addPubKeyCredParam(PublicKeyCredentialParameters $pubKeyCredParam): self |
| 93 |
{ |
| 94 |
$this->pubKeyCredParams[] = $pubKeyCredParam; |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @param PublicKeyCredentialParameters[] $pubKeyCredParams |
| 101 |
*/ |
| 102 |
public function addPubKeyCredParams(array $pubKeyCredParams): self |
| 103 |
{ |
| 104 |
foreach ($pubKeyCredParams as $pubKeyCredParam) { |
| 105 |
$this->addPubKeyCredParam($pubKeyCredParam); |
| 106 |
} |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
public function excludeCredential(PublicKeyCredentialDescriptor $excludeCredential): self |
| 112 |
{ |
| 113 |
$this->excludeCredentials[] = $excludeCredential; |
| 114 |
|
| 115 |
return $this; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @param PublicKeyCredentialDescriptor[] $excludeCredentials |
| 120 |
*/ |
| 121 |
public function excludeCredentials(array $excludeCredentials): self |
| 122 |
{ |
| 123 |
foreach ($excludeCredentials as $excludeCredential) { |
| 124 |
$this->excludeCredential($excludeCredential); |
| 125 |
} |
| 126 |
|
| 127 |
return $this; |
| 128 |
} |
| 129 |
|
| 130 |
public function setAuthenticatorSelection(AuthenticatorSelectionCriteria $authenticatorSelection): self |
| 131 |
{ |
| 132 |
$this->authenticatorSelection = $authenticatorSelection; |
| 133 |
|
| 134 |
return $this; |
| 135 |
} |
| 136 |
|
| 137 |
public function setAttestation(string $attestation): self |
| 138 |
{ |
| 139 |
Assertion::inArray($attestation, [ |
| 140 |
self::ATTESTATION_CONVEYANCE_PREFERENCE_NONE, |
| 141 |
self::ATTESTATION_CONVEYANCE_PREFERENCE_DIRECT, |
| 142 |
self::ATTESTATION_CONVEYANCE_PREFERENCE_INDIRECT, |
| 143 |
self::ATTESTATION_CONVEYANCE_PREFERENCE_ENTERPRISE, |
| 144 |
], 'Invalid attestation conveyance mode'); |
| 145 |
$this->attestation = $attestation; |
| 146 |
|
| 147 |
return $this; |
| 148 |
} |
| 149 |
|
| 150 |
public function getRp(): PublicKeyCredentialRpEntity |
| 151 |
{ |
| 152 |
return $this->rp; |
| 153 |
} |
| 154 |
|
| 155 |
public function getUser(): PublicKeyCredentialUserEntity |
| 156 |
{ |
| 157 |
return $this->user; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @return PublicKeyCredentialParameters[] |
| 162 |
*/ |
| 163 |
public function getPubKeyCredParams(): array |
| 164 |
{ |
| 165 |
return $this->pubKeyCredParams; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @return PublicKeyCredentialDescriptor[] |
| 170 |
*/ |
| 171 |
public function getExcludeCredentials(): array |
| 172 |
{ |
| 173 |
return $this->excludeCredentials; |
| 174 |
} |
| 175 |
|
| 176 |
public function getAuthenticatorSelection(): AuthenticatorSelectionCriteria |
| 177 |
{ |
| 178 |
return $this->authenticatorSelection; |
| 179 |
} |
| 180 |
|
| 181 |
public function getAttestation(): string |
| 182 |
{ |
| 183 |
return $this->attestation; |
| 184 |
} |
| 185 |
|
| 186 |
public static function createFromString(string $data): PublicKeyCredentialOptions |
| 187 |
{ |
| 188 |
$data = json_decode($data, true); |
| 189 |
Assertion::isArray($data, 'Invalid data'); |
| 190 |
|
| 191 |
return self::createFromArray($data); |
| 192 |
} |
| 193 |
|
| 194 |
public static function createFromArray(array $json): PublicKeyCredentialOptions |
| 195 |
{ |
| 196 |
Assertion::keyExists($json, 'rp', 'Invalid input. "rp" is missing.'); |
| 197 |
Assertion::keyExists($json, 'pubKeyCredParams', 'Invalid input. "pubKeyCredParams" is missing.'); |
| 198 |
Assertion::isArray($json['pubKeyCredParams'], 'Invalid input. "pubKeyCredParams" is not an array.'); |
| 199 |
Assertion::keyExists($json, 'challenge', 'Invalid input. "challenge" is missing.'); |
| 200 |
Assertion::keyExists($json, 'attestation', 'Invalid input. "attestation" is missing.'); |
| 201 |
Assertion::keyExists($json, 'user', 'Invalid input. "user" is missing.'); |
| 202 |
Assertion::keyExists($json, 'authenticatorSelection', 'Invalid input. "authenticatorSelection" is missing.'); |
| 203 |
|
| 204 |
$pubKeyCredParams = []; |
| 205 |
foreach ($json['pubKeyCredParams'] as $pubKeyCredParam) { |
| 206 |
$pubKeyCredParams[] = PublicKeyCredentialParameters::createFromArray($pubKeyCredParam); |
| 207 |
} |
| 208 |
$excludeCredentials = []; |
| 209 |
if (isset($json['excludeCredentials'])) { |
| 210 |
foreach ($json['excludeCredentials'] as $excludeCredential) { |
| 211 |
$excludeCredentials[] = PublicKeyCredentialDescriptor::createFromArray($excludeCredential); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return self::create( |
| 216 |
PublicKeyCredentialRpEntity::createFromArray($json['rp']), |
| 217 |
PublicKeyCredentialUserEntity::createFromArray($json['user']), |
| 218 |
Base64Url::decode($json['challenge']), |
| 219 |
$pubKeyCredParams |
| 220 |
) |
| 221 |
->excludeCredentials($excludeCredentials) |
| 222 |
->setAuthenticatorSelection(AuthenticatorSelectionCriteria::createFromArray($json['authenticatorSelection'])) |
| 223 |
->setAttestation($json['attestation']) |
| 224 |
->setTimeout($json['timeout'] ?? null) |
| 225 |
->setExtensions(isset($json['extensions']) ? AuthenticationExtensionsClientInputs::createFromArray($json['extensions']) : new AuthenticationExtensionsClientInputs()) |
| 226 |
; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* @return mixed[] |
| 231 |
*/ |
| 232 |
public function jsonSerialize(): array |
| 233 |
{ |
| 234 |
$json = [ |
| 235 |
'rp' => $this->rp->jsonSerialize(), |
| 236 |
'pubKeyCredParams' => array_map(static function (PublicKeyCredentialParameters $object): array { |
| 237 |
return $object->jsonSerialize(); |
| 238 |
}, $this->pubKeyCredParams), |
| 239 |
'challenge' => Base64Url::encode($this->challenge), |
| 240 |
'attestation' => $this->attestation, |
| 241 |
'user' => $this->user->jsonSerialize(), |
| 242 |
'authenticatorSelection' => $this->authenticatorSelection->jsonSerialize(), |
| 243 |
]; |
| 244 |
|
| 245 |
if (0 !== count($this->excludeCredentials)) { |
| 246 |
$json['excludeCredentials'] = array_map(static function (PublicKeyCredentialDescriptor $object): array { |
| 247 |
return $object->jsonSerialize(); |
| 248 |
}, $this->excludeCredentials); |
| 249 |
} |
| 250 |
|
| 251 |
if (0 !== $this->extensions->count()) { |
| 252 |
$json['extensions'] = $this->extensions; |
| 253 |
} |
| 254 |
|
| 255 |
if (null !== $this->timeout) { |
| 256 |
$json['timeout'] = $this->timeout; |
| 257 |
} |
| 258 |
|
| 259 |
return $json; |
| 260 |
} |
| 261 |
} |
| 262 |
|