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