|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Challenge; |
|
6
|
|
|
|
|
7
|
|
|
use Firehed\U2F\ClientErrorException; |
|
8
|
|
|
use LM\AuthAbstractor\Enum\Persistence\Operation; |
|
9
|
|
|
use LM\AuthAbstractor\Factory\U2fRegistrationFactory; |
|
10
|
|
|
use LM\AuthAbstractor\Model\AuthenticationProcess; |
|
11
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationProcess; |
|
12
|
|
|
use LM\AuthAbstractor\Model\IU2fRegistration; |
|
13
|
|
|
use LM\AuthAbstractor\Model\PersistOperation; |
|
14
|
|
|
use LM\AuthAbstractor\Implementation\ChallengeResponse; |
|
15
|
|
|
use LM\AuthAbstractor\Model\U2fRegistrationRequest; |
|
16
|
|
|
use LM\AuthAbstractor\U2f\U2fRegistrationManager; |
|
17
|
|
|
use Firehed\U2F\InvalidDataException; |
|
18
|
|
|
use LM\Common\Enum\Scalar; |
|
19
|
|
|
use LM\Common\Model\ArrayObject; |
|
20
|
|
|
use LM\AuthAbstractor\Model\IChallengeResponse; |
|
21
|
|
|
use LM\Common\Model\IntegerObject; |
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
23
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
25
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
|
26
|
|
|
use Symfony\Component\Form\FormError; |
|
27
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
28
|
|
|
use Twig_Environment; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* A challenge for asking the user to register a new U2F device. |
|
32
|
|
|
*/ |
|
33
|
|
|
class U2fRegistrationChallenge implements IChallenge |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var FormFactoryInterface */ |
|
36
|
|
|
private $formFactory; |
|
37
|
|
|
|
|
38
|
|
|
/** @var HttpFoundationFactory */ |
|
39
|
|
|
private $httpFoundationFactory; |
|
40
|
|
|
|
|
41
|
|
|
/** @var Twig_Environment */ |
|
42
|
|
|
private $twig; |
|
43
|
|
|
|
|
44
|
|
|
/** @var U2fRegistrationManager */ |
|
45
|
|
|
private $u2fRegistrationManager; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @internal |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
FormFactoryInterface $formFactory, |
|
52
|
|
|
HttpFoundationFactory $httpFoundationFactory, |
|
53
|
|
|
U2fRegistrationFactory $u2fRegistrationFactory, |
|
54
|
|
|
U2fRegistrationManager $u2fRegistrationManager, |
|
55
|
|
|
Twig_Environment $twig |
|
56
|
|
|
) { |
|
57
|
|
|
$this->formFactory = $formFactory; |
|
58
|
|
|
$this->httpFoundationFactory = $httpFoundationFactory; |
|
59
|
|
|
$this->twig = $twig; |
|
60
|
|
|
$this->u2fRegistrationFactory = $u2fRegistrationFactory; |
|
|
|
|
|
|
61
|
|
|
$this->u2fRegistrationManager = $u2fRegistrationManager; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @internal |
|
66
|
|
|
* @todo Maybe it should convert u2fRegistrations to ArrayObject, and then |
|
67
|
|
|
* U2fRegistrationManager would also take an ArrayObject as parameter. |
|
68
|
|
|
* @todo Handle invalid responses. |
|
69
|
|
|
* @todo security: An important part of the state of the application is |
|
70
|
|
|
* frozen during the authentication process. This includes the U2F owned by |
|
71
|
|
|
* the current user (if any), among other things. However, security |
|
72
|
|
|
* decisions are based on potentially obsolete frozen versions of |
|
73
|
|
|
* parameters. |
|
74
|
|
|
* @todo Rename u2fDeviceResponse to u2fTokenResponse |
|
75
|
|
|
*/ |
|
76
|
|
|
public function process( |
|
77
|
|
|
IAuthenticationProcess $process, |
|
78
|
|
|
?ServerRequestInterface $httpRequest |
|
79
|
|
|
): IChallengeResponse { |
|
80
|
|
|
$u2fRegistrations = $process |
|
|
|
|
|
|
81
|
|
|
->getTypedMap() |
|
82
|
|
|
->get('u2f_registrations', Scalar::_ARRAY) |
|
83
|
|
|
; |
|
84
|
|
|
$nU2fRegistrations = $process |
|
|
|
|
|
|
85
|
|
|
->getTypedMap() |
|
86
|
|
|
->get('n_u2f_registrations', IntegerObject::class) |
|
87
|
|
|
->toInteger() |
|
88
|
|
|
; |
|
89
|
|
|
|
|
90
|
|
|
$form = $this |
|
91
|
|
|
->formFactory |
|
92
|
|
|
->createBuilder() |
|
93
|
|
|
->add('u2fDeviceResponse', HiddenType::class) |
|
94
|
|
|
->getForm() |
|
95
|
|
|
; |
|
96
|
|
|
|
|
97
|
|
|
if (null !== $httpRequest) { |
|
98
|
|
|
$form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$typedMap = null; |
|
|
|
|
|
|
102
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
103
|
|
|
try { |
|
104
|
|
|
$currentU2fRegistrationRequest = $process |
|
|
|
|
|
|
105
|
|
|
->getTypedMap() |
|
106
|
|
|
->get('current_u2f_registration_request', U2fRegistrationRequest::class) |
|
107
|
|
|
; |
|
108
|
|
|
// ob_start(); // tmp |
|
109
|
|
|
// var_dump($form['u2fDeviceResponse']->getData()); |
|
110
|
|
|
// file_put_contents('/var/www/html/tmp.txt', ob_get_clean(), FILE_APPEND); |
|
111
|
|
|
$u2fRegistration = $this |
|
112
|
|
|
->u2fRegistrationManager |
|
113
|
|
|
->getU2fRegistrationFromResponse( |
|
114
|
|
|
$form['u2fDeviceResponse']->getData(), |
|
115
|
|
|
$currentU2fRegistrationRequest->getRequest() |
|
116
|
|
|
) |
|
117
|
|
|
; |
|
118
|
|
|
// ob_start(); // tmp |
|
119
|
|
|
// var_dump($u2fRegistration); |
|
120
|
|
|
// file_put_contents('/var/www/html/tmp.txt', ob_get_clean(), FILE_APPEND); |
|
121
|
|
|
|
|
122
|
|
|
$u2fRegistrations[] = $u2fRegistration; |
|
123
|
|
|
|
|
124
|
|
|
$typedMap = $process |
|
|
|
|
|
|
125
|
|
|
->getTypedMap() |
|
126
|
|
|
->set( |
|
127
|
|
|
'persist_operations', |
|
128
|
|
|
$process |
|
|
|
|
|
|
129
|
|
|
->getTypedMap() |
|
130
|
|
|
->get('persist_operations', ArrayObject::class) |
|
131
|
|
|
->add( |
|
132
|
|
|
new PersistOperation($u2fRegistration, new Operation(Operation::CREATE)), |
|
133
|
|
|
PersistOperation::class |
|
134
|
|
|
), |
|
135
|
|
|
ArrayObject::class |
|
136
|
|
|
) |
|
137
|
|
|
->set( |
|
138
|
|
|
'n_u2f_registrations', |
|
139
|
|
|
new IntegerObject($nU2fRegistrations + 1), |
|
140
|
|
|
IntegerObject::class |
|
141
|
|
|
) |
|
142
|
|
|
->set( |
|
143
|
|
|
'u2f_registrations', |
|
144
|
|
|
$u2fRegistrations, |
|
145
|
|
|
Scalar::_ARRAY |
|
146
|
|
|
) |
|
147
|
|
|
; |
|
148
|
|
|
return new ChallengeResponse( |
|
149
|
|
|
new AuthenticationProcess($typedMap), |
|
150
|
|
|
null, |
|
151
|
|
|
false, |
|
152
|
|
|
true |
|
153
|
|
|
); |
|
154
|
|
|
} catch (ClientErrorException $e) { |
|
155
|
|
|
$form->addError(new FormError('You already used this U2F device')); |
|
156
|
|
|
} catch (InvalidDataException $e) { |
|
157
|
|
|
$form->addError(new FormError('The response is invalid.')); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$u2fRegistrationRequest = $this |
|
162
|
|
|
->u2fRegistrationManager |
|
163
|
|
|
->generate(new ArrayObject( |
|
|
|
|
|
|
164
|
|
|
$u2fRegistrations, |
|
|
|
|
|
|
165
|
|
|
IU2fRegistration::class |
|
166
|
|
|
)) |
|
167
|
|
|
; |
|
168
|
|
|
// ob_start(); // tmp |
|
169
|
|
|
// var_dump($u2fRegistrationRequest); // tmp |
|
170
|
|
|
// file_put_contents('/var/www/html/tmp.txt', ob_get_clean()); // tmp |
|
171
|
|
|
|
|
172
|
|
|
$httpResponse = new Response($this |
|
173
|
|
|
->twig |
|
174
|
|
|
->render('u2f_registration.html.twig', [ |
|
175
|
|
|
'form' => $form->createView(), |
|
176
|
|
|
'nU2fRegistrations' => $nU2fRegistrations, |
|
177
|
|
|
'request_json' => $u2fRegistrationRequest->getRequestAsJson(), |
|
178
|
|
|
'sign_requests' => $u2fRegistrationRequest->getSignRequestsAsJson(), |
|
179
|
|
|
])) |
|
180
|
|
|
; |
|
181
|
|
|
|
|
182
|
|
|
return new ChallengeResponse( |
|
183
|
|
|
new AuthenticationProcess( |
|
184
|
|
|
$process |
|
|
|
|
|
|
185
|
|
|
->getTypedMap() |
|
186
|
|
|
->add( |
|
187
|
|
|
'current_u2f_registration_request', |
|
188
|
|
|
$u2fRegistrationRequest, |
|
189
|
|
|
U2fRegistrationRequest::class |
|
190
|
|
|
) |
|
191
|
|
|
), |
|
192
|
|
|
$httpResponse, |
|
193
|
|
|
false, |
|
194
|
|
|
false |
|
195
|
|
|
) |
|
196
|
|
|
; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|