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
+49
-18
No files matched your search
+49
-16
@@ -3,13 +3,25 @@
|
||||
define('CADDY_ADMIN_URL', getenv('CADDY_ADMIN_URL') ?: 'http://localhost:2019');
|
||||
define('CADDYFILE_STORE', getenv('CADDYFILE_STORE') ?: '/data/Caddyfile');
|
||||
define('CADDYFILE_IMPORT', getenv('CADDYFILE_IMPORT') ?: '');
|
||||
define('AUTH_USER', getenv('AUTH_USER') ?: '');
|
||||
define('AUTH_PASS', getenv('AUTH_PASS') ?: ''); // bcrypt hash
|
||||
define('AUTH_FILE', '/data/.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();
|
||||
$auth_enabled = AUTH_USER !== '' && AUTH_PASS !== '';
|
||||
$logged_in = !$auth_enabled || !empty($_SESSION['koncad_auth']);
|
||||
$auth = loadAuth();
|
||||
$first_run = $auth === null;
|
||||
$logged_in = !$first_run && !empty($_SESSION['koncad_auth']);
|
||||
|
||||
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||
function dedent(array $lines): array {
|
||||
@@ -114,10 +126,20 @@ if (isset($_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':
|
||||
$user = trim($_POST['user'] ?? '');
|
||||
$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;
|
||||
echo json_encode(['ok' => true]);
|
||||
} else {
|
||||
@@ -131,7 +153,7 @@ if (isset($_GET['action'])) {
|
||||
exit;
|
||||
|
||||
default:
|
||||
if ($auth_enabled && !$logged_in) {
|
||||
if (!$first_run && !$logged_in) {
|
||||
echo json_encode(['ok' => false, 'error' => 'Unauthorized']);
|
||||
exit;
|
||||
}
|
||||
@@ -191,17 +213,26 @@ if (isset($_GET['action'])) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// ─── Login page ───────────────────────────────────────────────────────────────
|
||||
if ($auth_enabled && !$logged_in):
|
||||
$login_error = '';
|
||||
// ─── Auth pages (setup / login) ───────────────────────────────────────────────
|
||||
if ($first_run || !$logged_in):
|
||||
$page_error = '';
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$u = trim($_POST['user'] ?? '');
|
||||
$p = $_POST['pass'] ?? '';
|
||||
if ($u === AUTH_USER && password_verify($p, AUTH_PASS)) {
|
||||
if ($first_run) {
|
||||
if ($u && $p) {
|
||||
file_put_contents(AUTH_FILE, json_encode(['user' => $u, 'pass' => password_hash($p, PASSWORD_DEFAULT)]));
|
||||
$_SESSION['koncad_auth'] = true;
|
||||
header('Location: /'); exit;
|
||||
}
|
||||
$login_error = 'Invalid credentials';
|
||||
$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';
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
@@ -209,13 +240,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>KonCad — Login</title>
|
||||
<title>KonCad — <?= $first_run ? 'Setup' : 'Login' ?></title>
|
||||
<style>
|
||||
:root { --bg:#0f1117; --surface:#1a1d27; --border:#2a2d3a; --text:#e2e8f0; --muted:#64748b; --accent:#7c8cf8; --error:#f87171; }
|
||||
* { 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; }
|
||||
.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; }
|
||||
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); }
|
||||
@@ -227,13 +259,14 @@ button:hover { background:#9ba8ff; }
|
||||
<body>
|
||||
<div class="card">
|
||||
<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">
|
||||
<label>Username</label>
|
||||
<input type="text" name="user" autofocus autocomplete="username">
|
||||
<label>Password</label>
|
||||
<input type="password" name="pass" autocomplete="current-password">
|
||||
<button type="submit">Log in</button>
|
||||
<input type="password" name="pass" autocomplete="<?= $first_run ? 'new-password' : 'current-password' ?>">
|
||||
<button type="submit"><?= $first_run ? 'Create account' : 'Log in' ?></button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
@@ -11,8 +11,6 @@ services:
|
||||
environment:
|
||||
CADDY_ADMIN_URL: http://caddy:2019
|
||||
CADDYFILE_IMPORT: /caddy/Caddyfile
|
||||
# AUTH_USER: admin
|
||||
# AUTH_PASS: # bcrypt hash — generate: php -r "echo password_hash('yourpass', PASSWORD_DEFAULT);"
|
||||
networks:
|
||||
- caddy-network
|
||||
|
||||
|
||||
Reference in new issue
Block a user