-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSetupController.php
More file actions
351 lines (305 loc) · 10.7 KB
/
SetupController.php
File metadata and controls
351 lines (305 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php declare(strict_types=1);
/**
* Class SetupController
*
* @author Robin 'codeFareith' von den Bergen <robinvonberg@gmx.de>
* @copyright (c) 2018-2022 by Robin von den Bergen
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version 1.0.0
*
* @link https://github.com/codeFareith/cf_google_authenticator
* @see https://www.fareith.de
* @see https://typo3.org
*/
namespace CodeFareith\CfGoogleAuthenticator\Controller\Frontend;
use CodeFareith\CfGoogleAuthenticator\Domain\Form\SetupForm;
use CodeFareith\CfGoogleAuthenticator\Domain\Immutable\AuthenticationSecret;
use CodeFareith\CfGoogleAuthenticator\Domain\Model\FrontendUser;
use CodeFareith\CfGoogleAuthenticator\Domain\Repository\FrontendUserRepository;
use CodeFareith\CfGoogleAuthenticator\Service\GoogleQrCodeGenerator;
use CodeFareith\CfGoogleAuthenticator\Utility\Base32Utility;
use CodeFareith\CfGoogleAuthenticator\Utility\PathUtility;
use CodeFareith\CfGoogleAuthenticator\Validation\Validator\SetupFormValidator;
use Exception;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
use TYPO3\CMS\Extbase\Object\Exception as ObjectException;
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
use TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
use function get_class;
use function vsprintf;
/**
* Two-factor authentication setup controller
*
* A controller that allows users to set up the
* two-factor authentication for their respective frontend accounts.
*
* @package CodeFareith\CfGoogleAuthenticator\Controller\Frontend
* @since 1.0.0
*/
class SetupController
extends ActionController
{
/*─────────────────────────────────────────────────────────────────────────────*\
Properties
\*─────────────────────────────────────────────────────────────────────────────*/
/**
* @var FrontendUserRepository
*/
protected $frontendUserRepository;
/**
* @var GoogleQrCodeGenerator
*/
protected $qrCodeGenerator;
/**
* @var SetupFormValidator
*/
protected $setupFormValidator;
/**
* @var LanguageService
*/
protected $languageService;
/**
* @var Context
*/
protected $context;
/**
* @var AuthenticationSecret
*/
private $authenticationSecret;
/**
* @var Dispatcher
*/
protected $dispatcher;
/*─────────────────────────────────────────────────────────────────────────────*\
Methods
\*─────────────────────────────────────────────────────────────────────────────*/
public function __construct(
FrontendUserRepository $frontendUserRepository,
GoogleQrCodeGenerator $qrCodeGenerator,
SetupFormValidator $setupFormValidator,
LanguageService $languageService,
Context $context,
Dispatcher $dispatcher
)
{
$this->frontendUserRepository = $frontendUserRepository;
$this->qrCodeGenerator = $qrCodeGenerator;
$this->setupFormValidator = $setupFormValidator;
$this->languageService = $languageService;
$this->context = $context;
$this->dispatcher = $dispatcher;
}
/**
* @throws Exception
*/
public function indexAction(): void
{
if ($this->isUserLoggedin()) {
$authenticationSecret = $this->getAuthenticationSecret();
$isEnabled = $this->isGoogleAuthenticatorEnabled();
$setupForm = $this->getSetupForm();
$qrCodeUri = $this->qrCodeGenerator->generateUri($authenticationSecret);
$this->view->assignMultiple(
[
'isEnabled' => $isEnabled,
'formData' => $setupForm,
'formName' => SetupForm::FORM_NAME,
'qrCodeUri' => $qrCodeUri,
]
);
}
}
/**
* @throws NoSuchArgumentException
*/
public function initializeUpdateAction(): void
{
if ($this->request->hasArgument(SetupForm::FORM_NAME)) {
$formObject = $this->getFormObject();
$results = $this->setupFormValidator->validate($formObject);
$this->request->setOriginalRequest(clone $this->request);
$this->request->setOriginalRequestMappingResults($results);
}
}
/**
* @throws NoSuchArgumentException
* @throws StopActionException
* @throws IllegalObjectTypeException
* @throws UnknownObjectException
* @throws UnsupportedRequestTypeException
*/
public function updateAction(): void
{
if ($this->isValidUpdateRequest()) {
$user = $this->getFrontendUser();
if ($user !== null) {
$formData = (array)$this->request->getArgument(SetupForm::FORM_NAME);
$action = null;
if ($this->request->hasArgument('enable')) {
$user->enableGoogleAuthenticator($formData['secret']);
$action = 'enable';
} elseif ($this->request->hasArgument('disable')) {
$user->disableGoogleAuthenticator();
$action = 'disable';
}
$this->frontendUserRepository->update($user);
$this->addSuccessMessage();
if ($action !== null) {
$this->dispatcher->dispatch(
__CLASS__,
'toggleGoogleAuthenticator',
[
'action' => $action,
'user' => $user,
'caller' => $this,
]
);
}
$this->redirect('index');
}
}
$this->forward('index');
}
/**
* @throws AspectNotFoundException
*/
private function isUserLoggedin(): bool
{
return (bool)$this->context->getPropertyFromAspect(
'frontend.user',
'isLoggedIn'
);
}
/**
* @throws Exception
*/
private function getAuthenticationSecret(): AuthenticationSecret
{
if ($this->authenticationSecret === null) {
$this->authenticationSecret = $this->objectManager->get(
AuthenticationSecret::class,
$this->getIssuer(),
$this->getUsername(),
$this->getSecretKey()
);
}
return $this->authenticationSecret;
}
private function getIssuer(): string
{
return vsprintf(
'%s - %s',
[
$this->getSiteName(),
'Frontend',
]
);
}
private function getSiteName(): string
{
return $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
}
private function getUsername(): string
{
return $GLOBALS['TSFE']->fe_user->user['username'];
}
/**
* @throws Exception
*/
private function getSecretKey(): string
{
if ($this->isGoogleAuthenticatorEnabled()) {
$secretKey = $GLOBALS['TSFE']->fe_user->user['tx_cfgoogleauthenticator_secret'];
} else {
$secretKey = Base32Utility::generateRandomString(16);
}
return $secretKey;
}
private function isValidUpdateRequest(): bool
{
$mappingResults = $this->request->getOriginalRequestMappingResults();
$hasErrors = $mappingResults->hasErrors();
$hasArgument = $this->request->hasArgument(SetupForm::FORM_NAME);
return (!$hasErrors && $hasArgument);
}
private function isGoogleAuthenticatorEnabled(): bool
{
return (bool)$GLOBALS['TSFE']->fe_user->user['tx_cfgoogleauthenticator_enabled'];
}
/**
* @throws Exception
*/
private function getSetupForm(): SetupForm
{
$object = $this->objectManager->get(
SetupForm::class,
$this->getAuthenticationSecret()->getSecretKey(),
''
);
if (!$object instanceof SetupForm) {
throw new ObjectException(
vsprintf(
'Invalid class. Expected "%s", got "%s".',
[
SetupForm::class,
get_class($object),
]
)
);
}
return $object;
}
/**
* @throws NoSuchArgumentException
*/
private function getFormObject(): SetupForm
{
$formData = (array)$this->request->getArgument(SetupForm::FORM_NAME);
/** @var SetupForm $formObject */
$formObject = $this->objectManager->get(
SetupForm::class,
$formData['secret'],
$formData['oneTimePassword']
);
return $formObject;
}
private function getFrontendUser(): FrontendUser
{
$user = null;
$userId = $this->getFrontendUserId();
$user = $this->frontendUserRepository->findByUid($userId);
return $user;
}
private function getFrontendUserId()
{
return $GLOBALS['TSFE']->fe_user->user['uid'];
}
private function getLanguageService(): LanguageService
{
return $this->languageService;
}
private function addSuccessMessage(): void
{
$this->addFlashMessage(
$this->getLanguageService()->sL(
PathUtility::makeLocalLangLinkPath(
'setup.update.success.body'
)
),
$this->getLanguageService()->sL(
PathUtility::makeLocalLangLinkPath(
'setup.update.success.title'
)
),
FlashMessage::OK
);
}
}