| 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 function array_key_exists; |
| 17 |
use Assert\Assertion; |
| 18 |
use Base64Url\Base64Url; |
| 19 |
use InvalidArgumentException; |
| 20 |
use function Safe\json_decode; |
| 21 |
use function Safe\sprintf; |
| 22 |
use Webauthn\TokenBinding\TokenBinding; |
| 23 |
|
| 24 |
class CollectedClientData |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private $rawData; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var mixed[] |
| 33 |
*/ |
| 34 |
private $data; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private $type; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
private $challenge; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
private $origin; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var mixed[]|null |
| 53 |
*/ |
| 54 |
private $tokenBinding; |
| 55 |
|
| 56 |
/** |
| 57 |
* @param mixed[] $data |
| 58 |
*/ |
| 59 |
public function __construct(string $rawData, array $data) |
| 60 |
{ |
| 61 |
$this->type = $this->findData($data, 'type'); |
| 62 |
$this->challenge = $this->findData($data, 'challenge', true, true); |
| 63 |
$this->origin = $this->findData($data, 'origin'); |
| 64 |
$this->tokenBinding = $this->findData($data, 'tokenBinding', false); |
| 65 |
$this->rawData = $rawData; |
| 66 |
$this->data = $data; |
| 67 |
} |
| 68 |
|
| 69 |
public static function createFormJson(string $data): self |
| 70 |
{ |
| 71 |
$rawData = Base64Url::decode($data); |
| 72 |
$json = json_decode($rawData, true); |
| 73 |
Assertion::isArray($json, 'Invalid collected client data'); |
| 74 |
|
| 75 |
return new self($rawData, $json); |
| 76 |
} |
| 77 |
|
| 78 |
public function getType(): string |
| 79 |
{ |
| 80 |
return $this->type; |
| 81 |
} |
| 82 |
|
| 83 |
public function getChallenge(): string |
| 84 |
{ |
| 85 |
return $this->challenge; |
| 86 |
} |
| 87 |
|
| 88 |
public function getOrigin(): string |
| 89 |
{ |
| 90 |
return $this->origin; |
| 91 |
} |
| 92 |
|
| 93 |
public function getTokenBinding(): ?TokenBinding |
| 94 |
{ |
| 95 |
return null === $this->tokenBinding ? null : TokenBinding::createFormArray($this->tokenBinding); |
| 96 |
} |
| 97 |
|
| 98 |
public function getRawData(): string |
| 99 |
{ |
| 100 |
return $this->rawData; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return string[] |
| 105 |
*/ |
| 106 |
public function all(): array |
| 107 |
{ |
| 108 |
return array_keys($this->data); |
| 109 |
} |
| 110 |
|
| 111 |
public function has(string $key): bool |
| 112 |
{ |
| 113 |
return array_key_exists($key, $this->data); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @return mixed |
| 118 |
*/ |
| 119 |
public function get(string $key) |
| 120 |
{ |
| 121 |
if (!$this->has($key)) { |
| 122 |
throw new InvalidArgumentException(sprintf('The key "%s" is missing', $key)); |
| 123 |
} |
| 124 |
|
| 125 |
return $this->data[$key]; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @param mixed[] $json |
| 130 |
* |
| 131 |
* @return mixed|null |
| 132 |
*/ |
| 133 |
private function findData(array $json, string $key, bool $isRequired = true, bool $isB64 = false) |
| 134 |
{ |
| 135 |
if (!array_key_exists($key, $json)) { |
| 136 |
if ($isRequired) { |
| 137 |
throw new InvalidArgumentException(sprintf('The key "%s" is missing', $key)); |
| 138 |
} |
| 139 |
|
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
return $isB64 ? Base64Url::decode($json[$key]) : $json[$key]; |
| 144 |
} |
| 145 |
} |
| 146 |
|