|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Mocker; |
|
6
|
|
|
|
|
7
|
|
|
use Firehed\U2F\RegisterRequest; |
|
8
|
|
|
use Firehed\U2F\SignRequest; |
|
9
|
|
|
use LM\AuthAbstractor\Configuration\IApplicationConfiguration; |
|
10
|
|
|
use LM\AuthAbstractor\Implementation\U2fRegistration; |
|
11
|
|
|
use LM\AuthAbstractor\Model\IU2fRegistration; |
|
12
|
|
|
use LM\Common\Model\ArrayObject; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This class is used for unit testing U2F. It retrieves and instantiates |
|
16
|
|
|
* IU2FRegistration objects. |
|
17
|
|
|
* |
|
18
|
|
|
* @see \LM\AuthAbstractor\Model\IU2fRegistration |
|
19
|
|
|
*/ |
|
20
|
|
|
class U2fMocker |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var IU2fRegistration[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $u2fRegistrations; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param IApplicationConfiguration $appConfig The configuration of the |
|
29
|
|
|
* application. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(IApplicationConfiguration $appConfig) |
|
32
|
|
|
{ |
|
33
|
|
|
$items = json_decode(file_get_contents($appConfig->getLibdir().'/u2f_registrations.json'), true); |
|
34
|
|
|
$this->u2fRegistrations = $this->createFromArray($items); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @internal |
|
39
|
|
|
* @todo $items is mutated! |
|
40
|
|
|
* @todo Use ArrayObject instead. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function createFromArray(array $items, array $list = []): array |
|
43
|
|
|
{ |
|
44
|
|
|
if (0 !== count($items)) { |
|
45
|
|
|
$item = array_pop($items); |
|
46
|
|
|
$list[$item['id']]['u2fRegistration'] = new U2fRegistration( |
|
47
|
|
|
$item['u2fRegistration']['attestationCertificate'], |
|
48
|
|
|
$item['u2fRegistration']['counter'], |
|
49
|
|
|
$item['u2fRegistration']['keyHandle'], |
|
50
|
|
|
$item['u2fRegistration']['publicKey'] |
|
51
|
|
|
); |
|
52
|
|
|
if (isset($item['registerRequest'])) { |
|
53
|
|
|
$list[$item['id']]['registerRequest'] = (new RegisterRequest()) |
|
54
|
|
|
->setAppId($item['registerRequest']['appId']) |
|
55
|
|
|
->setChallenge($item['registerRequest']['challenge']) |
|
56
|
|
|
; |
|
57
|
|
|
} |
|
58
|
|
|
if (isset($item['registerResponse'])) { |
|
59
|
|
|
$list[$item['id']]['registerResponseStr'] = json_encode($item['registerResponse']); |
|
60
|
|
|
} |
|
61
|
|
|
if (isset($item['u2fAuthentications'])) { |
|
62
|
|
|
$list[$item['id']]['u2fAuthentications'] = []; |
|
63
|
|
|
foreach ($item['u2fAuthentications'] as $u2fAuth) { |
|
64
|
|
|
$signRequests = array_map( |
|
65
|
|
|
function ($object) { |
|
66
|
|
|
return (new SignRequest()) |
|
67
|
|
|
->setAppId($object['appId']) |
|
68
|
|
|
->setChallenge($object['challenge']) |
|
69
|
|
|
->setKeyHandle(base64_decode($object['keyHandle'], true)) |
|
70
|
|
|
; |
|
71
|
|
|
}, |
|
72
|
|
|
$u2fAuth['signRequests'] |
|
73
|
|
|
); |
|
74
|
|
|
$list[$item['id']]['u2fAuthentications'][] = [ |
|
75
|
|
|
'signRequests' => $signRequests, |
|
76
|
|
|
'signResponse' => $u2fAuth['signResponse'], |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->createFromArray($items, $list); |
|
82
|
|
|
} else { |
|
83
|
|
|
return $list; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @api |
|
89
|
|
|
* @param int $id The index of the U2F registration. |
|
90
|
|
|
* @return mixed[] An array. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function get(int $id): array |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->u2fRegistrations[$id]; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @api |
|
99
|
|
|
* @return array An array of U2F registrations and related data. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getU2fRegistrations(): array |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->u2fRegistrations; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @api |
|
108
|
|
|
* @return IU2fRegistration[] An array of U2F registrations only. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getU2fRegistrationsOnly(): array |
|
111
|
|
|
{ |
|
112
|
|
|
return array_map( |
|
113
|
|
|
function ($item) { |
|
114
|
|
|
return $item['u2fRegistration']; |
|
115
|
|
|
}, |
|
116
|
|
|
$this->u2fRegistrations |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|