1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
4
|
|
|
* |
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace LoginCidadao\CoreBundle\Controller; |
12
|
|
|
|
13
|
|
|
use LoginCidadao\CoreBundle\Security\TwoFactorAuthenticationService; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
16
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
17
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
18
|
|
|
use LoginCidadao\CoreBundle\Form\Type\TwoFactorAuthenticationFormType; |
19
|
|
|
use LoginCidadao\CoreBundle\Form\Type\TwoFactorAuthenticationDisableFormType; |
20
|
|
|
use LoginCidadao\CoreBundle\Form\Type\TwoFactorAuthenticationBackupCodeGenerationFormType; |
21
|
|
|
use Symfony\Component\Form\FormError; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @Route("/two-factor") |
25
|
|
|
*/ |
26
|
|
|
class TwoFactorAuthenticationController extends Controller |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @Route("/enable", name="2fa_enable") |
30
|
|
|
* @Template() |
31
|
|
|
*/ |
32
|
|
|
public function enableAction(Request $request) |
33
|
|
|
{ |
34
|
|
|
/** @var TwoFactorAuthenticationService $twoFactor */ |
35
|
|
|
$twoFactor = $this->get('lc.two_factor'); |
36
|
|
|
|
37
|
|
|
$translator = $this->get('translator'); |
38
|
|
|
$person = $this->getUser(); |
39
|
|
|
$person->setGoogleAuthenticatorSecret($twoFactor->generateSecret()); |
40
|
|
|
|
41
|
|
|
$form = $this->createForm(TwoFactorAuthenticationFormType::class, $person); |
42
|
|
|
$form->handleRequest($request); |
43
|
|
|
|
44
|
|
|
if ($form->isValid()) { |
45
|
|
|
$verificationCode = $form->get('verification')->getData(); |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
$twoFactor->enable($form->getData(), $verificationCode); |
49
|
|
|
|
50
|
|
|
$message = $translator->trans('Two-Factor Authentication enabled.'); |
51
|
|
|
$this->get('session')->getFlashBag()->add('success', $message); |
52
|
|
|
|
53
|
|
|
return $this->redirect($this->generateUrl("fos_user_change_password")); |
54
|
|
|
} catch (\InvalidArgumentException $e) { |
55
|
|
|
$message = $translator->trans($e->getMessage()); |
56
|
|
|
$form->get('verification')->addError(new FormError($message)); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return ['form' => $form->createView(), 'secretUrl' => $twoFactor->getSecretUrl($person)]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @Route("/disable", name="2fa_disable") |
65
|
|
|
* @Template() |
66
|
|
|
*/ |
67
|
|
|
public function disableAction(Request $request) |
68
|
|
|
{ |
69
|
|
|
/** @var TwoFactorAuthenticationService $twoFactor */ |
70
|
|
|
$twoFactor = $this->get('lc.two_factor'); |
71
|
|
|
|
72
|
|
|
$person = $this->getUser(); |
73
|
|
|
$form = $this->createForm(TwoFactorAuthenticationDisableFormType::class, $person); |
74
|
|
|
$form->handleRequest($request); |
75
|
|
|
|
76
|
|
|
if ($form->isValid()) { |
77
|
|
|
$translator = $this->get('translator'); |
78
|
|
|
$twoFactor->disable($person); |
79
|
|
|
$message = $translator->trans('Two-Factor Authentication disabled.'); |
80
|
|
|
$this->get('session')->getFlashBag()->add('success', $message); |
81
|
|
|
|
82
|
|
|
return $this->redirect($this->generateUrl("fos_user_change_password")); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return ['form' => $form->createView()]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @Route("/backup-codes/generate", name="2fa_backup_codes_generate") |
90
|
|
|
* @Template() |
91
|
|
|
*/ |
92
|
|
|
public function generateBackupCodesAction(Request $request) |
93
|
|
|
{ |
94
|
|
|
/** @var TwoFactorAuthenticationService $twoFactor */ |
95
|
|
|
$twoFactor = $this->get('lc.two_factor'); |
96
|
|
|
|
97
|
|
|
$person = $this->getUser(); |
98
|
|
|
$form = $this->createForm(TwoFactorAuthenticationBackupCodeGenerationFormType::class, $person); |
99
|
|
|
$form->handleRequest($request); |
100
|
|
|
|
101
|
|
|
if ($form->isValid()) { |
102
|
|
|
$twoFactor->removeBackupCodes($person); |
103
|
|
|
$twoFactor->generateBackupCodes($person); |
104
|
|
|
|
105
|
|
|
$message = $this->get('translator') |
106
|
|
|
->trans('New Backup Codes generated. Don\'t forget to copy and store them safely.'); |
107
|
|
|
$this->get('session')->getFlashBag()->add('success', $message); |
108
|
|
|
|
109
|
|
|
return $this->redirect($this->generateUrl("fos_user_change_password")); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return ['form' => $form->createView()]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|