|
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
|
|
|
* @param array $data |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function __construct( |
|
61
|
|
|
Template\Context $context, |
|
62
|
|
|
QRCodeGenerator $qRCode, |
|
63
|
|
|
TwoFactorSecret $twoFactorSecret, |
|
64
|
|
|
Customer $customerGetter, |
|
65
|
|
|
CustomerConfig $customerConfig, |
|
66
|
|
|
array $data = [] |
|
67
|
|
|
) { |
|
68
|
4 |
|
parent::__construct($context, $data); |
|
69
|
4 |
|
$this->qRCode = $qRCode; |
|
70
|
4 |
|
$this->twoFactorSecret = $twoFactorSecret; |
|
71
|
4 |
|
$this->customerGetter = $customerGetter; |
|
72
|
4 |
|
$this->customerConfig = $customerConfig; |
|
73
|
4 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* A simple getter method to return the current customer |
|
77
|
|
|
* |
|
78
|
|
|
* @return CustomerInterface |
|
79
|
|
|
*/ |
|
80
|
4 |
|
public function getCustomer() |
|
81
|
|
|
{ |
|
82
|
4 |
|
return $this->customerGetter->getCustomer(); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Used to check if the code should be displayed - will return false if two factor is disabled in the config, or if |
|
87
|
|
|
* the customer does not have a code. Otherwise returns true |
|
88
|
|
|
* |
|
89
|
|
|
* @param CustomerInterface $customer |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool# |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
4 |
|
public function shouldQrCodeBeDisplayed(CustomerInterface $customer) |
|
94
|
|
|
{ |
|
95
|
4 |
|
if ($this->customerConfig->isTwoFactorEnabled() === false) { |
|
96
|
1 |
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
if ($this->twoFactorSecret->hasValue($customer) === false) { |
|
100
|
1 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Used to get the customers QR Code. Will return false if they don't have one, otherwise will return the image |
|
108
|
|
|
* string |
|
109
|
|
|
* |
|
110
|
|
|
* @param CustomerInterface $customer |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool|string |
|
113
|
|
|
*/ |
|
114
|
2 |
|
public function getQrCode(CustomerInterface $customer) |
|
115
|
|
|
{ |
|
116
|
2 |
|
$secret = $this->twoFactorSecret->getValue($customer); |
|
117
|
2 |
|
$email = $customer->getEmail(); |
|
118
|
2 |
|
$companyName = $this->customerConfig->getCompanyName(); |
|
119
|
2 |
|
$qrCode = $this->qRCode->generateQRCode($companyName, $email, $secret); |
|
120
|
|
|
|
|
121
|
2 |
|
return $qrCode; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|