add session auth + always read from import file on load
Publish Docker image / build (push) Has been cancelled
Publish Docker image / build (push) Has been cancelled
This commit is contained in:
1 parent
14fc275da7
commit
e54fec540b
2 files changed
+93
-4
No files matched your search
+91
-4
@@ -3,6 +3,13 @@
|
|||||||
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_PASS', getenv('AUTH_PASS') ?: ''); // bcrypt hash
|
||||||
|
|
||||||
|
// ─── Session / Auth ──────────────────────────────────────────────────────────
|
||||||
|
session_start();
|
||||||
|
$auth_enabled = AUTH_USER !== '' && AUTH_PASS !== '';
|
||||||
|
$logged_in = !$auth_enabled || !empty($_SESSION['koncad_auth']);
|
||||||
|
|
||||||
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||||
function dedent(array $lines): array {
|
function dedent(array $lines): array {
|
||||||
@@ -105,6 +112,31 @@ if (isset($_GET['action'])) {
|
|||||||
ini_set('display_errors', 0);
|
ini_set('display_errors', 0);
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
switch ($_GET['action']) {
|
||||||
|
|
||||||
|
case 'login':
|
||||||
|
$user = trim($_POST['user'] ?? '');
|
||||||
|
$pass = $_POST['pass'] ?? '';
|
||||||
|
if ($auth_enabled && $user === AUTH_USER && password_verify($pass, AUTH_PASS)) {
|
||||||
|
$_SESSION['koncad_auth'] = true;
|
||||||
|
echo json_encode(['ok' => true]);
|
||||||
|
} else {
|
||||||
|
echo json_encode(['ok' => false, 'error' => 'Invalid credentials']);
|
||||||
|
}
|
||||||
|
exit;
|
||||||
|
|
||||||
|
case 'logout':
|
||||||
|
session_destroy();
|
||||||
|
echo json_encode(['ok' => true]);
|
||||||
|
exit;
|
||||||
|
|
||||||
|
default:
|
||||||
|
if ($auth_enabled && !$logged_in) {
|
||||||
|
echo json_encode(['ok' => false, 'error' => 'Unauthorized']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch ($_GET['action']) {
|
switch ($_GET['action']) {
|
||||||
|
|
||||||
case 'check_admin':
|
case 'check_admin':
|
||||||
@@ -117,11 +149,10 @@ if (isset($_GET['action'])) {
|
|||||||
exit;
|
exit;
|
||||||
|
|
||||||
case 'load':
|
case 'load':
|
||||||
if (file_exists(CADDYFILE_STORE)) {
|
if (CADDYFILE_IMPORT && file_exists(CADDYFILE_IMPORT)) {
|
||||||
$raw = file_get_contents(CADDYFILE_STORE);
|
|
||||||
} elseif (CADDYFILE_IMPORT && file_exists(CADDYFILE_IMPORT)) {
|
|
||||||
$raw = file_get_contents(CADDYFILE_IMPORT);
|
$raw = file_get_contents(CADDYFILE_IMPORT);
|
||||||
file_put_contents(CADDYFILE_STORE, $raw);
|
} elseif (file_exists(CADDYFILE_STORE)) {
|
||||||
|
$raw = file_get_contents(CADDYFILE_STORE);
|
||||||
} else {
|
} else {
|
||||||
$raw = '';
|
$raw = '';
|
||||||
}
|
}
|
||||||
@@ -159,12 +190,60 @@ if (isset($_GET['action'])) {
|
|||||||
echo json_encode(['ok' => false, 'error' => 'Unknown action']);
|
echo json_encode(['ok' => false, 'error' => 'Unknown action']);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Login page ───────────────────────────────────────────────────────────────
|
||||||
|
if ($auth_enabled && !$logged_in):
|
||||||
|
$login_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;
|
||||||
|
}
|
||||||
|
$login_error = 'Invalid credentials';
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<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>
|
||||||
|
<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; }
|
||||||
|
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); }
|
||||||
|
button { width:100%; padding:0.55rem; border-radius:6px; border:none; background:var(--accent); color:#0f1117; font-size:0.9rem; font-weight:700; cursor:pointer; }
|
||||||
|
button:hover { background:#9ba8ff; }
|
||||||
|
.error { color:var(--error); font-size:0.82rem; margin-bottom:1rem; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="card">
|
||||||
|
<h1>KonCad</h1>
|
||||||
|
<?php if ($login_error): ?><div class="error"><?= htmlspecialchars($login_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>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
<?php exit; endif; ?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>KonCad</title>
|
<title>KonCad</title>
|
||||||
<style>
|
<style>
|
||||||
:root {
|
:root {
|
||||||
@@ -442,6 +521,9 @@ textarea.code:focus { border-color: var(--accent); }
|
|||||||
<h1>KonCad</h1>
|
<h1>KonCad</h1>
|
||||||
<span id="status">Loading…</span>
|
<span id="status">Loading…</span>
|
||||||
<button class="btn btn-accent" id="deploy-btn" onclick="preDeploy()">Deploy</button>
|
<button class="btn btn-accent" id="deploy-btn" onclick="preDeploy()">Deploy</button>
|
||||||
|
<?php if ($auth_enabled): ?>
|
||||||
|
<button class="btn" onclick="logout()">Log out</button>
|
||||||
|
<?php endif; ?>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
@@ -777,6 +859,11 @@ function esc(s) {
|
|||||||
.replace(/>/g, '>').replace(/"/g, '"');
|
.replace(/>/g, '>').replace(/"/g, '"');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function logout() {
|
||||||
|
await fetch('?action=logout', { method: 'POST' });
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
|
||||||
function handleTab(e) {
|
function handleTab(e) {
|
||||||
if (e.key !== 'Tab') return;
|
if (e.key !== 'Tab') return;
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ 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