-
-
Notifications
You must be signed in to change notification settings - Fork 458
/
2fa.php
155 lines (138 loc) · 4.52 KB
/
2fa.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
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can also view it online at
* https://files.froxlor.org/misc/COPYING.txt
*
* @copyright the authors
* @author Froxlor team <team@froxlor.org>
* @license https://files.froxlor.org/misc/COPYING.txt GPLv2
*/
if (!defined('AREA')) {
header("Location: index.php");
exit();
}
use Froxlor\Database\Database;
use Froxlor\FroxlorLogger;
use Froxlor\FroxlorTwoFactorAuth;
use Froxlor\Settings;
use Froxlor\UI\Panel\UI;
use Froxlor\UI\Request;
use Froxlor\UI\Response;
use Froxlor\PhpHelper;
use Froxlor\User;
if (Settings::Get('2fa.enabled') != '1') {
Response::dynamicError('2fa.2fa_not_activated');
}
// This file is being included in admin_index and customer_index
// and therefore does not need to require lib/init.php
if (AREA == 'admin') {
$upd_stmt = Database::prepare("UPDATE `" . TABLE_PANEL_ADMINS . "` SET `type_2fa` = :t2fa, `data_2fa` = :d2fa WHERE adminid = :id");
$uid = $userinfo['adminid'];
} elseif (AREA == 'customer') {
$upd_stmt = Database::prepare("UPDATE `" . TABLE_PANEL_CUSTOMERS . "` SET `type_2fa` = :t2fa, `data_2fa` = :d2fa WHERE customerid = :id");
$uid = $userinfo['customerid'];
}
$success_message = "";
$tfa = new FroxlorTwoFactorAuth('Froxlor ' . Settings::Get('system.hostname'));
// do the delete and then just show a success-message
if ($action == 'delete') {
Database::pexecute($upd_stmt, [
't2fa' => 0,
'd2fa' => "",
'id' => $uid
]);
Response::standardSuccess('2fa.2fa_removed');
} elseif ($action == 'preadd') {
$type = Request::post('type_2fa', '0');
$data = "";
if ($type > 0) {
// generate secret for TOTP
$data = $tfa->createSecret();
$userinfo['type_2fa'] = $type;
$userinfo['data_2fa'] = $data;
$userinfo['2fa_unsaved'] = true;
// if type = email, send a code there for confirmation
if ($type == 1) {
$code = $tfa->getCode($data);
$_mailerror = false;
$mailerr_msg = "";
$replace_arr = [
'CODE' => $code
];
$mail_body = html_entity_decode(PhpHelper::replaceVariables(lng('mails.2fa.mailbody'), $replace_arr));
try {
$mail->Subject = lng('mails.2fa.subject');
$mail->AltBody = $mail_body;
$mail->MsgHTML(str_replace("\n", "<br />", $mail_body));
$mail->AddAddress($userinfo['email'], User::getCorrectUserSalutation($userinfo));
$mail->Send();
} catch (\PHPMailer\PHPMailer\Exception $e) {
$mailerr_msg = $e->errorMessage();
$_mailerror = true;
} catch (Exception $e) {
$mailerr_msg = $e->getMessage();
$_mailerror = true;
}
if ($_mailerror) {
Response::dynamicError($mailerr_msg);
}
}
UI::twig()->addGlobal('userinfo', $userinfo);
} else {
Response::dynamicError('Select one of the possible values for 2FA');
}
} elseif ($action == 'add') {
$type = Request::post('type_2fa', '0');
$data = Request::post('data_2fa', '');
$code = Request::post('codevalidation', '');
// validate
$result = $tfa->verifyCode($data, $code, 3);
if ($result) {
if ($type == 0 || $type == 1) {
// no fixed secret for email validation, the validation code will be set on the fly
$data = "";
}
Database::pexecute($upd_stmt, [
't2fa' => $type,
'd2fa' => $data,
'id' => $uid
]);
Response::standardSuccess('2fa.2fa_added', $filename);
}
Response::dynamicError('Invalid/wrong code');
}
$log->logAction(FroxlorLogger::USR_ACTION, LOG_NOTICE, "viewed 2fa::overview");
$type_select_values = [];
$ga_qrcode = '';
if ($userinfo['type_2fa'] == '0') {
// available types
$type_select_values = [
0 => '-',
1 => 'E-Mail',
2 => 'Authenticator'
];
asort($type_select_values);
} elseif ($userinfo['type_2fa'] == '1') {
// email 2fa enabled
} elseif ($userinfo['type_2fa'] == '2') {
// authenticator 2fa enabled
$ga_qrcode = $tfa->getQRCodeImageAsDataUri($userinfo['loginname'], $userinfo['data_2fa']);
}
UI::view('user/2fa.html.twig', [
'type_select_values' => $type_select_values,
'ga_qrcode' => $ga_qrcode
]);