forked from projectsend/projectsend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotp-setup.php
More file actions
339 lines (297 loc) · 17.3 KB
/
totp-setup.php
File metadata and controls
339 lines (297 loc) · 17.3 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* TOTP (Authenticator App) setup page
* Users can set up, disable, or regenerate backup codes for TOTP 2FA
*/
require_once 'bootstrap.php';
redirect_if_not_logged_in();
if (!(bool)get_option('two_factor_allow_totp', null, '1')) {
exit_with_error_code(403);
}
$page_title = __('Authenticator App Setup', 'cftp_admin');
$page_id = 'totp_setup';
$active_nav = 'users';
$totp = new \ProjectSend\Classes\Totp();
$user_id = CURRENT_USER_ID;
$is_enabled = $totp->isEnabledForUser($user_id);
$step = isset($_GET['step']) ? $_GET['step'] : ($is_enabled ? 'manage' : 'intro');
// Handle POST actions
if ($_POST) {
switch ($_POST['do']) {
case 'totp_begin':
// Generate a new secret and store in session
$secret = $totp->generateSecret();
$_SESSION['totp_setup_secret'] = $secret;
ps_redirect(BASE_URI . 'totp-setup.php?step=scan');
break;
case 'totp_verify':
// Verify the code against the session-stored secret
$secret = $_SESSION['totp_setup_secret'] ?? null;
if (!$secret) {
$flash->error(__('Setup session expired. Please start again.', 'cftp_admin'));
ps_redirect(BASE_URI . 'totp-setup.php');
break;
}
$code = $_POST['totp_code'];
if ($totp->verifyCode($secret, $code)) {
// Enable TOTP for the user
$totp->enableForUser($user_id, $secret);
// Generate backup codes
$backup_codes = $totp->generateBackupCodes();
$totp->storeBackupCodes($user_id, $backup_codes);
// Store codes temporarily in session for display
$_SESSION['totp_backup_codes'] = $backup_codes;
unset($_SESSION['totp_setup_secret']);
$flash->success(__('Authenticator app has been enabled successfully.', 'cftp_admin'));
ps_redirect(BASE_URI . 'totp-setup.php?step=backup_codes');
} else {
$flash->error(__('Invalid code. Please try again.', 'cftp_admin'));
ps_redirect(BASE_URI . 'totp-setup.php?step=scan');
}
break;
case 'totp_disable':
// Require password confirmation
$user = new \ProjectSend\Classes\Users($user_id);
if (!password_verify($_POST['password'], $user->getRawPassword())) {
$flash->error(__('Incorrect password.', 'cftp_admin'));
ps_redirect(BASE_URI . 'totp-setup.php');
break;
}
$totp->disableForUser($user_id);
$flash->success(__('Authenticator app has been disabled.', 'cftp_admin'));
ps_redirect(BASE_URI . 'totp-setup.php');
break;
case 'totp_regenerate_backup':
// Require password confirmation
$user = new \ProjectSend\Classes\Users($user_id);
if (!password_verify($_POST['password'], $user->getRawPassword())) {
$flash->error(__('Incorrect password.', 'cftp_admin'));
ps_redirect(BASE_URI . 'totp-setup.php');
break;
}
$backup_codes = $totp->generateBackupCodes();
$totp->storeBackupCodes($user_id, $backup_codes);
$_SESSION['totp_backup_codes'] = $backup_codes;
$flash->success(__('New backup codes have been generated.', 'cftp_admin'));
ps_redirect(BASE_URI . 'totp-setup.php?step=backup_codes');
break;
}
}
$csrf_token = getCsrfToken();
// Prepare data for the scan step
if ($step === 'scan') {
$secret = $_SESSION['totp_setup_secret'] ?? null;
if (!$secret) {
ps_redirect(BASE_URI . 'totp-setup.php');
}
$user_data = get_user_by_id($user_id);
$provisioning_uri = $totp->getProvisioningUri($secret, $user_data['email']);
$qr_data_uri = $totp->generateQrCodeDataUri($provisioning_uri);
}
// Get backup codes from session for display
$backup_codes = null;
if ($step === 'backup_codes') {
$backup_codes = $_SESSION['totp_backup_codes'] ?? null;
if ($backup_codes) {
unset($_SESSION['totp_backup_codes']);
}
}
// Get remaining backup codes count for manage step
$remaining_codes = 0;
if ($is_enabled) {
$remaining_codes = $totp->getRemainingBackupCodesCount($user_id);
}
include_once ADMIN_VIEWS_DIR . DS . 'header.php';
?>
<div class="row">
<?php if ($step === 'manage') { ?>
<!-- Sidebar navigation -->
<div class="col-lg-2 d-none d-lg-block">
<nav class="options-section-nav mb-3">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="#section-status"><?php _e('Status', 'cftp_admin'); ?></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section-backup-codes"><?php _e('Backup codes', 'cftp_admin'); ?></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section-disable"><?php _e('Disable authenticator', 'cftp_admin'); ?></a>
</li>
</ul>
</nav>
</div>
<?php } ?>
<!-- Main content area -->
<div class="col-12 col-lg-7">
<div class="ps-card">
<div class="ps-card-body totp-setup">
<?php if ($step === 'intro') { ?>
<h3><?php _e('Set up authenticator app', 'cftp_admin'); ?></h3>
<p><?php _e('Use an authenticator app like Google Authenticator, Authy, or 1Password to generate verification codes for two-factor authentication.', 'cftp_admin'); ?></p>
<p><?php _e('This adds an extra layer of security to your account by requiring a code from your phone in addition to your password.', 'cftp_admin'); ?></p>
<form action="totp-setup.php" method="post">
<?php addCsrf(); ?>
<input type="hidden" name="do" value="totp_begin">
<button type="submit" class="btn btn-primary">
<i class="fa fa-shield"></i> <?php _e('Begin setup', 'cftp_admin'); ?>
</button>
</form>
<?php } elseif ($step === 'scan') { ?>
<h3><?php _e('Scan QR code', 'cftp_admin'); ?></h3>
<p><?php _e('Scan this QR code with your authenticator app:', 'cftp_admin'); ?></p>
<div class="totp-qr-container">
<img src="<?php echo $qr_data_uri; ?>" alt="<?php _e('QR Code', 'cftp_admin'); ?>" />
</div>
<div class="totp-secret-display">
<p><?php _e("Can't scan the code? Enter this key manually:", 'cftp_admin'); ?></p>
<div class="totp-secret-key">
<code id="totp_secret_text"><?php echo htmlspecialchars($secret); ?></code>
<button type="button" class="btn btn-sm btn-outline-secondary" id="copy_secret_btn" title="<?php _e('Copy to clipboard', 'cftp_admin'); ?>">
<i class="fa fa-clipboard"></i>
</button>
</div>
</div>
<div class="options_divide"></div>
<h3><?php _e('Verify setup', 'cftp_admin'); ?></h3>
<p><?php _e('Enter the 6-digit code shown in your authenticator app to verify the setup:', 'cftp_admin'); ?></p>
<form action="totp-setup.php" method="post">
<?php addCsrf(); ?>
<input type="hidden" name="do" value="totp_verify">
<div class="form-group row">
<label class="col-sm-4 control-label"><?php _e('Verification code', 'cftp_admin'); ?></label>
<div class="col-sm-8">
<input type="text" name="totp_code" class="form-control totp-code-input" maxlength="6" pattern="[0-9]{6}" placeholder="000000" autocomplete="off" required autofocus />
</div>
</div>
<div class="after_form_buttons">
<button type="submit" class="btn btn-wide btn-primary"><?php _e('Verify and enable', 'cftp_admin'); ?></button>
<a href="<?php echo BASE_URI; ?>totp-setup.php" class="btn btn-wide btn-outline-secondary"><?php _e('Cancel', 'cftp_admin'); ?></a>
</div>
</form>
<?php } elseif ($step === 'backup_codes') { ?>
<h3><?php _e('Backup codes', 'cftp_admin'); ?></h3>
<div class="alert alert-warning">
<i class="fa fa-exclamation-triangle"></i>
<?php _e('Save these backup codes in a safe place. Each code can only be used once. If you lose access to your authenticator app, you can use these codes to sign in.', 'cftp_admin'); ?>
</div>
<?php if ($backup_codes) { ?>
<div class="totp-backup-codes" id="backup_codes_container">
<div class="backup-codes-grid">
<?php foreach ($backup_codes as $code) { ?>
<div class="backup-code"><code><?php echo htmlspecialchars($code); ?></code></div>
<?php } ?>
</div>
</div>
<div class="totp-backup-actions">
<button type="button" class="btn btn-sm btn-outline-secondary" id="copy_backup_codes_btn">
<i class="fa fa-clipboard"></i> <?php _e('Copy all codes', 'cftp_admin'); ?>
</button>
<button type="button" class="btn btn-sm btn-outline-secondary" id="download_backup_codes_btn">
<i class="fa fa-download"></i> <?php _e('Download as text file', 'cftp_admin'); ?>
</button>
</div>
<?php } else { ?>
<p><?php _e('Backup codes are only shown once when generated. If you need new codes, use the regenerate option.', 'cftp_admin'); ?></p>
<?php } ?>
<div class="options_divide"></div>
<div class="after_form_buttons">
<a href="<?php echo BASE_URI; ?>totp-setup.php" class="btn btn-wide btn-primary"><?php _e('Done', 'cftp_admin'); ?></a>
</div>
<?php } elseif ($step === 'manage') { ?>
<h3 id="section-status"><?php _e('Status', 'cftp_admin'); ?></h3>
<p><?php _e('Your authenticator app is configured and active.', 'cftp_admin'); ?></p>
<div class="form-group row">
<label class="col-sm-4 control-label"><?php _e('Status', 'cftp_admin'); ?></label>
<div class="col-sm-8">
<span class="badge bg-success"><i class="fa fa-check"></i> <?php _e('Active', 'cftp_admin'); ?></span>
</div>
</div>
<div class="form-group row">
<label class="col-sm-4 control-label"><?php _e('Backup codes remaining', 'cftp_admin'); ?></label>
<div class="col-sm-8">
<span><?php echo $remaining_codes; ?></span>
<?php if ($remaining_codes <= 2) { ?>
<span class="text-warning ms-2"><i class="fa fa-exclamation-triangle"></i> <?php _e('Consider regenerating your backup codes.', 'cftp_admin'); ?></span>
<?php } ?>
</div>
</div>
<div class="options_divide"></div>
<h3 id="section-backup-codes"><?php _e('Regenerate backup codes', 'cftp_admin'); ?></h3>
<p><?php _e('Backup codes are single-use codes that allow you to sign in if you lose access to your authenticator app (for example, if your phone is lost, stolen, or reset). Each code can only be used once, and they do not expire until used.', 'cftp_admin'); ?></p>
<p><?php _e('Regenerating will immediately invalidate all of your current backup codes and replace them with a new set. Make sure you are in a position to save the new codes before proceeding, as they will only be displayed once.', 'cftp_admin'); ?></p>
<div class="after_form_buttons">
<button type="button" class="btn btn-wide btn-warning" data-bs-toggle="modal" data-bs-target="#regenerateBackupCodesModal">
<?php _e('Regenerate backup codes', 'cftp_admin'); ?>
</button>
</div>
<div class="options_divide"></div>
<h3 id="section-disable"><?php _e('Disable authenticator', 'cftp_admin'); ?></h3>
<p><?php _e('If you no longer want to use an authenticator app for two-factor authentication, you can disable it here. This will permanently remove the authenticator app link and delete all of your backup codes from the system.', 'cftp_admin'); ?></p>
<p><?php _e('After disabling, your account will fall back to email-based verification codes if two-factor authentication is required by your administrator, or no second factor at all if it is optional.', 'cftp_admin'); ?></p>
<div class="after_form_buttons">
<button type="button" class="btn btn-wide btn-danger" data-bs-toggle="modal" data-bs-target="#disableAuthenticatorModal">
<?php _e('Disable authenticator', 'cftp_admin'); ?>
</button>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
<?php if ($step === 'manage') { ?>
<!-- Regenerate Backup Codes Modal -->
<div class="modal fade" id="regenerateBackupCodesModal" tabindex="-1" aria-labelledby="regenerateBackupCodesModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="totp-setup.php" method="post">
<?php addCsrf(); ?>
<input type="hidden" name="do" value="totp_regenerate_backup">
<div class="modal-header">
<h5 class="modal-title" id="regenerateBackupCodesModalLabel"><?php _e('Regenerate backup codes', 'cftp_admin'); ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p><?php _e('Your current backup codes will be invalidated and replaced with a new set. Enter your password to confirm.', 'cftp_admin'); ?></p>
<div class="form-group">
<label for="regenerate_password"><?php _e('Password', 'cftp_admin'); ?></label>
<input type="password" name="password" id="regenerate_password" class="form-control mt-1" required />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"><?php _e('Cancel', 'cftp_admin'); ?></button>
<button type="submit" class="btn btn-warning"><?php _e('Regenerate', 'cftp_admin'); ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- Disable Authenticator Modal -->
<div class="modal fade" id="disableAuthenticatorModal" tabindex="-1" aria-labelledby="disableAuthenticatorModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="totp-setup.php" method="post">
<?php addCsrf(); ?>
<input type="hidden" name="do" value="totp_disable">
<div class="modal-header">
<h5 class="modal-title" id="disableAuthenticatorModalLabel"><?php _e('Disable authenticator', 'cftp_admin'); ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p><?php _e('Your authenticator app and all backup codes will be removed from your account. Enter your password to confirm.', 'cftp_admin'); ?></p>
<div class="form-group">
<label for="disable_password"><?php _e('Password', 'cftp_admin'); ?></label>
<input type="password" name="password" id="disable_password" class="form-control mt-1" required />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"><?php _e('Cancel', 'cftp_admin'); ?></button>
<button type="submit" class="btn btn-danger"><?php _e('Disable', 'cftp_admin'); ?></button>
</div>
</form>
</div>
</div>
</div>
<?php } ?>
<?php
include_once ADMIN_VIEWS_DIR . DS . 'footer.php';