Repository navigation
Expand file tree
/
Copy pathtwo_factor_setup.php
More file actions
148 lines (115 loc) · 3.5 KB
/
Copy pathtwo_factor_setup.php
File metadata and controls
148 lines (115 loc) · 3.5 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
<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/two_factor.php';
require_login();
$pageTitle = 'Two-Factor Authentication';
$currentPage = 'account';
$user = get_current_user_record();
if (!$user) {
header('Location: login.php');
exit;
}
$error = '';
$message = '';
$userId = (int)$user['id'];
$email = (string)$user['email'];
$secret = (string)($user['two_factor_secret'] ?? '');
if ($secret === '') {
$secret = two_factor_generate_secret();
$stmt = db()->prepare("
UPDATE users
SET two_factor_secret = :secret
WHERE id = :id
LIMIT 1
");
$stmt->execute([
'secret' => $secret,
'id' => $userId,
]);
$user['two_factor_secret'] = $secret;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verify_csrf_token();
try {
$code = trim((string)($_POST['code'] ?? ''));
if (!two_factor_verify_code($secret, $code)) {
throw new RuntimeException('Invalid authentication code. Please try again.');
}
$stmt = db()->prepare("
UPDATE users
SET two_factor_enabled = 1,
two_factor_confirmed_at = NOW()
WHERE id = :id
LIMIT 1
");
$stmt->execute([
'id' => $userId,
]);
$message = 'Two-factor authentication has been enabled.';
$user['two_factor_enabled'] = 1;
} catch (Throwable $e) {
$error = $e->getMessage();
}
}
$qrUrl = two_factor_qr_url($email, $secret);
require_once __DIR__ . '/includes/header.php';
?>
<h1 class="page-title brand-title-font">Two-Factor Authentication</h1>
<?php if ($message !== ''): ?>
<div class="msg ok"><?= h($message) ?></div>
<?php endif; ?>
<?php if ($error !== ''): ?>
<div class="msg err"><?= h($error) ?></div>
<?php endif; ?>
<div class="card" style="max-width:680px;">
<h2>Authenticator App Setup</h2>
<?php if ((int)($user['two_factor_enabled'] ?? 0) === 1): ?>
<p class="muted">
Two-factor authentication is currently enabled for your account.
</p>
<div class="actions-row">
<a class="btn btn-secondary" href="account.php">Back to Account</a>
</div>
<?php else: ?>
<p class="muted">
Scan this QR code with Google Authenticator, Microsoft Authenticator, 1Password, Authy, or another TOTP app.
</p>
<div style="margin:20px 0; text-align:center;">
<img
src="<?= h($qrUrl) ?>"
alt="Two-factor authentication QR code"
style="width:220px; height:220px; max-width:100%;"
>
</div>
<p class="muted">
If you cannot scan the QR code, manually enter this setup key:
</p>
<div class="card" style="background:#f8fafc; margin-bottom:18px;">
<strong style="word-break:break-all; letter-spacing:1px;">
<?= h($secret) ?>
</strong>
</div>
<form method="post">
<?= csrf_field() ?>
<label for="code">Enter 6-digit code</label>
<input
type="text"
id="code"
name="code"
inputmode="numeric"
pattern="[0-9]{6}"
maxlength="6"
required
autocomplete="one-time-code"
placeholder="123456"
>
<div class="actions-row">
<button type="submit">Enable 2FA</button>
<a class="btn btn-secondary" href="account.php">Cancel</a>
</div>
</form>
<?php endif; ?>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>