|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A two factor authentication module that protects both the admin and customer logins |
|
4
|
|
|
* Copyright (C) 2017 Ross Mitchell |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of Rossmitchell/Twofactor. |
|
7
|
|
|
* |
|
8
|
|
|
* Rossmitchell/Twofactor is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU General Public License as published by |
|
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* This program is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU General Public License |
|
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace Rossmitchell\Twofactor\Controller\Adminhtml\Adminlogin; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Backend\App\Action\Context; |
|
25
|
|
|
use Magento\Framework\App\ResponseInterface; |
|
26
|
|
|
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException; |
|
27
|
|
|
use Rossmitchell\Twofactor\Model\Admin\AdminUser; |
|
28
|
|
|
use Rossmitchell\Twofactor\Model\Admin\Attribute\TwoFactorSecret; |
|
29
|
|
|
use Rossmitchell\Twofactor\Model\Admin\Session; |
|
30
|
|
|
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\Verify as GoogleVerify; |
|
31
|
|
|
use Rossmitchell\Twofactor\Model\Urls\Fetcher; |
|
32
|
|
|
use Rossmitchell\Twofactor\Model\Verification\IsVerified; |
|
33
|
|
|
use Rossmitchell\Twofactor\Model\Config\Admin as UserAdmin; |
|
34
|
|
|
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor; |
|
35
|
|
|
|
|
36
|
|
|
class Verify extends AbstractController |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var TwoFactorSecret |
|
40
|
|
|
*/ |
|
41
|
|
|
private $twoFactorSecret; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var GoogleVerify |
|
44
|
|
|
*/ |
|
45
|
|
|
private $verify; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var IsVerified |
|
48
|
|
|
*/ |
|
49
|
|
|
private $isVerified; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var Session |
|
52
|
|
|
*/ |
|
53
|
|
|
private $adminSession; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Verify constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Context $context |
|
59
|
|
|
* @param UserAdmin $userAdmin |
|
60
|
|
|
* @param AdminUser $adminGetter |
|
61
|
|
|
* @param Fetcher $fetcher |
|
62
|
|
|
* @param IsUsingTwoFactor $isUsingTwoFactor |
|
63
|
|
|
* @param TwoFactorSecret $twoFactorSecret |
|
64
|
|
|
* @param GoogleVerify $verify |
|
65
|
|
|
* @param IsVerified $isVerified |
|
66
|
|
|
* @param Session $adminSession |
|
67
|
|
|
* @param Fetcher $fetcher |
|
68
|
|
|
*/ |
|
69
|
3 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
70
|
|
|
Context $context, |
|
71
|
|
|
UserAdmin $userAdmin, |
|
72
|
|
|
AdminUser $adminGetter, |
|
73
|
|
|
IsUsingTwoFactor $isUsingTwoFactor, |
|
74
|
|
|
TwoFactorSecret $twoFactorSecret, |
|
75
|
|
|
GoogleVerify $verify, |
|
76
|
|
|
IsVerified $isVerified, |
|
77
|
|
|
Session $adminSession, |
|
78
|
|
|
Fetcher $fetcher |
|
79
|
|
|
) { |
|
80
|
3 |
|
parent::__construct($context, $userAdmin, $adminGetter, $fetcher, $isUsingTwoFactor); |
|
81
|
3 |
|
$this->twoFactorSecret = $twoFactorSecret; |
|
82
|
3 |
|
$this->verify = $verify; |
|
83
|
3 |
|
$this->isVerified = $isVerified; |
|
84
|
3 |
|
$this->adminSession = $adminSession; |
|
85
|
3 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Dispatch request |
|
89
|
|
|
* |
|
90
|
|
|
* @return \Magento\Framework\Controller\ResultInterface|ResponseInterface |
|
91
|
|
|
* @throws \Magento\Framework\Exception\NotFoundException |
|
92
|
|
|
*/ |
|
93
|
3 |
View Code Duplication |
public function execute() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
3 |
|
if ($this->shouldActionBeRun() === false) { |
|
96
|
1 |
|
return $this->getRedirectAction(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
$secret = $this->getRequest()->getParam('secret'); |
|
100
|
2 |
|
$adminUser = $this->getAdminUser(); |
|
101
|
|
|
|
|
102
|
2 |
|
$verificationPassed = $this->verifySecret($adminUser, $secret); |
|
103
|
|
|
|
|
104
|
2 |
|
if ($verificationPassed === false) { |
|
105
|
1 |
|
return $this->handleError(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return $this->handleSuccess(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
private function verifySecret($adminUser, $postedSecret) |
|
112
|
|
|
{ |
|
113
|
2 |
|
$customerSecret = $this->twoFactorSecret->getValue($adminUser); |
|
114
|
|
|
try { |
|
115
|
2 |
|
$verified = $this->verify->verify($customerSecret, $postedSecret); |
|
116
|
|
|
} catch (InvalidCharactersException $exception) { |
|
117
|
|
|
$verified = false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
2 |
|
return $verified; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
View Code Duplication |
private function handleError() |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
1 |
|
$this->isVerified->removeIsVerified($this->adminSession); |
|
126
|
1 |
|
$this->addErrorMessage(); |
|
127
|
1 |
|
$authenticateUrl = $this->getUrlFetcher()->getAuthenticationUrl(true); |
|
128
|
|
|
|
|
129
|
1 |
|
return $this->redirect($authenticateUrl); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
private function addErrorMessage() |
|
133
|
|
|
{ |
|
134
|
1 |
|
$this->messageManager->addErrorMessage("Two Factor Code was incorrect"); |
|
135
|
1 |
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
private function handleSuccess() |
|
138
|
|
|
{ |
|
139
|
1 |
|
$this->isVerified->setIsVerified($this->adminSession); |
|
140
|
1 |
|
$accountUrl = $this->getUrlFetcher()->getAdminDashboardUrl(); |
|
141
|
|
|
|
|
142
|
1 |
|
return $this->redirect($accountUrl); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.