| 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 WP_DEFENDER_VENDOR\Webauthn; |
| 15 |
|
| 16 |
use WP_DEFENDER_VENDOR\Assert\Assertion; |
| 17 |
use WP_DEFENDER_VENDOR\Base64Url\Base64Url; |
| 18 |
use InvalidArgumentException; |
| 19 |
use JsonSerializable; |
| 20 |
use WP_DEFENDER_VENDOR\Ramsey\Uuid\Uuid; |
| 21 |
use WP_DEFENDER_VENDOR\Ramsey\Uuid\UuidInterface; |
| 22 |
use function WP_DEFENDER_VENDOR\Safe\base64_decode; |
| 23 |
use function WP_DEFENDER_VENDOR\Safe\sprintf; |
| 24 |
use Throwable; |
| 25 |
use WP_DEFENDER_VENDOR\Webauthn\TrustPath\TrustPath; |
| 26 |
use WP_DEFENDER_VENDOR\Webauthn\TrustPath\TrustPathLoader; |
| 27 |
|
| 28 |
/** |
| 29 |
* @see https://www.w3.org/TR/webauthn/#iface-pkcredential |
| 30 |
*/ |
| 31 |
class PublicKeyCredentialSource implements JsonSerializable |
| 32 |
{ |
| 33 |
/** |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $publicKeyCredentialId; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
protected $type; |
| 42 |
|
| 43 |
/** |
| 44 |
* @var string[] |
| 45 |
*/ |
| 46 |
protected $transports; |
| 47 |
|
| 48 |
/** |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
protected $attestationType; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var TrustPath |
| 55 |
*/ |
| 56 |
protected $trustPath; |
| 57 |
|
| 58 |
/** |
| 59 |
* @var UuidInterface |
| 60 |
*/ |
| 61 |
protected $aaguid; |
| 62 |
|
| 63 |
/** |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
protected $credentialPublicKey; |
| 67 |
|
| 68 |
/** |
| 69 |
* @var string |
| 70 |
*/ |
| 71 |
protected $userHandle; |
| 72 |
|
| 73 |
/** |
| 74 |
* @var int |
| 75 |
*/ |
| 76 |
protected $counter; |
| 77 |
|
| 78 |
/** |
| 79 |
* @var array|null |
| 80 |
*/ |
| 81 |
protected $otherUI; |
| 82 |
|
| 83 |
/** |
| 84 |
* @param string[] $transports |
| 85 |
*/ |
| 86 |
public function __construct(string $publicKeyCredentialId, string $type, array $transports, string $attestationType, TrustPath $trustPath, UuidInterface $aaguid, string $credentialPublicKey, string $userHandle, int $counter, ?array $otherUI = null) |
| 87 |
{ |
| 88 |
$this->publicKeyCredentialId = $publicKeyCredentialId; |
| 89 |
$this->type = $type; |
| 90 |
$this->transports = $transports; |
| 91 |
$this->aaguid = $aaguid; |
| 92 |
$this->credentialPublicKey = $credentialPublicKey; |
| 93 |
$this->userHandle = $userHandle; |
| 94 |
$this->counter = $counter; |
| 95 |
$this->attestationType = $attestationType; |
| 96 |
$this->trustPath = $trustPath; |
| 97 |
$this->otherUI = $otherUI; |
| 98 |
} |
| 99 |
|
| 100 |
public function getPublicKeyCredentialId(): string |
| 101 |
{ |
| 102 |
return $this->publicKeyCredentialId; |
| 103 |
} |
| 104 |
|
| 105 |
public function getPublicKeyCredentialDescriptor(): PublicKeyCredentialDescriptor |
| 106 |
{ |
| 107 |
return new PublicKeyCredentialDescriptor( |
| 108 |
$this->type, |
| 109 |
$this->publicKeyCredentialId, |
| 110 |
$this->transports |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
public function getAttestationType(): string |
| 115 |
{ |
| 116 |
return $this->attestationType; |
| 117 |
} |
| 118 |
|
| 119 |
public function getTrustPath(): TrustPath |
| 120 |
{ |
| 121 |
return $this->trustPath; |
| 122 |
} |
| 123 |
|
| 124 |
public function getAttestedCredentialData(): AttestedCredentialData |
| 125 |
{ |
| 126 |
return new AttestedCredentialData( |
| 127 |
$this->aaguid, |
| 128 |
$this->publicKeyCredentialId, |
| 129 |
$this->credentialPublicKey |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
public function getType(): string |
| 134 |
{ |
| 135 |
return $this->type; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @return string[] |
| 140 |
*/ |
| 141 |
public function getTransports(): array |
| 142 |
{ |
| 143 |
return $this->transports; |
| 144 |
} |
| 145 |
|
| 146 |
public function getAaguid(): UuidInterface |
| 147 |
{ |
| 148 |
return $this->aaguid; |
| 149 |
} |
| 150 |
|
| 151 |
public function getCredentialPublicKey(): string |
| 152 |
{ |
| 153 |
return $this->credentialPublicKey; |
| 154 |
} |
| 155 |
|
| 156 |
public function getUserHandle(): string |
| 157 |
{ |
| 158 |
return $this->userHandle; |
| 159 |
} |
| 160 |
|
| 161 |
public function getCounter(): int |
| 162 |
{ |
| 163 |
return $this->counter; |
| 164 |
} |
| 165 |
|
| 166 |
public function setCounter(int $counter): void |
| 167 |
{ |
| 168 |
$this->counter = $counter; |
| 169 |
} |
| 170 |
|
| 171 |
public function getOtherUI(): ?array |
| 172 |
{ |
| 173 |
return $this->otherUI; |
| 174 |
} |
| 175 |
|
| 176 |
public function setOtherUI(?array $otherUI): self |
| 177 |
{ |
| 178 |
$this->otherUI = $otherUI; |
| 179 |
|
| 180 |
return $this; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* @param mixed[] $data |
| 185 |
*/ |
| 186 |
public static function createFromArray(array $data): self |
| 187 |
{ |
| 188 |
$keys = array_keys(get_class_vars(self::class)); |
| 189 |
foreach ($keys as $key) { |
| 190 |
if ('otherUI' === $key) { |
| 191 |
continue; |
| 192 |
} |
| 193 |
Assertion::keyExists($data, $key, sprintf('The parameter "%s" is missing', $key)); |
| 194 |
} |
| 195 |
switch (true) { |
| 196 |
case 36 === mb_strlen($data['aaguid'], '8bit'): |
| 197 |
$uuid = Uuid::fromString($data['aaguid']); |
| 198 |
break; |
| 199 |
default: // Kept for compatibility with old format |
| 200 |
$decoded = base64_decode($data['aaguid'], true); |
| 201 |
$uuid = Uuid::fromBytes($decoded); |
| 202 |
} |
| 203 |
|
| 204 |
try { |
| 205 |
return new self( |
| 206 |
Base64Url::decode($data['publicKeyCredentialId']), |
| 207 |
$data['type'], |
| 208 |
$data['transports'], |
| 209 |
$data['attestationType'], |
| 210 |
TrustPathLoader::loadTrustPath($data['trustPath']), |
| 211 |
$uuid, |
| 212 |
Base64Url::decode($data['credentialPublicKey']), |
| 213 |
Base64Url::decode($data['userHandle']), |
| 214 |
$data['counter'], |
| 215 |
$data['otherUI'] ?? null |
| 216 |
); |
| 217 |
} catch (Throwable $throwable) { |
| 218 |
throw new InvalidArgumentException('Unable to load the data', $throwable->getCode(), $throwable); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* @return mixed[] |
| 224 |
*/ |
| 225 |
public function jsonSerialize(): array |
| 226 |
{ |
| 227 |
return [ |
| 228 |
'publicKeyCredentialId' => Base64Url::encode($this->publicKeyCredentialId), |
| 229 |
'type' => $this->type, |
| 230 |
'transports' => $this->transports, |
| 231 |
'attestationType' => $this->attestationType, |
| 232 |
'trustPath' => $this->trustPath->jsonSerialize(), |
| 233 |
'aaguid' => $this->aaguid->toString(), |
| 234 |
'credentialPublicKey' => Base64Url::encode($this->credentialPublicKey), |
| 235 |
'userHandle' => Base64Url::encode($this->userHandle), |
| 236 |
'counter' => $this->counter, |
| 237 |
'otherUI' => $this->otherUI, |
| 238 |
]; |
| 239 |
} |
| 240 |
} |
| 241 |
|