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
/
ResponseTrait.php
62 lines (51 loc) · 1.47 KB
/
ResponseTrait.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
<?php
declare(strict_types=1);
namespace Firehed\U2F;
use Firehed\U2F\InvalidDataException as IDE;
/**
* @deprecated
* @internal
*/
trait ResponseTrait
{
use KeyHandleTrait;
/** @var ClientData */
protected $clientData;
/** @var string (binary) */
private $signature = '';
public function getSignature(): string
{
return $this->signature;
}
protected function setSignature(string $signature): self
{
$this->signature = $signature;
return $this;
}
public static function fromJson(string $json): self
{
$data = json_decode($json, true);
if (json_last_error() !== \JSON_ERROR_NONE) {
throw new IDE(IDE::MALFORMED_DATA, 'JSON');
}
if (isset($data['errorCode'])) {
throw new ClientErrorException($data['errorCode']);
}
$ret = new self;
$ret->validateKeyInArray('clientData', $data);
$ret->clientData = ClientData::fromJson(fromBase64Web($data['clientData']));
return $ret->parseResponse($data);
}
abstract protected function parseResponse(array $response): self;
/** @param array<string, mixed> $data */
private function validateKeyInArray(string $key, array $data): bool
{
if (!isset($data[$key])) {
throw new IDE(IDE::MISSING_KEY, $key);
}
if (!is_string($data[$key])) {
throw new IDE(IDE::MALFORMED_DATA, $key);
}
return true;
}
}