|
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\Frontend; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Framework\App\ResponseFactory; |
|
25
|
|
|
use Magento\Framework\Event\Observer; |
|
26
|
|
|
use Magento\Framework\Event\ObserverInterface; |
|
27
|
|
|
use Magento\Framework\UrlInterface; |
|
28
|
|
|
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerAdmin; |
|
29
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor; |
|
30
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Customer; |
|
31
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Session; |
|
32
|
|
|
use Rossmitchell\Twofactor\Model\Urls\Checker; |
|
33
|
|
|
use Rossmitchell\Twofactor\Model\Urls\Fetcher; |
|
34
|
|
|
use Rossmitchell\Twofactor\Model\Verification\IsVerified; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Class Postdispatch |
|
38
|
|
|
* |
|
39
|
|
|
* This is call after the page response has been generated, but before it has been sent through to the user. There are a |
|
40
|
|
|
* couple of benefits to calling the method at this point rather than before the response has been generated. First, it |
|
41
|
|
|
* gets called as soon as the customer logs in, which should save a redirect, and it also means that everything has |
|
42
|
|
|
* already been instantiated, so I don't have to worry about the session issues that can crop up when a method is called |
|
43
|
|
|
* to early. |
|
44
|
|
|
* |
|
45
|
|
|
* @TODO: This method is really quite complicated and should be refactored into separate classes |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
|
|
class Postdispatch implements ObserverInterface |
|
48
|
|
|
{ |
|
49
|
|
|
/** |
|
50
|
|
|
* @var ResponseFactory |
|
51
|
|
|
*/ |
|
52
|
|
|
private $responseFactory; |
|
53
|
|
|
/** |
|
54
|
|
|
* @var UrlInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $url; |
|
57
|
|
|
/** |
|
58
|
|
|
* @var Customer |
|
59
|
|
|
*/ |
|
60
|
|
|
private $customerGetter; |
|
61
|
|
|
/** |
|
62
|
|
|
* @var IsUsingTwoFactor |
|
63
|
|
|
*/ |
|
64
|
|
|
private $isUsingTwoFactor; |
|
65
|
|
|
/** |
|
66
|
|
|
* @var IsVerified |
|
67
|
|
|
*/ |
|
68
|
|
|
private $isVerified; |
|
69
|
|
|
/** |
|
70
|
|
|
* @var Session |
|
71
|
|
|
*/ |
|
72
|
|
|
private $customerSession; |
|
73
|
|
|
/** |
|
74
|
|
|
* @var CustomerAdmin |
|
75
|
|
|
*/ |
|
76
|
|
|
private $customerAdmin; |
|
77
|
|
|
/** |
|
78
|
|
|
* @var Fetcher |
|
79
|
|
|
*/ |
|
80
|
|
|
private $fetcher; |
|
81
|
|
|
/** |
|
82
|
|
|
* @var Checker |
|
83
|
|
|
*/ |
|
84
|
|
|
private $checker; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Predispatch constructor. |
|
88
|
|
|
* |
|
89
|
|
|
* @param ResponseFactory $responseFactory |
|
90
|
|
|
* @param UrlInterface $url |
|
91
|
|
|
* @param Customer $customerGetter |
|
92
|
|
|
* @param IsVerified $isVerified |
|
93
|
|
|
* @param Session $customerSession |
|
94
|
|
|
* @param IsUsingTwoFactor $isUsingTwoFactor |
|
95
|
|
|
* @param CustomerAdmin $customerAdmin |
|
96
|
|
|
* @param Fetcher $fetcher |
|
97
|
|
|
* @param Checker $checker |
|
98
|
|
|
*/ |
|
99
|
27 |
|
public function __construct( |
|
100
|
|
|
ResponseFactory $responseFactory, |
|
101
|
|
|
UrlInterface $url, |
|
102
|
|
|
Customer $customerGetter, |
|
103
|
|
|
IsVerified $isVerified, |
|
104
|
|
|
Session $customerSession, |
|
105
|
|
|
IsUsingTwoFactor $isUsingTwoFactor, |
|
106
|
|
|
CustomerAdmin $customerAdmin, |
|
107
|
|
|
Fetcher $fetcher, |
|
108
|
|
|
Checker $checker |
|
109
|
|
|
) { |
|
110
|
27 |
|
$this->responseFactory = $responseFactory; |
|
111
|
27 |
|
$this->url = $url; |
|
112
|
27 |
|
$this->customerGetter = $customerGetter; |
|
113
|
27 |
|
$this->isUsingTwoFactor = $isUsingTwoFactor; |
|
114
|
27 |
|
$this->isVerified = $isVerified; |
|
115
|
27 |
|
$this->customerSession = $customerSession; |
|
116
|
27 |
|
$this->customerAdmin = $customerAdmin; |
|
117
|
27 |
|
$this->fetcher = $fetcher; |
|
118
|
27 |
|
$this->checker = $checker; |
|
119
|
27 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* This is the observer method. It *now* listens for the controller_front_send_response_before event, and really |
|
123
|
|
|
* should be renamed |
|
124
|
|
|
* |
|
125
|
|
|
* @TODO: Rename the class so it matches the event |
|
|
|
|
|
|
126
|
|
|
* |
|
127
|
|
|
* @param Observer $observer |
|
128
|
|
|
* |
|
129
|
|
|
* @return void |
|
130
|
|
|
*/ |
|
131
|
27 |
View Code Duplication |
public function execute(Observer $observer) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
27 |
|
if ($this->customerAdmin->isTwoFactorEnabled() !== true) { |
|
134
|
10 |
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
17 |
|
if ($this->shouldTheCustomerBeRedirected() === false) { |
|
138
|
12 |
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
5 |
|
if ($this->hasTwoFactorBeenChecked() === true) { |
|
142
|
2 |
|
return; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
3 |
|
$controller = $observer->getEvent()->getData('response'); |
|
146
|
3 |
|
$this->redirectToTwoFactorCheck($controller); |
|
147
|
3 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* This checks to see if the customer is on a page that shouldn't be redirected, if we actually have a customer, and |
|
151
|
|
|
* if so does that customer have two fact enabled. Very similar checks are done in the admin observer and this is |
|
152
|
|
|
* one of the methods that I want to refactor, once the test coverage is high enough to let me do this with |
|
153
|
|
|
* confidence |
|
154
|
|
|
* |
|
155
|
|
|
* @TODO: Refactor this |
|
|
|
|
|
|
156
|
|
|
* |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
17 |
|
private function shouldTheCustomerBeRedirected() |
|
160
|
|
|
{ |
|
161
|
17 |
|
if ($this->areWeOnAnAllowedPage() === true) { |
|
162
|
5 |
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
12 |
|
$customer = $this->customerGetter->getCustomer(); |
|
166
|
12 |
|
if ($customer === false) { |
|
167
|
2 |
|
return false; |
|
168
|
|
|
} |
|
169
|
10 |
|
$usingTwoFactor = $this->isUsingTwoFactor->getValue($customer); |
|
170
|
10 |
|
if ($usingTwoFactor === false) { |
|
171
|
5 |
|
return false; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
5 |
|
return true; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Checks if we are on the authentication or verification page. This code is duplicated in the admin observer, other |
|
179
|
|
|
* than forAdmin flag and can be refactored |
|
180
|
|
|
* |
|
181
|
|
|
* @TODO: move this to either the Checker class or somewhere else |
|
|
|
|
|
|
182
|
|
|
* |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
17 |
View Code Duplication |
private function areWeOnAnAllowedPage() |
|
|
|
|
|
|
186
|
|
|
{ |
|
187
|
17 |
|
$twoFactorUrls = $this->checker; |
|
188
|
17 |
|
if ($twoFactorUrls->areWeOnTheAuthenticationPage(false) === true) { |
|
189
|
2 |
|
return true; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
15 |
|
if ($twoFactorUrls->areWeOnTheVerificationPage(false) === true) { |
|
193
|
3 |
|
return true; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
12 |
|
return false; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Checks the session to see if the verification flag has been set. Can be refactored |
|
201
|
|
|
* |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
5 |
|
private function hasTwoFactorBeenChecked() |
|
205
|
|
|
{ |
|
206
|
5 |
|
$session = $this->customerSession; |
|
207
|
5 |
|
$checked = $this->isVerified->isVerified($session); |
|
208
|
|
|
|
|
209
|
5 |
|
return ($checked === true); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Redirects the customer to two factor authentication page, i.e. where they need to enter in their code./ |
|
214
|
|
|
* |
|
215
|
|
|
* @param $response |
|
216
|
|
|
*/ |
|
217
|
3 |
|
private function redirectToTwoFactorCheck($response) |
|
218
|
|
|
{ |
|
219
|
3 |
|
$twoFactorCheckUrl = $this->fetcher->getAuthenticationUrl(false); |
|
220
|
|
|
|
|
221
|
3 |
|
$response->setRedirect($twoFactorCheckUrl); |
|
222
|
3 |
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.