-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.php
188 lines (150 loc) · 5.11 KB
/
setup.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
<?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://www.gnu.org/licenses/gpl.txt)
*
*/
if(!defined('e107_INIT'))
{
require_once(__DIR__.'/../../class2.php');
}
// Make this page inaccessible when plugin is not installed.
if (!e107::isInstalled('twofactorauth'))
{
e107::redirect();
exit;
}
// Only show this page when user is logged in already .
if(!USER)
{
e107::redirect();
}
// Load required files (TwoFactorAuth Library and twofactorauth class)
e107_require_once(e_PLUGIN.'twofactorauth/vendor/autoload.php');
use \RobThree\Auth\TwoFactorAuth;
use \RobThree\Auth\Providers\Qr\EndroidQrCodeProvider;
$tfa_library = new TwoFactorAuth(new EndroidQrCodeProvider());
require_once(e_PLUGIN."twofactorauth/twofactorauth_class.php");
$tfa_class = new tfa_class();
// Load LAN files
e107::lan('twofactorauth', false, true);
$caption = LAN_2FA_TITLE." - ".LAN_SETTINGS;
e107::title($caption);
require_once(HEADERF);
$text = "";
$usersettings_url = e107::getUrl()->create('user/myprofile/edit', array('id' => USERID));
// Check if 2FA is already enabled for current user
$tfaActivated = $tfa_class->tfaActivated(USERID) ? true : false;
// Setting up 2FA
if(!$tfaActivated && isset($_POST['enter-totp-enable']))
{
$secret_key = (string) $_POST['secret_key'];
$totp = intval($_POST['totp']);
$totp = (string) $totp;
$text = '';
if($tfa_class->processEnable(USERID, $secret_key, $totp))
{
e107::getMessage()->addSuccess(e107::getParser()->toHTML(LAN_2FA_ENABLED, true));
// Check if Recovery Codes functionality is enabled, and if yes, generate new recovery codes
if(e107::getPlugPref('twofactorauth', 'tfa_recoverycodes'))
{
if($recovery_codes = $tfa_class->generateRecoveryCodes(USERID))
{
// TODO Let user download a txt file with the codes
// TODO allow some templating?
$recovery_codes1 = array_slice($recovery_codes, 0, 5);
$recovery_codes2 = array_slice($recovery_codes, 5);
$recovery_codes1 = implode("<br>", $recovery_codes1);
$recovery_codes2 = implode("<br>", $recovery_codes2);
$text .= "<p><strong>".LAN_2FA_RECOVERYCODES_GENERATED."</strong></p>";
$text .= '<div class="row justify-content-center">';
$text .= ' <div class="col col-md-6 text-center">
'.$recovery_codes1.'
<br>
</div>
';
$text .= ' <div class="col col-md-6 text-center">
'.$recovery_codes2.'
</div>
';
$text .= '</div>';
$text .= "<br>";
}
else
{
e107::getMessage()->addError(e107::getParser()->toHTML(LAN_2FA_RECOVERYCODES_GENERATED_ERROR, true));
}
}
$text .= "<a class='btn btn-primary' href='".$usersettings_url."'>".LAN_2FA_RETURN_USERSETTINGS."</a>.";
e107::getRender()->tablerender($caption, e107::getMessage()->render().$text);
require_once(FOOTERF);
exit;
}
}
if($tfaActivated && isset($_POST['enter-totp-disable']))
{
$totp = intval($_POST['totp']);
$totp = (string) $totp;
// Check if Recovery Codes are set, and if yes, remove them
if(e107::getPlugPref('twofactorauth', 'tfa_recoverycodes'))
{
if($removed = $tfa_class->removeRecoveryCodes(USERID))
{
// Recovery codes removed, now disable 2FA
if($tfa_class->processDisable(USERID, $totp))
{
e107::getMessage()->addSuccess(e107::getParser()->toHTML(LAN_2FA_DISABLED, true));
$text = "<a class='btn btn-primary' href='".$usersettings_url."'>".LAN_2FA_RETURN_USERSETTINGS."</a>.";
}
else
{
e107::getMessage()->addError(e107::getParser()->toHTML(LAN_2FA_DISABLED_ERROR, true));
}
}
else
{
e107::getMessage()->addError(LAN_2FA_RECOVERYCODES_REMOVED_ERROR);
}
}
e107::getRender()->tablerender($caption, e107::getMessage()->render().$text);
require_once(FOOTERF);
exit;
}
// 2FA not setup yet, show instructions
if(!$tfaActivated)
{
// Generate Secret Key
$secret = $tfa_library->createSecret();
// Setup label - defaults to SITENAME
$label = e107::getPlugPref('twofactorauth', 'tfa_label');
if(empty($label))
{
$label = SITENAME;
}
$instructions1 = str_replace(
array("[", "]"),
array("<strong><a href='https://github.com/e107inc/twofactorauth#recommended-authenticator-applications' target='_blank'>", "</a></strong>"),
LAN_2FA_ENABLE_INSTRUCTIONS1
);
e107::getMessage()->addInfo($instructions1);
$text .= '<img class="center-block" src="' . $tfa_library->getQRCodeImageAsDataUri($label, $secret) . '"><br>';
$text .= '<p class="text-center font-italic">'.chunk_split($secret, 4, ' ').'</p>';
$text .= '<p>'.LAN_2FA_ENABLE_INSTRUCTIONS2.'</p>';
$text .= $tfa_class->showTotpInputForm('enable', $secret);
// TEMP FOR DEV PURPOSES
// $correct_totp = $tfa_library->getCode($secret);
// $text .= $correct_totp;
}
// 2FA is already activated, show option(s) to disable.
else
{
e107::getMessage()->addInfo(e107::getParser()->toHTML(LAN_2FA_DISABLE_INSTRUCTIONS, true));
$text .= $tfa_class->showTotpInputForm('disable');
}
// Let's render and show it all!
e107::getRender()->tablerender($caption, e107::getMessage()->render().$text);
require_once(FOOTERF);
exit;