first-run setup form — create credentials via UI, stored in /data/.auth
Publish Docker image / build (push) Failing after 1m54s
Publish Docker image / build (push) Failing after 1m54s
This commit is contained in:
1 parent
e54fec540b
commit
123720d39a
2 files changed
+51
-20
No files matched your search
+51
-18
@@ -3,13 +3,25 @@
|
|||||||
define('CADDY_ADMIN_URL', getenv('CADDY_ADMIN_URL') ?: 'http://localhost:2019');
|
define('CADDY_ADMIN_URL', getenv('CADDY_ADMIN_URL') ?: 'http://localhost:2019');
|
||||||
define('CADDYFILE_STORE', getenv('CADDYFILE_STORE') ?: '/data/Caddyfile');
|
define('CADDYFILE_STORE', getenv('CADDYFILE_STORE') ?: '/data/Caddyfile');
|
||||||
define('CADDYFILE_IMPORT', getenv('CADDYFILE_IMPORT') ?: '');
|
define('CADDYFILE_IMPORT', getenv('CADDYFILE_IMPORT') ?: '');
|
||||||
define('AUTH_USER', getenv('AUTH_USER') ?: '');
|
define('AUTH_FILE', '/data/.auth');
|
||||||
define('AUTH_PASS', getenv('AUTH_PASS') ?: ''); // bcrypt hash
|
|
||||||
|
|
||||||
// ─── Session / Auth ──────────────────────────────────────────────────────────
|
// ─── Session / Auth ──────────────────────────────────────────────────────────
|
||||||
|
// Credentials: env vars take priority, then /data/.auth, then null (first run)
|
||||||
|
function loadAuth(): ?array {
|
||||||
|
$u = getenv('AUTH_USER') ?: '';
|
||||||
|
$p = getenv('AUTH_PASS') ?: '';
|
||||||
|
if ($u !== '' && $p !== '') return ['user' => $u, 'pass' => $p];
|
||||||
|
if (file_exists(AUTH_FILE)) {
|
||||||
|
$d = json_decode(file_get_contents(AUTH_FILE), true);
|
||||||
|
if (!empty($d['user']) && !empty($d['pass'])) return $d;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
session_start();
|
session_start();
|
||||||
$auth_enabled = AUTH_USER !== '' && AUTH_PASS !== '';
|
$auth = loadAuth();
|
||||||
$logged_in = !$auth_enabled || !empty($_SESSION['koncad_auth']);
|
$first_run = $auth === null;
|
||||||
|
$logged_in = !$first_run && !empty($_SESSION['koncad_auth']);
|
||||||
|
|
||||||
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||||
function dedent(array $lines): array {
|
function dedent(array $lines): array {
|
||||||
@@ -114,10 +126,20 @@ if (isset($_GET['action'])) {
|
|||||||
|
|
||||||
switch ($_GET['action']) {
|
switch ($_GET['action']) {
|
||||||
|
|
||||||
|
case 'setup':
|
||||||
|
if (!$first_run) { echo json_encode(['ok' => false, 'error' => 'Already configured']); exit; }
|
||||||
|
$user = trim($_POST['user'] ?? '');
|
||||||
|
$pass = $_POST['pass'] ?? '';
|
||||||
|
if (!$user || !$pass) { echo json_encode(['ok' => false, 'error' => 'Username and password required']); exit; }
|
||||||
|
file_put_contents(AUTH_FILE, json_encode(['user' => $user, 'pass' => password_hash($pass, PASSWORD_DEFAULT)]));
|
||||||
|
$_SESSION['koncad_auth'] = true;
|
||||||
|
echo json_encode(['ok' => true]);
|
||||||
|
exit;
|
||||||
|
|
||||||
case 'login':
|
case 'login':
|
||||||
$user = trim($_POST['user'] ?? '');
|
$user = trim($_POST['user'] ?? '');
|
||||||
$pass = $_POST['pass'] ?? '';
|
$pass = $_POST['pass'] ?? '';
|
||||||
if ($auth_enabled && $user === AUTH_USER && password_verify($pass, AUTH_PASS)) {
|
if (!$first_run && $user === $auth['user'] && password_verify($pass, $auth['pass'])) {
|
||||||
$_SESSION['koncad_auth'] = true;
|
$_SESSION['koncad_auth'] = true;
|
||||||
echo json_encode(['ok' => true]);
|
echo json_encode(['ok' => true]);
|
||||||
} else {
|
} else {
|
||||||
@@ -131,7 +153,7 @@ if (isset($_GET['action'])) {
|
|||||||
exit;
|
exit;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if ($auth_enabled && !$logged_in) {
|
if (!$first_run && !$logged_in) {
|
||||||
echo json_encode(['ok' => false, 'error' => 'Unauthorized']);
|
echo json_encode(['ok' => false, 'error' => 'Unauthorized']);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
@@ -191,17 +213,26 @@ if (isset($_GET['action'])) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Login page ───────────────────────────────────────────────────────────────
|
// ─── Auth pages (setup / login) ───────────────────────────────────────────────
|
||||||
if ($auth_enabled && !$logged_in):
|
if ($first_run || !$logged_in):
|
||||||
$login_error = '';
|
$page_error = '';
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
$u = trim($_POST['user'] ?? '');
|
$u = trim($_POST['user'] ?? '');
|
||||||
$p = $_POST['pass'] ?? '';
|
$p = $_POST['pass'] ?? '';
|
||||||
if ($u === AUTH_USER && password_verify($p, AUTH_PASS)) {
|
if ($first_run) {
|
||||||
$_SESSION['koncad_auth'] = true;
|
if ($u && $p) {
|
||||||
header('Location: /'); exit;
|
file_put_contents(AUTH_FILE, json_encode(['user' => $u, 'pass' => password_hash($p, PASSWORD_DEFAULT)]));
|
||||||
|
$_SESSION['koncad_auth'] = true;
|
||||||
|
header('Location: /'); exit;
|
||||||
|
}
|
||||||
|
$page_error = 'Username and password required';
|
||||||
|
} else {
|
||||||
|
if ($u === $auth['user'] && password_verify($p, $auth['pass'])) {
|
||||||
|
$_SESSION['koncad_auth'] = true;
|
||||||
|
header('Location: /'); exit;
|
||||||
|
}
|
||||||
|
$page_error = 'Invalid credentials';
|
||||||
}
|
}
|
||||||
$login_error = 'Invalid credentials';
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
@@ -209,13 +240,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>KonCad — Login</title>
|
<title>KonCad — <?= $first_run ? 'Setup' : 'Login' ?></title>
|
||||||
<style>
|
<style>
|
||||||
:root { --bg:#0f1117; --surface:#1a1d27; --border:#2a2d3a; --text:#e2e8f0; --muted:#64748b; --accent:#7c8cf8; --error:#f87171; }
|
:root { --bg:#0f1117; --surface:#1a1d27; --border:#2a2d3a; --text:#e2e8f0; --muted:#64748b; --accent:#7c8cf8; --error:#f87171; }
|
||||||
* { box-sizing:border-box; margin:0; padding:0; }
|
* { box-sizing:border-box; margin:0; padding:0; }
|
||||||
body { background:var(--bg); color:var(--text); font-family:system-ui,sans-serif; min-height:100vh; display:flex; align-items:center; justify-content:center; }
|
body { background:var(--bg); color:var(--text); font-family:system-ui,sans-serif; min-height:100vh; display:flex; align-items:center; justify-content:center; }
|
||||||
.card { background:var(--surface); border:1px solid var(--border); border-radius:10px; padding:2rem; width:100%; max-width:360px; }
|
.card { background:var(--surface); border:1px solid var(--border); border-radius:10px; padding:2rem; width:100%; max-width:360px; }
|
||||||
h1 { font-size:1.2rem; font-weight:700; color:var(--accent); margin-bottom:1.5rem; }
|
h1 { font-size:1.2rem; font-weight:700; color:var(--accent); margin-bottom:0.4rem; }
|
||||||
|
.subtitle { font-size:0.8rem; color:var(--muted); margin-bottom:1.5rem; }
|
||||||
label { display:block; font-size:0.8rem; color:var(--muted); margin-bottom:0.3rem; }
|
label { display:block; font-size:0.8rem; color:var(--muted); margin-bottom:0.3rem; }
|
||||||
input { width:100%; background:#0d0f18; border:1px solid var(--border); border-radius:6px; color:var(--text); font-size:0.9rem; padding:0.5rem 0.75rem; outline:none; margin-bottom:1rem; }
|
input { width:100%; background:#0d0f18; border:1px solid var(--border); border-radius:6px; color:var(--text); font-size:0.9rem; padding:0.5rem 0.75rem; outline:none; margin-bottom:1rem; }
|
||||||
input:focus { border-color:var(--accent); }
|
input:focus { border-color:var(--accent); }
|
||||||
@@ -227,13 +259,14 @@ button:hover { background:#9ba8ff; }
|
|||||||
<body>
|
<body>
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<h1>KonCad</h1>
|
<h1>KonCad</h1>
|
||||||
<?php if ($login_error): ?><div class="error"><?= htmlspecialchars($login_error) ?></div><?php endif; ?>
|
<div class="subtitle"><?= $first_run ? 'Create your login credentials' : 'Sign in to continue' ?></div>
|
||||||
|
<?php if ($page_error): ?><div class="error"><?= htmlspecialchars($page_error) ?></div><?php endif; ?>
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<label>Username</label>
|
<label>Username</label>
|
||||||
<input type="text" name="user" autofocus autocomplete="username">
|
<input type="text" name="user" autofocus autocomplete="username">
|
||||||
<label>Password</label>
|
<label>Password</label>
|
||||||
<input type="password" name="pass" autocomplete="current-password">
|
<input type="password" name="pass" autocomplete="<?= $first_run ? 'new-password' : 'current-password' ?>">
|
||||||
<button type="submit">Log in</button>
|
<button type="submit"><?= $first_run ? 'Create account' : 'Log in' ?></button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
CADDY_ADMIN_URL: http://caddy:2019
|
CADDY_ADMIN_URL: http://caddy:2019
|
||||||
CADDYFILE_IMPORT: /caddy/Caddyfile
|
CADDYFILE_IMPORT: /caddy/Caddyfile
|
||||||
# AUTH_USER: admin
|
|
||||||
# AUTH_PASS: # bcrypt hash — generate: php -r "echo password_hash('yourpass', PASSWORD_DEFAULT);"
|
|
||||||
networks:
|
networks:
|
||||||
- caddy-network
|
- caddy-network
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user