web: remember-me on the sign-in page

Checked (the default) issues a 30-day persistent cookie; unchecked keeps a
browser-session cookie and a 12-hour token. The sign-in card also links the
public favicon/apple icon and uses the app mark.
This commit is contained in:
2026-10-20 02:22:36 +01:00
parent ebf02c3bed
commit caae8a8038
+18 -13
View File
@@ -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 = "" } = {}) {
<input type="hidden" name="next" value="${next.replace(/"/g, "&quot;")}" />
<label for="password">Password</label>
<input id="password" name="password" type="password" autocomplete="current-password" autofocus required />
<label class="remember"><input type="checkbox" name="remember" checked /> Remember me on this device</label>
${error ? `<p class="error">${error}</p>` : ""}
<button type="submit">Sign in</button>
</form>
<p class="hint">The session lasts 30 days on this device.</p>
<p class="hint">Remembered sessions last 30 days; otherwise the sign-in ends when the browser closes.</p>
</main>
</body>
</html>`;
@@ -350,7 +354,8 @@ const server = createServer(async (req, res) => {
.end(loginPage({ next: safeNext(fields.get("next")), error: "Wrong password." }));
return;
}
res.setHeader("Set-Cookie", sessionCookie(req));
const remember = fields.get("remember") === "on";
res.setHeader("Set-Cookie", sessionCookie(req, remember));
res.writeHead(303, { Location: safeNext(fields.get("next")) }).end();
return;
} else if (pathname === "/logout") {