1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\U2f; |
6
|
|
|
|
7
|
|
|
use Firehed\U2F\SignRequest; |
8
|
|
|
use Firehed\U2F\SignResponse; |
9
|
|
|
use LM\AuthAbstractor\Exception\NoRegisteredU2fTokenException; |
10
|
|
|
use LM\AuthAbstractor\Factory\U2fRegistrationFactory; |
11
|
|
|
use LM\Common\Model\ArrayObject; |
12
|
|
|
use LM\AuthAbstractor\Model\IU2fRegistration; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class is used for generating U2F sign requests, and for processing their |
16
|
|
|
* responses. |
17
|
|
|
* |
18
|
|
|
* @internal |
19
|
|
|
*/ |
20
|
|
|
class U2fAuthenticationManager |
21
|
|
|
{ |
22
|
|
|
/** @var U2fRegistrationFactory */ |
23
|
|
|
private $u2fRegistrationFactory; |
24
|
|
|
|
25
|
|
|
/** @var U2fServerGenerator */ |
26
|
|
|
private $u2fServerGenerator; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
U2fRegistrationFactory $u2fRegistrationFactory, |
30
|
|
|
U2fServerGenerator $u2fServerGenerator |
31
|
|
|
) { |
32
|
|
|
$this->u2fRegistrationFactory = $u2fRegistrationFactory; |
33
|
|
|
$this->u2fServerGenerator = $u2fServerGenerator; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Generates a new U2F sign request. |
38
|
|
|
* |
39
|
|
|
* @param string $username The username of the member. |
40
|
|
|
* @param ArrayObject $registrations An array object of U2F registrations. |
41
|
|
|
* @throws NoRegisteredU2fTokenException if the member does not have any U2F |
42
|
|
|
* tokens registered with their account. |
43
|
|
|
* @todo Use a scalar array instead? |
44
|
|
|
*/ |
45
|
|
|
public function generate( |
46
|
|
|
string $username, |
|
|
|
|
47
|
|
|
ArrayObject $registrations |
48
|
|
|
): array { |
49
|
|
|
$firehedRegs = array_map( |
50
|
|
|
[$this->u2fRegistrationFactory, 'toFirehed'], |
51
|
|
|
$registrations->toArray(IU2fRegistration::class) |
52
|
|
|
); |
53
|
|
|
$signRequests = $this |
54
|
|
|
->u2fServerGenerator |
55
|
|
|
->getServer() |
56
|
|
|
->generateSignRequests($firehedRegs) |
57
|
|
|
; |
58
|
|
|
|
59
|
|
|
if (0 === count($signRequests)) { |
60
|
|
|
throw new NoRegisteredU2fTokenException(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $signRequests; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Process a response. |
68
|
|
|
* |
69
|
|
|
* @param ArrayObject $registrations An ArrayObject of IU2fRegistration. |
70
|
|
|
* @param ArrayObject $signRequests An ArrayObject of Firehed SignRequest. |
71
|
|
|
* @param string $u2fTokenResponse The response from the U2F token. |
72
|
|
|
* @return IU2fRegistration The U2F registration with an updated counter. |
73
|
|
|
* @todo Use scalar arrays instead? |
74
|
|
|
*/ |
75
|
|
|
public function processResponse( |
76
|
|
|
ArrayObject $registrations, |
77
|
|
|
ArrayObject $signRequests, |
78
|
|
|
string $u2fTokenResponse |
79
|
|
|
): IU2fRegistration { |
80
|
|
|
$server = $this |
81
|
|
|
->u2fServerGenerator |
82
|
|
|
->getServer() |
83
|
|
|
; |
84
|
|
|
$firehedRegs = array_map( |
85
|
|
|
[$this->u2fRegistrationFactory, 'toFirehed'], |
86
|
|
|
$registrations->toArray(IU2fRegistration::class) |
87
|
|
|
); |
88
|
|
|
$server |
89
|
|
|
->setRegistrations($firehedRegs) |
90
|
|
|
->setSignRequests($signRequests->toArray(SignRequest::class)) |
91
|
|
|
; |
92
|
|
|
$response = SignResponse::fromJson($u2fTokenResponse); |
93
|
|
|
$registration = $server->authenticate($response); |
94
|
|
|
|
95
|
|
|
return $this |
96
|
|
|
->u2fRegistrationFactory |
97
|
|
|
->fromFirehed($registration) |
98
|
|
|
; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.