| 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\AttestationStatement; |
| 15 |
|
| 16 |
use function array_key_exists; |
| 17 |
use WP_DEFENDER_VENDOR\Assert\Assertion; |
| 18 |
use JsonSerializable; |
| 19 |
use function WP_DEFENDER_VENDOR\Safe\sprintf; |
| 20 |
use WP_DEFENDER_VENDOR\Webauthn\TrustPath\TrustPath; |
| 21 |
use WP_DEFENDER_VENDOR\Webauthn\TrustPath\TrustPathLoader; |
| 22 |
|
| 23 |
class AttestationStatement implements JsonSerializable |
| 24 |
{ |
| 25 |
public const TYPE_NONE = 'none'; |
| 26 |
public const TYPE_BASIC = 'basic'; |
| 27 |
public const TYPE_SELF = 'self'; |
| 28 |
public const TYPE_ATTCA = 'attca'; |
| 29 |
public const TYPE_ECDAA = 'ecdaa'; |
| 30 |
public const TYPE_ANONCA = 'anonca'; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $fmt; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var mixed[] |
| 39 |
*/ |
| 40 |
private $attStmt; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var TrustPath |
| 44 |
*/ |
| 45 |
private $trustPath; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
private $type; |
| 51 |
|
| 52 |
/** |
| 53 |
* @param mixed[] $attStmt |
| 54 |
*/ |
| 55 |
public function __construct(string $fmt, array $attStmt, string $type, TrustPath $trustPath) |
| 56 |
{ |
| 57 |
$this->fmt = $fmt; |
| 58 |
$this->attStmt = $attStmt; |
| 59 |
$this->type = $type; |
| 60 |
$this->trustPath = $trustPath; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @param mixed[] $attStmt |
| 65 |
*/ |
| 66 |
public static function createNone(string $fmt, array $attStmt, TrustPath $trustPath): self |
| 67 |
{ |
| 68 |
return new self($fmt, $attStmt, self::TYPE_NONE, $trustPath); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @param mixed[] $attStmt |
| 73 |
*/ |
| 74 |
public static function createBasic(string $fmt, array $attStmt, TrustPath $trustPath): self |
| 75 |
{ |
| 76 |
return new self($fmt, $attStmt, self::TYPE_BASIC, $trustPath); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param mixed[] $attStmt |
| 81 |
*/ |
| 82 |
public static function createSelf(string $fmt, array $attStmt, TrustPath $trustPath): self |
| 83 |
{ |
| 84 |
return new self($fmt, $attStmt, self::TYPE_SELF, $trustPath); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @param mixed[] $attStmt |
| 89 |
*/ |
| 90 |
public static function createAttCA(string $fmt, array $attStmt, TrustPath $trustPath): self |
| 91 |
{ |
| 92 |
return new self($fmt, $attStmt, self::TYPE_ATTCA, $trustPath); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @param mixed[] $attStmt |
| 97 |
*/ |
| 98 |
public static function createEcdaa(string $fmt, array $attStmt, TrustPath $trustPath): self |
| 99 |
{ |
| 100 |
return new self($fmt, $attStmt, self::TYPE_ECDAA, $trustPath); |
| 101 |
} |
| 102 |
|
| 103 |
public static function createAnonymizationCA(string $fmt, array $attStmt, TrustPath $trustPath): self |
| 104 |
{ |
| 105 |
return new self($fmt, $attStmt, self::TYPE_ANONCA, $trustPath); |
| 106 |
} |
| 107 |
|
| 108 |
public function getFmt(): string |
| 109 |
{ |
| 110 |
return $this->fmt; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @return mixed[] |
| 115 |
*/ |
| 116 |
public function getAttStmt(): array |
| 117 |
{ |
| 118 |
return $this->attStmt; |
| 119 |
} |
| 120 |
|
| 121 |
public function has(string $key): bool |
| 122 |
{ |
| 123 |
return array_key_exists($key, $this->attStmt); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @return mixed |
| 128 |
*/ |
| 129 |
public function get(string $key) |
| 130 |
{ |
| 131 |
Assertion::true($this->has($key), sprintf('The attestation statement has no key "%s".', $key)); |
| 132 |
|
| 133 |
return $this->attStmt[$key]; |
| 134 |
} |
| 135 |
|
| 136 |
public function getTrustPath(): TrustPath |
| 137 |
{ |
| 138 |
return $this->trustPath; |
| 139 |
} |
| 140 |
|
| 141 |
public function getType(): string |
| 142 |
{ |
| 143 |
return $this->type; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @param mixed[] $data |
| 148 |
*/ |
| 149 |
public static function createFromArray(array $data): self |
| 150 |
{ |
| 151 |
foreach (['fmt', 'attStmt', 'trustPath', 'type'] as $key) { |
| 152 |
Assertion::keyExists($data, $key, sprintf('The key "%s" is missing', $key)); |
| 153 |
} |
| 154 |
|
| 155 |
return new self( |
| 156 |
$data['fmt'], |
| 157 |
$data['attStmt'], |
| 158 |
$data['type'], |
| 159 |
TrustPathLoader::loadTrustPath($data['trustPath']) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @return mixed[] |
| 165 |
*/ |
| 166 |
public function jsonSerialize(): array |
| 167 |
{ |
| 168 |
return [ |
| 169 |
'fmt' => $this->fmt, |
| 170 |
'attStmt' => $this->attStmt, |
| 171 |
'trustPath' => $this->trustPath->jsonSerialize(), |
| 172 |
'type' => $this->type, |
| 173 |
]; |
| 174 |
} |
| 175 |
} |
| 176 |
|