diff --git a/web/serve.mjs b/web/serve.mjs index 55240532..a53c70cb 100644 --- a/web/serve.mjs +++ b/web/serve.mjs @@ -37,7 +37,6 @@ const AUTH_DISABLED = process.env.PIPELINES_WEB_AUTH === "disabled"; const AUTH_FILE = process.env.PIPELINES_WEB_AUTH_FILE ?? join(homedir(), ".config", "pipelines", "web-auth.json"); const SESSION_COOKIE = "pipelines_session"; -const SESSION_TTL_SECONDS = 30 * 24 * 60 * 60; function loadAuth() { if (AUTH_DISABLED) return null; @@ -80,8 +79,12 @@ function signSession(expires) { return `${expires}.${createHmac("sha256", auth.secret).update(String(expires)).digest("base64url")}`; } -function sessionToken() { - return signSession(Math.floor(Date.now() / 1000) + SESSION_TTL_SECONDS); +const SESSION_LONG_SECONDS = 30 * 24 * 60 * 60; +const SESSION_SHORT_SECONDS = 12 * 60 * 60; + +function sessionToken(remember = false) { + const ttl = remember ? SESSION_LONG_SECONDS : SESSION_SHORT_SECONDS; + return signSession(Math.floor(Date.now() / 1000) + ttl); } function validSession(value) { @@ -110,14 +113,11 @@ function passwordOk(candidate) { return given.length === expected.length && timingSafeEqual(given, expected); } -function sessionCookie(req) { - const bits = [ - `${SESSION_COOKIE}=${sessionToken()}`, - "Path=/", - "HttpOnly", - "SameSite=Strict", - `Max-Age=${SESSION_TTL_SECONDS}`, - ]; +function sessionCookie(req, remember = false) { + const bits = [`${SESSION_COOKIE}=${sessionToken(remember)}`, "Path=/", "HttpOnly", "SameSite=Strict"]; + // Remember me persists the cookie; otherwise it is a browser-session cookie + // (and the signed token itself expires after 12 hours). + if (remember) bits.push(`Max-Age=${SESSION_LONG_SECONDS}`); // Tailscale terminates TLS and usually forwards the scheme; when it does, the // cookie is Secure. A direct plain-http LAN visit still works without it. const forwarded = String(req.headers["x-forwarded-proto"] ?? "").split(",")[0].trim(); @@ -174,6 +174,9 @@ function loginPage({ next = "/", error = "" } = {}) { input { width: 100%; padding: 11px 12px; border-radius: 9px; border: 1px solid #33333c; background: #101013; color: #e8e8ea; font: inherit; } input:focus { outline: 2px solid #8fa8c8; outline-offset: 1px; } + .remember { display: flex; align-items: center; gap: 8px; margin-top: 12px; + color: #b8b8c0; font-size: 12px; cursor: pointer; } + .remember input { width: auto; margin: 0; accent-color: #8fa8c8; } button { width: 100%; margin-top: 14px; padding: 11px 12px; border: 0; border-radius: 9px; background: #8fa8c8; color: #101013; font: inherit; font-weight: 650; cursor: pointer; } .error { margin: 10px 0 0; color: #e08a8a; font-size: 13px; } @@ -189,10 +192,11 @@ function loginPage({ next = "/", error = "" } = {}) { + ${error ? `
${error}
` : ""} -The session lasts 30 days on this device.
+Remembered sessions last 30 days; otherwise the sign-in ends when the browser closes.