|
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\Block\Customer\Account\Edit; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Customer\Api\Data\CustomerInterface; |
|
25
|
|
|
use Magento\Framework\View\Element\Template; |
|
26
|
|
|
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerConfig; |
|
27
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Attribute\TwoFactorSecret; |
|
28
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Customer; |
|
29
|
|
|
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\QRCode as QRCodeGenerator; |
|
30
|
|
|
|
|
31
|
|
|
class QRCode extends Template |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var QRCodeGenerator |
|
35
|
|
|
*/ |
|
36
|
|
|
private $qRCode; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var TwoFactorSecret |
|
39
|
|
|
*/ |
|
40
|
|
|
private $twoFactorSecret; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var Customer |
|
43
|
|
|
*/ |
|
44
|
|
|
private $customerGetter; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var CustomerConfig |
|
47
|
|
|
*/ |
|
48
|
|
|
private $customerConfig; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* QRCode constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Template\Context $context |
|
54
|
|
|
* @param QRCodeGenerator $qRCode |
|
55
|
|
|
* @param TwoFactorSecret $twoFactorSecret |
|
56
|
|
|
* @param Customer $customerGetter |
|
57
|
|
|
* @param CustomerConfig $customerConfig |
|
58
|
|
|
* |
|
59
|
|
|
* @internal param array $data |
|
60
|
|
|
*/ |
|
61
|
8 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
62
|
|
|
Template\Context $context, |
|
63
|
|
|
QRCodeGenerator $qRCode, |
|
64
|
|
|
TwoFactorSecret $twoFactorSecret, |
|
65
|
|
|
Customer $customerGetter, |
|
66
|
|
|
CustomerConfig $customerConfig, |
|
67
|
|
|
array $data = [] |
|
68
|
|
|
) { |
|
69
|
8 |
|
parent::__construct($context, $data); |
|
70
|
8 |
|
$this->qRCode = $qRCode; |
|
71
|
8 |
|
$this->twoFactorSecret = $twoFactorSecret; |
|
72
|
8 |
|
$this->customerGetter = $customerGetter; |
|
73
|
8 |
|
$this->customerConfig = $customerConfig; |
|
74
|
8 |
|
} |
|
75
|
|
|
|
|
76
|
8 |
|
public function getCustomer() |
|
77
|
|
|
{ |
|
78
|
8 |
|
return $this->customerGetter->getCustomer(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
8 |
|
public function hasAQrCode(CustomerInterface $customer) |
|
82
|
|
|
{ |
|
83
|
8 |
|
if ($this->customerConfig->isTwoFactorEnabled() == false) { |
|
84
|
2 |
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
6 |
|
return $this->twoFactorSecret->hasValue($customer); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
public function getQrCode(CustomerInterface $customer) |
|
91
|
|
|
{ |
|
92
|
4 |
|
if ($this->twoFactorSecret->hasValue($customer) === false) { |
|
93
|
|
|
throw new \Exception("Could not load the QR code because the customer does not have a secret"); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
$secret = $this->twoFactorSecret->hasValue($customer); |
|
97
|
4 |
|
$email = $customer->getEmail(); |
|
98
|
4 |
|
$qrCode = $this->qRCode->generateQRCode('Test', $email, $secret); |
|
99
|
|
|
|
|
100
|
4 |
|
return $qrCode; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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.