| 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 JsonSerializable; |
| 18 |
use WP_DEFENDER_VENDOR\Ramsey\Uuid\Uuid; |
| 19 |
use WP_DEFENDER_VENDOR\Ramsey\Uuid\UuidInterface; |
| 20 |
use function WP_DEFENDER_VENDOR\Safe\base64_decode; |
| 21 |
|
| 22 |
/** |
| 23 |
* @see https://www.w3.org/TR/webauthn/#sec-attested-credential-data |
| 24 |
*/ |
| 25 |
class AttestedCredentialData implements JsonSerializable |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var UuidInterface |
| 29 |
*/ |
| 30 |
private $aaguid; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $credentialId; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var string|null |
| 39 |
*/ |
| 40 |
private $credentialPublicKey; |
| 41 |
|
| 42 |
public function __construct(UuidInterface $aaguid, string $credentialId, ?string $credentialPublicKey) |
| 43 |
{ |
| 44 |
$this->aaguid = $aaguid; |
| 45 |
$this->credentialId = $credentialId; |
| 46 |
$this->credentialPublicKey = $credentialPublicKey; |
| 47 |
} |
| 48 |
|
| 49 |
public function getAaguid(): UuidInterface |
| 50 |
{ |
| 51 |
return $this->aaguid; |
| 52 |
} |
| 53 |
|
| 54 |
public function setAaguid(UuidInterface $aaguid): void |
| 55 |
{ |
| 56 |
$this->aaguid = $aaguid; |
| 57 |
} |
| 58 |
|
| 59 |
public function getCredentialId(): string |
| 60 |
{ |
| 61 |
return $this->credentialId; |
| 62 |
} |
| 63 |
|
| 64 |
public function getCredentialPublicKey(): ?string |
| 65 |
{ |
| 66 |
return $this->credentialPublicKey; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @param mixed[] $json |
| 71 |
*/ |
| 72 |
public static function createFromArray(array $json): self |
| 73 |
{ |
| 74 |
Assertion::keyExists($json, 'aaguid', 'Invalid input. "aaguid" is missing.'); |
| 75 |
Assertion::keyExists($json, 'credentialId', 'Invalid input. "credentialId" is missing.'); |
| 76 |
switch (true) { |
| 77 |
case 36 === mb_strlen($json['aaguid'], '8bit'): |
| 78 |
$uuid = Uuid::fromString($json['aaguid']); |
| 79 |
break; |
| 80 |
default: // Kept for compatibility with old format |
| 81 |
$decoded = base64_decode($json['aaguid'], true); |
| 82 |
$uuid = Uuid::fromBytes($decoded); |
| 83 |
} |
| 84 |
$credentialId = base64_decode($json['credentialId'], true); |
| 85 |
|
| 86 |
$credentialPublicKey = null; |
| 87 |
if (isset($json['credentialPublicKey'])) { |
| 88 |
$credentialPublicKey = base64_decode($json['credentialPublicKey'], true); |
| 89 |
} |
| 90 |
|
| 91 |
return new self( |
| 92 |
$uuid, |
| 93 |
$credentialId, |
| 94 |
$credentialPublicKey |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @return mixed[] |
| 100 |
*/ |
| 101 |
public function jsonSerialize(): array |
| 102 |
{ |
| 103 |
$result = [ |
| 104 |
'aaguid' => $this->aaguid->toString(), |
| 105 |
'credentialId' => base64_encode($this->credentialId), |
| 106 |
]; |
| 107 |
if (null !== $this->credentialPublicKey) { |
| 108 |
$result['credentialPublicKey'] = base64_encode($this->credentialPublicKey); |
| 109 |
} |
| 110 |
|
| 111 |
return $result; |
| 112 |
} |
| 113 |
} |
| 114 |
|