-
Notifications
You must be signed in to change notification settings - Fork 2
/
twofactorauth_class.php
300 lines (255 loc) · 7.96 KB
/
twofactorauth_class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
* TwoFactorAuth
*
* Copyright (C) 2021-2022 e107 Inc. (https://www.e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http:https://www.gnu.org/licenses/gpl.txt)
*
*/
e107_require_once(e_PLUGIN.'twofactorauth/vendor/autoload.php');
use \RobThree\Auth\TwoFactorAuth;
class tfa_class
{
public $tfa_debug = false;
public function __construct()
{
// Check debug mode
if(e107::getPlugPref('twofactorauth', 'tfa_debug'))
{
$this->tfa_debug = true;
}
}
public function init($data, $eventname)
{
// Login
if($eventname == 'user_validlogin')
{
$user_id = $data;
}
// FPW
else
{
// error_log($eventname);
// error_log(print_r($data, true));
// return false;
$user_id = $data["user_id"];
}
// Check if 2FA is activated
if($this->tfaActivated($user_id) == false)
{
// 2FA is NOT activated, return false to proceed with core login process.
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": 2FA is NOT activated for User ID ".$user_id);
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
return false;
}
// 2FA is enabled for this user. Continue verification process. Service page to enter TOTP digits, generated by user's authenthicator app.
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": 2FA is activated for User ID ".$user_id);
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": User will need to enter digits. Redirect to tfa/verify");
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
// Store some information in a session, so we can retrieve it again later
e107::getSession('2fa')->set('user_id', $user_id); // Store User ID
e107::getSession('2fa')->set('previous_page', e_REQUEST_URL); // Store the page the user is logging in from
// Redirect to page to enter TOTP
$url = e107::url('twofactorauth', 'verify');
e107::redirect($url);
return true;
}
public function tfaActivated($user_id)
{
if(!e107::getUserExt()->get($user_id, "user_plugin_twofactorauth_secret_key"))
{
return false;
}
return true;
}
public function showTotpInputForm($action = 'login', $secret = '')
{
$text = '';
switch($action)
{
case 'login':
$action = 'submit';
$button_name = "enter-totp-login";
break;
case 'fpw':
$action = 'submit';
$button_name = "enter-totp-fpw";
break;
case 'enable':
$action = 'submit';
$button_name = "enter-totp-enable";
break;
case 'disable':
$action = 'delete';
$button_name = "enter-totp-disable";
break;
default:
$action = 'submit';
$button_name = "enter-totp-login";
break;
}
$form_options = array(
//"size" => "small",
'required' => 1,
'placeholder' => LAN_2FA_ENTER_TOTP_PLACEHOLDER,
'autofocus' => true,
);
// Display form to enter TOTP
$text .= e107::getForm()->open('enter-totp');
$text .= e107::getForm()->text("totp", "", 80, $form_options);
if(!empty($secret))
{
$text .= e107::getForm()->hidden("secret_key", $secret);
}
$text .= "<br>";
$text .= e107::getForm()->button($button_name, LAN_VERIFY, $action);
$text .= e107::getForm()->close();
return $text;
}
private function verifyTotp($user_id = USERID, $totp)
{
$tfa_library = new TwoFactorAuth();
// Retrieve secret_key of this user, stored in the database
$secret_key = e107::getUserExt()->get($user_id, "user_plugin_twofactorauth_secret_key");
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": User ID: ".$user_id);
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": Secret Key: ".$secret_key);
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": Entered TOTP: ".$totp);
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
// Check if the entered TOTP is correct.
if($tfa_library->verifyCode($secret_key, $totp) === true)
{
// TOTP is correct.
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": The TOTP code that was entered, is correct");
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
return true;
}
else
{
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": The TOTP code that was entered, is INCORRECT");
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
return false;
}
}
public function processLogin($user_id = USERID, $totp)
{
if($this->verifyTotp($user_id, $totp))
{
// Continue processing login
$user = e107::user($user_id);
$ulogin = new userlogin();
$ulogin->validLogin($user);
// Get previous page the user was on before logging in.
$redirect_to = e107::getSession('2fa')->get('previous_page');
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": Session Previous page: ".$redirect_to);
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
// Clear session data
e107::getSession('2fa')->clearData();
// Redirect to previous page or otherwise to homepage
if($redirect_to)
{
e107::getRedirect()->redirect($redirect_to);
}
else
{
e107::redirect();
}
}
// The entered TOTP is INCORRECT
else
{
return false;
}
}
public function processFpw($user_id = USERID, $totp)
{
if($this->verifyTotp($user_id, $totp))
{
return true;
}
// The entered TOTP is INCORRECT
else
{
return LAN_2FA_INCORRECT_TOTP;
}
}
public function processEnable($user_id = USERID, $secret_key, $totp)
{
$tfa_library = new TwoFactorAuth();
// Verify code
if($tfa_library->verifyCode($secret_key, $totp) === false)
{
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": Entered TOTP is incorrect: ".$totp);
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": Secret Key: ".$secret_key);
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
e107::getMessage()->addError(LAN_2FA_INCORRECT_TOTP);
return false;
}
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": Entered TOTP is correct. Continue adding secret key to EUF.");
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
if(!e107::getUserExt()->set($user_id, "user_plugin_twofactorauth_secret_key", $secret_key))
{
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": Could not add secret key to EUF");
e107::getAdminLog()->addDebug(e107::getDb()->getLastErrorText());
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
e107::getMessage()->addError(LAN_2FA_DATABASE_ERROR);
return false;
}
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": Secret key has been added to the EUF");
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
return true;
}
public function processDisable($user_id = USERID, $totp)
{
$tfa_library = new TwoFactorAuth();
// Retrieve secret_key of this user, stored in the database
$secret_key = e107::getUserExt()->get($user_id, "user_plugin_twofactorauth_secret_key");
// Verify code
if($tfa_library->verifyCode($secret_key, $totp) === false)
{
e107::getMessage()->addError(LAN_2FA_INCORRECT_TOTP);
return false;
}
if(!e107::getUserExt()->set($user_id, "user_plugin_twofactorauth_secret_key", ''))
{
if($this->tfa_debug)
{
e107::getAdminLog()->addDebug(__LINE__." ".__METHOD__.": Could not empty secret_key EUF field");
e107::getAdminLog()->toFile('twofactorauth', 'TwoFactorAuth Debug Information', true);
}
e107::getMessage()->addError(LAN_2FA_DATABASE_ERROR);
return false;
}
return true;
}
}