|
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\Customer; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Customer\Model\Customer; |
|
25
|
|
|
use Magento\Framework\Event\Observer; |
|
26
|
|
|
use Magento\Framework\Event\ObserverInterface; |
|
27
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor; |
|
28
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Attribute\TwoFactorSecret; |
|
29
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Session; |
|
30
|
|
|
use Rossmitchell\Twofactor\Model\Verification\IsVerified; |
|
31
|
|
|
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\Secret; |
|
32
|
|
|
|
|
33
|
|
|
class SaveBefore implements ObserverInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var Secret |
|
37
|
|
|
*/ |
|
38
|
|
|
private $secret; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var IsVerified |
|
41
|
|
|
*/ |
|
42
|
|
|
private $isVerified; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var Session |
|
45
|
|
|
*/ |
|
46
|
|
|
private $session; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var TwoFactorSecret |
|
49
|
|
|
*/ |
|
50
|
|
|
private $twoFactorSecret; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var IsUsingTwoFactor |
|
53
|
|
|
*/ |
|
54
|
|
|
private $isUsingTwoFactor; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* SaveBefore constructor. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Secret $secret |
|
60
|
|
|
* @param IsVerified $isVerified |
|
61
|
|
|
* @param Session $session |
|
62
|
|
|
* @param TwoFactorSecret $twoFactorSecret |
|
63
|
|
|
* @param IsUsingTwoFactor $isUsingTwoFactor |
|
64
|
|
|
*/ |
|
65
|
5 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
66
|
|
|
Secret $secret, |
|
67
|
|
|
IsVerified $isVerified, |
|
68
|
|
|
Session $session, |
|
69
|
|
|
TwoFactorSecret $twoFactorSecret, |
|
70
|
|
|
IsUsingTwoFactor $isUsingTwoFactor |
|
71
|
|
|
) { |
|
72
|
5 |
|
$this->secret = $secret; |
|
73
|
5 |
|
$this->isVerified = $isVerified; |
|
74
|
5 |
|
$this->session = $session; |
|
75
|
5 |
|
$this->twoFactorSecret = $twoFactorSecret; |
|
76
|
5 |
|
$this->isUsingTwoFactor = $isUsingTwoFactor; |
|
77
|
5 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Execute observer |
|
81
|
|
|
* |
|
82
|
|
|
* @param Observer $observer |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
5 |
|
public function execute(Observer $observer) |
|
87
|
|
|
{ |
|
88
|
|
|
/** @var Customer $customer */ |
|
89
|
5 |
|
$customer = $observer->getEvent()->getCustomer(); |
|
90
|
5 |
|
if ($this->needsToUpdate($customer) === false) { |
|
91
|
5 |
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
$this->generateSecretForCustomer($customer); |
|
95
|
3 |
|
$this->markCustomerAsVerified(); |
|
96
|
3 |
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
private function needsToUpdate(Customer $customer) |
|
99
|
|
|
{ |
|
100
|
5 |
|
$useTwoFactor = $this->isUsingTwoFactor->getValue($customer); |
|
101
|
5 |
|
$hasSecret = $this->twoFactorSecret->getValue($customer); |
|
102
|
|
|
/* If the use two factor attribute is not set then there is no need to do anything */ |
|
103
|
5 |
|
if ($useTwoFactor === false) { |
|
104
|
2 |
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/* If there is no secret set, then we need to update */ |
|
108
|
3 |
|
if (empty($hasSecret)) { |
|
109
|
3 |
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/* If we reach this point the use_two_factor attribute is set to true, and a secret has previously been saved */ |
|
113
|
3 |
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
private function generateSecretForCustomer(Customer $customer) |
|
117
|
|
|
{ |
|
118
|
3 |
|
$secret = $this->secret->generateSecret(); |
|
119
|
3 |
|
$this->twoFactorSecret->setValue($customer, $secret); |
|
120
|
3 |
|
} |
|
121
|
|
|
|
|
122
|
3 |
|
private function markCustomerAsVerified() |
|
123
|
|
|
{ |
|
124
|
3 |
|
$this->isVerified->setIsVerified($this->session); |
|
125
|
3 |
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
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.