|
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\Adminhtml\System\Account; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Framework\View\Element\Template; |
|
25
|
|
|
use Magento\User\Model\User; |
|
26
|
|
|
use Rossmitchell\Twofactor\Model\Admin\AdminUser; |
|
27
|
|
|
use Rossmitchell\Twofactor\Model\Admin\Attribute\TwoFactorSecret; |
|
28
|
|
|
use Rossmitchell\Twofactor\Model\Config\Admin; |
|
29
|
|
|
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\QRCode as GoogleQRCode; |
|
30
|
|
|
|
|
31
|
|
|
class QRCode extends Template |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var AdminUser |
|
35
|
|
|
*/ |
|
36
|
|
|
private $adminUser; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var TwoFactorSecret |
|
39
|
|
|
*/ |
|
40
|
|
|
private $twoFactorSecret; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var GoogleQRCode |
|
43
|
|
|
*/ |
|
44
|
|
|
private $qRCode; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var Admin |
|
47
|
|
|
*/ |
|
48
|
|
|
private $adminConfig; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* QRCode constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Template\Context $context |
|
54
|
|
|
* @param AdminUser $adminUser |
|
55
|
|
|
* @param TwoFactorSecret $twoFactorSecret |
|
56
|
|
|
* @param GoogleQRCode $qRCode |
|
57
|
|
|
* @param Admin $adminConfig |
|
58
|
|
|
* @param array $data |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function __construct( |
|
61
|
|
|
Template\Context $context, |
|
62
|
|
|
AdminUser $adminUser, |
|
63
|
|
|
TwoFactorSecret $twoFactorSecret, |
|
64
|
|
|
GoogleQRCode $qRCode, |
|
65
|
|
|
Admin $adminConfig, |
|
66
|
|
|
array $data = [] |
|
67
|
|
|
) { |
|
68
|
3 |
|
parent::__construct($context, $data); |
|
69
|
3 |
|
$this->adminUser = $adminUser; |
|
70
|
3 |
|
$this->twoFactorSecret = $twoFactorSecret; |
|
71
|
3 |
|
$this->qRCode = $qRCode; |
|
72
|
3 |
|
$this->adminConfig = $adminConfig; |
|
73
|
3 |
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
public function getAdminUser() |
|
76
|
|
|
{ |
|
77
|
3 |
|
return $this->adminUser->getAdminUser(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
public function shouldQRCodeBeDisplayed(User $adminUser) |
|
81
|
|
|
{ |
|
82
|
3 |
|
if ($this->adminConfig->isTwoFactorEnabled() == false) { |
|
83
|
1 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
if ($this->getSecret($adminUser) === null) { |
|
87
|
1 |
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function getQRCode(User $adminUser) |
|
94
|
|
|
{ |
|
95
|
1 |
|
$secret = $this->getSecret($adminUser); |
|
96
|
1 |
|
$companyName = $this->adminConfig->getCompanyName(); |
|
97
|
|
|
|
|
98
|
1 |
|
return $this->qRCode->generateQRCode($companyName, $adminUser->getEmail(), $secret); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
private function getSecret(User $adminUser) |
|
102
|
|
|
{ |
|
103
|
2 |
|
return $this->twoFactorSecret->getValue($adminUser); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|