|
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\Observer\Controller\Admin; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Backend\App\Action; |
|
25
|
|
|
use Magento\Framework\Event\Observer; |
|
26
|
|
|
use Magento\Framework\Event\ObserverInterface; |
|
27
|
|
|
use Rossmitchell\Twofactor\Model\Admin\AdminUser; |
|
28
|
|
|
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor; |
|
29
|
|
|
use Rossmitchell\Twofactor\Model\Admin\Session; |
|
30
|
|
|
use Rossmitchell\Twofactor\Model\Config\Admin; |
|
31
|
|
|
use Rossmitchell\Twofactor\Model\Urls\Checker; |
|
32
|
|
|
use Rossmitchell\Twofactor\Model\Urls\Fetcher; |
|
33
|
|
|
use Rossmitchell\Twofactor\Model\Verification\IsVerified; |
|
34
|
|
|
|
|
35
|
|
|
class Postdispatch implements ObserverInterface |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var AdminUser |
|
39
|
|
|
*/ |
|
40
|
|
|
private $adminUser; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var IsUsingTwoFactor |
|
43
|
|
|
*/ |
|
44
|
|
|
private $isUsingTwoFactor; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var Session |
|
47
|
|
|
*/ |
|
48
|
|
|
private $session; |
|
49
|
|
|
/** |
|
50
|
|
|
* @var IsVerified |
|
51
|
|
|
*/ |
|
52
|
|
|
private $isVerified; |
|
53
|
|
|
/** |
|
54
|
|
|
* @var Admin |
|
55
|
|
|
*/ |
|
56
|
|
|
private $adminConfig; |
|
57
|
|
|
/** |
|
58
|
|
|
* @var Fetcher |
|
59
|
|
|
*/ |
|
60
|
|
|
private $fetcher; |
|
61
|
|
|
/** |
|
62
|
|
|
* @var Checker |
|
63
|
|
|
*/ |
|
64
|
|
|
private $checker; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Postdispatch constructor. |
|
68
|
|
|
* |
|
69
|
|
|
* @param AdminUser $adminUser |
|
70
|
|
|
* @param IsUsingTwoFactor $isUsingTwoFactor |
|
71
|
|
|
* @param Session $session |
|
72
|
|
|
* @param IsVerified $isVerified |
|
73
|
|
|
* @param Fetcher $fetcher |
|
74
|
|
|
* @param Checker $checker |
|
75
|
|
|
* @param Admin $adminConfig |
|
76
|
|
|
*/ |
|
77
|
22 |
|
public function __construct( |
|
78
|
|
|
AdminUser $adminUser, |
|
79
|
|
|
IsUsingTwoFactor $isUsingTwoFactor, |
|
80
|
|
|
Session $session, |
|
81
|
|
|
IsVerified $isVerified, |
|
82
|
|
|
Fetcher $fetcher, |
|
83
|
|
|
Checker $checker, |
|
84
|
|
|
Admin $adminConfig |
|
85
|
|
|
) { |
|
86
|
22 |
|
$this->adminUser = $adminUser; |
|
87
|
22 |
|
$this->isUsingTwoFactor = $isUsingTwoFactor; |
|
88
|
22 |
|
$this->session = $session; |
|
89
|
22 |
|
$this->isVerified = $isVerified; |
|
90
|
22 |
|
$this->adminConfig = $adminConfig; |
|
91
|
22 |
|
$this->checker = $checker; |
|
92
|
22 |
|
$this->fetcher = $fetcher; |
|
93
|
22 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param Observer $observer |
|
97
|
|
|
* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
22 |
|
public function execute(Observer $observer) |
|
101
|
|
|
{ |
|
102
|
22 |
|
if ($this->isTwoFactorEnabled() === false) { |
|
103
|
6 |
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
16 |
|
if ($this->shouldTheUserBeRedirected() === false) { |
|
107
|
10 |
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
6 |
|
if ($this->areWeOnANonRedirectingPage() === true) { |
|
111
|
4 |
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
$controller = $observer->getEvent()->getData('response'); |
|
115
|
2 |
|
$this->redirectToAuthenticationPage($controller); |
|
116
|
2 |
|
} |
|
117
|
|
|
|
|
118
|
22 |
|
private function isTwoFactorEnabled() |
|
119
|
|
|
{ |
|
120
|
22 |
|
return ($this->adminConfig->isTwoFactorEnabled() == true); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
16 |
|
private function shouldTheUserBeRedirected() |
|
124
|
|
|
{ |
|
125
|
16 |
|
$adminUser = $this->adminUser; |
|
126
|
16 |
|
if ($adminUser->hasAdminUser() === false) { |
|
127
|
4 |
|
return false; |
|
128
|
|
|
} |
|
129
|
12 |
|
$user = $this->adminUser->getAdminUser(); |
|
130
|
|
|
|
|
131
|
12 |
|
if ($this->isUsingTwoFactor->getValue($user) === false) { |
|
132
|
4 |
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
8 |
|
if ($this->isVerified->isVerified($this->session) === true) { |
|
136
|
2 |
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
6 |
|
return true; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
6 |
View Code Duplication |
private function areWeOnANonRedirectingPage() |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
6 |
|
$urls = $this->checker; |
|
145
|
|
|
|
|
146
|
6 |
|
if ($urls->areWeOnTheAuthenticationPage(true) === true) { |
|
147
|
2 |
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
4 |
|
if ($urls->areWeOnTheVerificationPage(true) === true) { |
|
151
|
2 |
|
return true; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
2 |
|
return false; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
2 |
|
private function redirectToAuthenticationPage($response) |
|
158
|
|
|
{ |
|
159
|
2 |
|
$twoFactorCheckUrl = $this->fetcher->getAuthenticationUrl(true); |
|
160
|
2 |
|
$response->setRedirect($twoFactorCheckUrl); |
|
161
|
2 |
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
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.