diff --git a/app/index.php b/app/index.php index 278a11a..8ca76ce 100644 --- a/app/index.php +++ b/app/index.php @@ -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)) { - $_SESSION['koncad_auth'] = true; - header('Location: /'); exit; + 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; + } + $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'; } ?> @@ -209,13 +240,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { -KonCad — Login +KonCad — <?= $first_run ? 'Setup' : 'Login' ?>