|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2018 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 U2FAuthentication\Fido2; |
|
15
|
|
|
|
|
16
|
|
|
use Assert\Assertion; |
|
17
|
|
|
|
|
18
|
|
|
class PublicKeyCredentialDescriptor implements \JsonSerializable |
|
19
|
|
|
{ |
|
20
|
|
|
public const CREDENTIAL_TYPE_PUBLIC_KEY = 'public-key'; |
|
21
|
|
|
|
|
22
|
|
|
public const AUTHENTICATOR_TRANSPORT_USB = 'usb'; |
|
23
|
|
|
public const AUTHENTICATOR_TRANSPORT_NFC = 'nfc'; |
|
24
|
|
|
public const AUTHENTICATOR_TRANSPORT_BLE = 'ble'; |
|
25
|
|
|
public const AUTHENTICATOR_TRANSPORT_INTERNAL = 'internal'; |
|
26
|
|
|
|
|
27
|
|
|
private $type; |
|
28
|
|
|
|
|
29
|
|
|
private $id; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $transports; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string[] $transports |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(string $type, string $id, array $transports = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->type = $type; |
|
42
|
|
|
$this->id = $id; |
|
43
|
|
|
$this->transports = $transports; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getType(): string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->type; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getId(): string |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->id; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string[] |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getTransports(): array |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->transports; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public static function createFromJson(string $json): self |
|
65
|
|
|
{ |
|
66
|
|
|
$data = \Safe\json_decode($json, true); |
|
67
|
|
|
Assertion::isArray($data, 'Invalid input.'); |
|
68
|
|
|
Assertion::keyExists($data, 'type', 'Invalid input.'); |
|
69
|
|
|
Assertion::keyExists($data, 'id', 'Invalid input.'); |
|
70
|
|
|
|
|
71
|
|
|
return new self( |
|
72
|
|
|
$data['type'], |
|
73
|
|
|
\Safe\base64_decode($data['id'], true), |
|
74
|
|
|
$data['transports'] ?? [] |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function jsonSerialize() |
|
79
|
|
|
{ |
|
80
|
|
|
$json = [ |
|
81
|
|
|
'type' => $this->type, |
|
82
|
|
|
'id' => base64_encode($this->id), |
|
83
|
|
|
]; |
|
84
|
|
|
if (!empty($this->transports)) { |
|
85
|
|
|
$json['transports'] = $this->transports; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $json; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|