| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use function is_array; |
| 9 |
use function is_string; |
| 10 |
use const JSON_THROW_ON_ERROR; |
| 11 |
use ParagonIE\ConstantTime\Base64UrlSafe; |
| 12 |
use function sprintf; |
| 13 |
use Webauthn\Exception\InvalidDataException; |
| 14 |
|
| 15 |
class CollectedClientData |
| 16 |
{ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public readonly array $data; |
| 21 |
|
| 22 |
public readonly string $type; |
| 23 |
|
| 24 |
public readonly string $challenge; |
| 25 |
|
| 26 |
public readonly string $origin; |
| 27 |
|
| 28 |
public readonly null|string $topOrigin; |
| 29 |
|
| 30 |
public readonly bool $crossOrigin; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function __construct( |
| 36 |
public readonly string $rawData, |
| 37 |
array $data |
| 38 |
) { |
| 39 |
$type = $data['type'] ?? ''; |
| 40 |
(is_string($type) && $type !== '') || throw InvalidDataException::create( |
| 41 |
$data, |
| 42 |
'Invalid parameter "type". Shall be a non-empty string.' |
| 43 |
); |
| 44 |
$this->type = $type; |
| 45 |
|
| 46 |
$challenge = $data['challenge'] ?? ''; |
| 47 |
is_string($challenge) || throw InvalidDataException::create( |
| 48 |
$data, |
| 49 |
'Invalid parameter "challenge". Shall be a string.' |
| 50 |
); |
| 51 |
$challenge = Base64UrlSafe::decodeNoPadding($challenge); |
| 52 |
$challenge !== '' || throw InvalidDataException::create( |
| 53 |
$data, |
| 54 |
'Invalid parameter "challenge". Shall not be empty.' |
| 55 |
); |
| 56 |
$this->challenge = $challenge; |
| 57 |
|
| 58 |
$origin = $data['origin'] ?? ''; |
| 59 |
(is_string($origin) && $origin !== '') || throw InvalidDataException::create( |
| 60 |
$data, |
| 61 |
'Invalid parameter "origin". Shall be a non-empty string.' |
| 62 |
); |
| 63 |
$this->origin = $origin; |
| 64 |
|
| 65 |
|
| 66 |
$topOrigin = $data['topOrigin'] ?? null; |
| 67 |
$this->topOrigin = $topOrigin; |
| 68 |
|
| 69 |
$crossOrigin = $data['crossOrigin'] ?? false; |
| 70 |
$this->crossOrigin = $crossOrigin; |
| 71 |
|
| 72 |
$tokenBinding = $data['tokenBinding'] ?? null; |
| 73 |
$tokenBinding === null || is_array($tokenBinding) || throw InvalidDataException::create( |
| 74 |
$data, |
| 75 |
'Invalid parameter "tokenBinding". Shall be an object or .' |
| 76 |
); |
| 77 |
|
| 78 |
$this->data = $data; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static function create(string $rawData, array $data): self |
| 85 |
{ |
| 86 |
return new self($rawData, $data); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static function createFormJson(string $data): self |
| 93 |
{ |
| 94 |
$rawData = Base64UrlSafe::decodeNoPadding($data); |
| 95 |
$json = json_decode($rawData, true, flags: JSON_THROW_ON_ERROR); |
| 96 |
is_array($json) || throw InvalidDataException::create($data, 'Invalid JSON data.'); |
| 97 |
|
| 98 |
return self::create($rawData, $json); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function all(): array |
| 105 |
{ |
| 106 |
return array_keys($this->data); |
| 107 |
} |
| 108 |
|
| 109 |
public function has(string $key): bool |
| 110 |
{ |
| 111 |
return array_key_exists($key, $this->data); |
| 112 |
} |
| 113 |
|
| 114 |
public function get(string $key): mixed |
| 115 |
{ |
| 116 |
if (! $this->has($key)) { |
| 117 |
throw InvalidDataException::create($this->data, sprintf('The key "%s" is missing', $key)); |
| 118 |
} |
| 119 |
|
| 120 |
return $this->data[$key]; |
| 121 |
} |
| 122 |
} |