1
|
|
|
<?php |
2
|
|
|
/* vim: set expandtab sw=4 ts=4 sts=4: */ |
3
|
|
|
/** |
4
|
|
|
* Second authentication factor handling |
5
|
|
|
* |
6
|
|
|
* @package PhpMyAdmin |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace PhpMyAdmin\Plugins\TwoFactor; |
11
|
|
|
|
12
|
|
|
use BaconQrCode\Renderer\Image\SvgImageBackEnd; |
13
|
|
|
use PhpMyAdmin\Plugins\TwoFactorPlugin; |
14
|
|
|
use PhpMyAdmin\TwoFactor; |
15
|
|
|
use PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException; |
16
|
|
|
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException; |
17
|
|
|
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException; |
18
|
|
|
use PragmaRX\Google2FAQRCode\Google2FA; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* HOTP and TOTP based two-factor authentication |
22
|
|
|
* |
23
|
|
|
* Also known as Google, Authy, or OTP |
24
|
|
|
* |
25
|
|
|
* @package PhpMyAdmin |
26
|
|
|
*/ |
27
|
|
|
class Application extends TwoFactorPlugin |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public static $id = 'application'; |
33
|
|
|
|
34
|
|
|
protected $_google2fa; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates object |
38
|
|
|
* |
39
|
|
|
* @param TwoFactor $twofactor TwoFactor instance |
40
|
|
|
*/ |
41
|
|
|
public function __construct(TwoFactor $twofactor) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($twofactor); |
44
|
|
|
if (extension_loaded('imagick')) { |
45
|
|
|
$this->_google2fa = new Google2FA(); |
46
|
|
|
} else { |
47
|
|
|
$this->_google2fa = new Google2FA(new SvgImageBackEnd()); |
48
|
|
|
} |
49
|
|
|
$this->_google2fa->setWindow(8); |
50
|
|
|
if (! isset($this->_twofactor->config['settings']['secret'])) { |
51
|
|
|
$this->_twofactor->config['settings']['secret'] = ''; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return Google2FA |
57
|
|
|
*/ |
58
|
|
|
public function getGoogle2fa(): Google2FA |
59
|
|
|
{ |
60
|
|
|
return $this->_google2fa; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Checks authentication, returns true on success |
65
|
|
|
* |
66
|
|
|
* @return boolean |
67
|
|
|
* @throws IncompatibleWithGoogleAuthenticatorException |
68
|
|
|
* @throws InvalidCharactersException |
69
|
|
|
* @throws SecretKeyTooShortException |
70
|
|
|
*/ |
71
|
|
|
public function check() |
72
|
|
|
{ |
73
|
|
|
$this->_provided = false; |
74
|
|
|
if (! isset($_POST['2fa_code'])) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
$this->_provided = true; |
78
|
|
|
return $this->_google2fa->verifyKey( |
79
|
|
|
$this->_twofactor->config['settings']['secret'], |
80
|
|
|
$_POST['2fa_code'] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Renders user interface to enter two-factor authentication |
86
|
|
|
* |
87
|
|
|
* @return string HTML code |
88
|
|
|
*/ |
89
|
|
|
public function render() |
90
|
|
|
{ |
91
|
|
|
return $this->template->render('login/twofactor/application'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Renders user interface to configure two-factor authentication |
96
|
|
|
* |
97
|
|
|
* @return string HTML code |
98
|
|
|
*/ |
99
|
|
|
public function setup() |
100
|
|
|
{ |
101
|
|
|
$secret = $this->_twofactor->config['settings']['secret']; |
102
|
|
|
$inlineUrl = $this->_google2fa->getQRCodeInline( |
103
|
|
|
'phpMyAdmin (' . $this->getAppId(false) . ')', |
104
|
|
|
$this->_twofactor->user, |
105
|
|
|
$secret |
106
|
|
|
); |
107
|
|
|
return $this->template->render('login/twofactor/application_configure', [ |
108
|
|
|
'image' => $inlineUrl, |
109
|
|
|
'secret' => $secret, |
110
|
|
|
'has_imagick' => extension_loaded('imagick'), |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Performs backend configuration |
116
|
|
|
* |
117
|
|
|
* @return boolean |
118
|
|
|
* @throws IncompatibleWithGoogleAuthenticatorException |
119
|
|
|
* @throws InvalidCharactersException |
120
|
|
|
* @throws SecretKeyTooShortException |
121
|
|
|
*/ |
122
|
|
|
public function configure() |
123
|
|
|
{ |
124
|
|
|
if (! isset($_SESSION['2fa_application_key'])) { |
125
|
|
|
$_SESSION['2fa_application_key'] = $this->_google2fa->generateSecretKey(); |
126
|
|
|
} |
127
|
|
|
$this->_twofactor->config['settings']['secret'] = $_SESSION['2fa_application_key']; |
128
|
|
|
|
129
|
|
|
$result = $this->check(); |
130
|
|
|
if ($result) { |
131
|
|
|
unset($_SESSION['2fa_application_key']); |
132
|
|
|
} |
133
|
|
|
return $result; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get user visible name |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public static function getName() |
142
|
|
|
{ |
143
|
|
|
return __('Authentication Application (2FA)'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get user visible description |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public static function getDescription() |
152
|
|
|
{ |
153
|
|
|
return __('Provides authentication using HOTP and TOTP applications such as FreeOTP, Google Authenticator or Authy.'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|