|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\U2f; |
|
6
|
|
|
|
|
7
|
|
|
use LM\AuthAbstractor\Factory\U2fRegistrationFactory; |
|
8
|
|
|
use LM\AuthAbstractor\Model\IU2fRegistration; |
|
9
|
|
|
use LM\AuthAbstractor\Model\U2fRegistrationRequest; |
|
10
|
|
|
use LM\Common\Model\ArrayObject; |
|
11
|
|
|
use Firehed\U2F\RegisterRequest; |
|
12
|
|
|
use Firehed\U2F\RegisterResponse; |
|
13
|
|
|
use Firehed\U2F\SignRequest; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This class is used for generating U2F register requests and processing their |
|
17
|
|
|
* responses. |
|
18
|
|
|
* |
|
19
|
|
|
* @internal |
|
20
|
|
|
*/ |
|
21
|
|
|
class U2fRegistrationManager |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var U2fRegistrationFactory */ |
|
24
|
|
|
private $u2fRegistrationFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** @var U2fServerGenerator */ |
|
27
|
|
|
private $u2fServerGenerator; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct( |
|
30
|
|
|
U2fRegistrationFactory $u2fRegistrationFactory, |
|
31
|
|
|
U2fServerGenerator $u2fServerGenerator |
|
32
|
|
|
) { |
|
33
|
|
|
$this->u2fRegistrationFactory = $u2fRegistrationFactory; |
|
34
|
|
|
$this->u2fServerGenerator = $u2fServerGenerator; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Generates a new U2F register requset. |
|
39
|
|
|
* |
|
40
|
|
|
* @param null|ArrayObject $registrations An array of IU2fRegistration. |
|
41
|
|
|
* @todo Use an ArrayObject instead? |
|
42
|
|
|
*/ |
|
43
|
|
|
public function generate(?ArrayObject $registrations = null): U2fRegistrationRequest |
|
44
|
|
|
{ |
|
45
|
|
|
$server = $this |
|
46
|
|
|
->u2fServerGenerator |
|
47
|
|
|
->getServer() |
|
48
|
|
|
; |
|
49
|
|
|
$request = $server->generateRegisterRequest(); |
|
50
|
|
|
|
|
51
|
|
|
$signRequests = null; |
|
52
|
|
|
if (null !== $registrations) { |
|
53
|
|
|
$firehedRegs = array_map( |
|
54
|
|
|
[$this->u2fRegistrationFactory, 'toFirehed'], |
|
55
|
|
|
$registrations->toArray(IU2fRegistration::class) |
|
56
|
|
|
); |
|
57
|
|
|
$signRequests = new ArrayObject( |
|
|
|
|
|
|
58
|
|
|
$server->generateSignRequests($firehedRegs), |
|
59
|
|
|
SignRequest::class |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return new U2fRegistrationRequest($request, $signRequests); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Verifies and returns a new U2F registration from a response. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $u2fKeyResponse The response from the U2F token. |
|
70
|
|
|
* @param RegisterRequest $request The U2F register request. |
|
71
|
|
|
* @return IU2fRegistration The new U2F registration. |
|
72
|
|
|
* @todo Rename $u2fKeyResponse to $u2fTokenResponse |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getU2fRegistrationFromResponse( |
|
75
|
|
|
string $u2fKeyResponse, |
|
76
|
|
|
RegisterRequest $request |
|
77
|
|
|
): IU2fRegistration { |
|
78
|
|
|
$server = $this |
|
79
|
|
|
->u2fServerGenerator |
|
80
|
|
|
->getServer() |
|
81
|
|
|
; |
|
82
|
|
|
$server |
|
83
|
|
|
->setRegisterRequest($request) |
|
84
|
|
|
; |
|
85
|
|
|
$response = RegisterResponse::fromJson($u2fKeyResponse); |
|
86
|
|
|
$registration = $server->register($response); |
|
87
|
|
|
|
|
88
|
|
|
return $this |
|
89
|
|
|
->u2fRegistrationFactory |
|
90
|
|
|
->fromFirehed($registration) |
|
91
|
|
|
; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|