This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ClientData.php
92 lines (80 loc) · 2.44 KB
/
ClientData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace Firehed\U2F;
use Firehed\U2F\InvalidDataException as IDE;
/**
* @deprecated
* @internal
*/
class ClientData
{
use ChallengeTrait;
/** @var string */
private $originalJson;
/** @var string */
private $cid_pubkey;
/** @var string */
private $origin;
/** @var string */
private $typ;
public static function fromJson(string $json): ClientData
{
$data = json_decode($json, true);
if (json_last_error() !== \JSON_ERROR_NONE) {
throw new IDE(IDE::MALFORMED_DATA, 'json');
}
$ret = new self;
$ret->setType($ret->validateKey('typ', $data));
$ret->setChallenge($ret->validateKey('challenge', $data));
$ret->origin = $ret->validateKey('origin', $data);
// This field is optional
if (isset($data['cid_pubkey'])) {
$ret->cid_pubkey = $data['cid_pubkey'];
}
$ret->originalJson = $json;
return $ret;
}
public function getApplicationParameter(): string
{
return hash('sha256', $this->origin, true);
}
/**
* Checks the 'typ' field against the allowed types in the U2F spec (sec.
* 7.1)
* @param string $type the 'typ' value
* @return $this
* @throws InvalidDataException if a non-conforming value is provided
*/
private function setType(string $type): self
{
switch ($type) {
case 'navigator.id.getAssertion': // fall through
case 'navigator.id.finishEnrollment':
break;
default:
throw new IDE(IDE::MALFORMED_DATA, 'typ');
}
$this->typ = $type;
return $this;
}
/**
* Checks for the presence of $key in $data. Returns the value if found,
* throws an InvalidDataException if missing
* @param string $key The array key to check
* @param array<string, string> $data The array to check in
* @return string The data, if present
* @throws InvalidDataException if not prsent
*/
private function validateKey(string $key, array $data): string
{
if (!array_key_exists($key, $data)) {
throw new IDE(IDE::MISSING_KEY, $key);
}
return $data[$key];
}
// Returns the SHA256 hash of this object per the raw message formats spec
public function getChallengeParameter(): string
{
return hash('sha256', $this->originalJson, true);
}
}