-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
293 lines (240 loc) · 10.3 KB
/
setup.php
File metadata and controls
293 lines (240 loc) · 10.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
<?php
declare(strict_types=1);
require __DIR__ . '/config.php';
function setup_is_local_request(): bool
{
$ip = get_client_ip();
return in_array($ip, ['127.0.0.1', '::1'], true);
}
function setup_token_is_valid(): bool
{
$configuredToken = env_value('SETUP_TOKEN', '') ?? '';
if ($configuredToken === '') {
return setup_is_local_request();
}
$submittedToken = $_POST['setup_token'] ?? $_GET['token'] ?? '';
return is_string($submittedToken) && hash_equals($configuredToken, $submittedToken);
}
function setup_table_columns(PDO $pdo, string $table): array
{
$stmt = $pdo->prepare(
'SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?
ORDER BY ORDINAL_POSITION'
);
$stmt->execute([$table]);
$columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (!is_array($columns)) {
return [];
}
return array_map('strval', $columns);
}
function setup_admin_count(PDO $pdo): int
{
try {
$stmt = $pdo->prepare('SELECT COUNT(*) FROM admins');
$stmt->execute();
return (int) $stmt->fetchColumn();
} catch (Throwable $e) {
safe_log('Setup could not count admins', ['message' => $e->getMessage()]);
return -1;
}
}
function setup_password_errors(string $password): array
{
$errors = [];
if (strlen($password) < 6) {
$errors[] = 'Password must be at least 6 characters.';
}
return $errors;
}
function setup_insert_first_admin(PDO $pdo, array $columns, string $name, string $email, string $password): int
{
$hasPasswordHash = in_array('password_hash', $columns, true);
$hasLegacyPassword = in_array('password', $columns, true);
if (!$hasPasswordHash && !$hasLegacyPassword) {
throw new RuntimeException('No supported password column exists.');
}
$insert = [];
$values = [];
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
if (in_array('name', $columns, true)) {
$insert['name'] = $name;
}
$insert['email'] = $email;
if ($hasPasswordHash) {
$insert['password_hash'] = $passwordHash;
}
if ($hasLegacyPassword) {
$insert['password'] = $passwordHash;
}
if (in_array('twofa_enabled', $columns, true)) {
$insert['twofa_enabled'] = 0;
}
if (in_array('is_active', $columns, true)) {
$insert['is_active'] = 1;
}
if (in_array('profile_photo', $columns, true)) {
$insert['profile_photo'] = '';
}
if (in_array('created_at', $columns, true)) {
$insert['created_at'] = now_sql();
}
if (in_array('updated_at', $columns, true)) {
$insert['updated_at'] = now_sql();
}
foreach ($insert as $value) {
$values[] = $value;
}
$columnSql = implode(', ', array_map(static fn (string $column): string => '`' . $column . '`', array_keys($insert)));
$placeholders = implode(', ', array_fill(0, count($insert), '?'));
$stmt = $pdo->prepare('INSERT INTO admins (' . $columnSql . ') VALUES (' . $placeholders . ')');
$stmt->execute($values);
return (int) $pdo->lastInsertId();
}
$appName = (string) app_config('app_name');
$columns = setup_table_columns($pdo, 'admins');
$adminCount = $columns === [] ? -1 : setup_admin_count($pdo);
$tokenAllowed = setup_token_is_valid();
$setupAvailable = $tokenAllowed && $columns !== [] && $adminCount === 0;
$success = false;
$error = '';
$messages = [];
csrf_token();
if (!$tokenAllowed) {
$error = 'Setup is locked. Configure SETUP_TOKEN and open setup.php?token=your-token.';
} elseif ($columns === []) {
$error = 'Database schema is not installed. Import database/schema.sql first.';
} elseif ($adminCount > 0) {
$error = 'Setup is already complete. Admin credentials are configured.';
} elseif ($adminCount < 0) {
$error = 'Setup cannot verify the admin table.';
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $setupAvailable) {
$postedToken = $_POST['csrf'] ?? null;
if (!csrf_validate(is_string($postedToken) ? $postedToken : null)) {
csrf_rotate();
$error = 'Your setup session expired. Refresh and try again.';
} else {
$name = trim((string) ($_POST['name'] ?? ''));
$email = strtolower(trim((string) ($_POST['email'] ?? '')));
$password = (string) ($_POST['password'] ?? '');
$confirmPassword = (string) ($_POST['password_confirm'] ?? '');
if ($name === '') {
$name = 'Admin';
}
if (strlen($name) > 100) {
$messages[] = 'Name must be 100 characters or fewer.';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 255) {
$messages[] = 'Enter a valid admin email address.';
}
if (!hash_equals($password, $confirmPassword)) {
$messages[] = 'Password confirmation does not match.';
}
$messages = array_merge($messages, setup_password_errors($password));
if ($messages === []) {
try {
$pdo->beginTransaction();
$lockedCount = setup_admin_count($pdo);
if ($lockedCount !== 0) {
throw new RuntimeException('Admin setup has already been completed.');
}
$adminId = setup_insert_first_admin($pdo, $columns, $name, $email, $password);
$pdo->commit();
csrf_rotate();
admin_log($pdo, $adminId, 'setup_admin_created');
safe_log('First admin account created', ['admin_id' => $adminId, 'email_hash' => keyed_hash($email)]);
$success = true;
$setupAvailable = false;
} catch (Throwable $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
safe_log('First admin setup failed', ['message' => $e->getMessage()]);
$error = 'Could not create the admin account. Check server logs.';
}
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>First Admin Setup | <?= h($appName) ?></title>
<link rel="stylesheet" href="assets/css/admin.css">
</head>
<body class="premium-login-page">
<main class="premium-login-shell setup-shell" aria-labelledby="setup-title">
<section class="premium-login-card">
<div class="premium-card-shine" aria-hidden="true"></div>
<header class="premium-login-brand">
<div class="premium-logo-wrap">
<img src="assets/images/logo_main.png" alt="<?= h($appName) ?> logo">
</div>
<h1 id="setup-title">First <span>Setup</span></h1>
<p>Secureauth Security Gateway</p>
</header>
<?php if ($success): ?>
<div class="premium-alert premium-alert-success" role="status">
<span class="premium-alert-mark" aria-hidden="true">OK</span>
<div>Admin account created. Setup is now disabled.</div>
</div>
<a class="premium-link-button" href="index.php">Go to login</a>
<?php else: ?>
<?php if ($error !== ''): ?>
<div class="premium-alert premium-alert-warning" role="alert">
<span class="premium-alert-mark" aria-hidden="true">!</span>
<div><?= h($error) ?></div>
</div>
<?php endif; ?>
<?php if ($messages !== []): ?>
<div class="premium-alert premium-alert-error" role="alert">
<span class="premium-alert-mark" aria-hidden="true">!</span>
<div>
<?php foreach ($messages as $message): ?>
<p><?= h($message) ?></p>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php if ($setupAvailable): ?>
<form method="post" class="premium-auth-form" autocomplete="off" novalidate>
<input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
<?php if ((env_value('SETUP_TOKEN', '') ?? '') !== ''): ?>
<input type="hidden" name="setup_token" value="<?= h((string) ($_GET['token'] ?? $_POST['setup_token'] ?? '')) ?>">
<?php endif; ?>
<div class="premium-field">
<label for="name">Admin Name</label>
<input id="name" name="name" type="text" maxlength="100" autocomplete="name" placeholder="Admin" value="<?= h((string) ($_POST['name'] ?? '')) ?>">
</div>
<div class="premium-field">
<label for="email">Admin Email</label>
<input id="email" name="email" type="email" maxlength="255" autocomplete="username" required placeholder="admin@example.com" value="<?= h((string) ($_POST['email'] ?? '')) ?>">
</div>
<div class="premium-field">
<label for="password">Password</label>
<input id="password" name="password" type="password" autocomplete="new-password" required placeholder="Minimum 6 characters">
</div>
<div class="premium-field">
<label for="password_confirm">Confirm Password</label>
<input id="password_confirm" name="password_confirm" type="password" autocomplete="new-password" required placeholder="Repeat password">
</div>
<button class="premium-submit" type="submit">Create Admin</button>
</form>
<?php endif; ?>
<?php endif; ?>
</section>
<p class="premium-login-footnote">
<svg class="premium-footer-lock" aria-hidden="true" viewBox="0 0 24 24" focusable="false">
<rect x="5" y="11" width="14" height="10" rx="2"></rect>
<path d="M8 11V7a4 4 0 0 1 8 0v4"></path>
</svg>
First-Time Credential Setup
</p>
</main>
</body>
</html>