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:
+18
-13
@@ -37,7 +37,6 @@ const AUTH_DISABLED = process.env.PIPELINES_WEB_AUTH === "disabled";
|
|||||||
const AUTH_FILE =
|
const AUTH_FILE =
|
||||||
process.env.PIPELINES_WEB_AUTH_FILE ?? join(homedir(), ".config", "pipelines", "web-auth.json");
|
process.env.PIPELINES_WEB_AUTH_FILE ?? join(homedir(), ".config", "pipelines", "web-auth.json");
|
||||||
const SESSION_COOKIE = "pipelines_session";
|
const SESSION_COOKIE = "pipelines_session";
|
||||||
const SESSION_TTL_SECONDS = 30 * 24 * 60 * 60;
|
|
||||||
|
|
||||||
function loadAuth() {
|
function loadAuth() {
|
||||||
if (AUTH_DISABLED) return null;
|
if (AUTH_DISABLED) return null;
|
||||||
@@ -80,8 +79,12 @@ function signSession(expires) {
|
|||||||
return `${expires}.${createHmac("sha256", auth.secret).update(String(expires)).digest("base64url")}`;
|
return `${expires}.${createHmac("sha256", auth.secret).update(String(expires)).digest("base64url")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sessionToken() {
|
const SESSION_LONG_SECONDS = 30 * 24 * 60 * 60;
|
||||||
return signSession(Math.floor(Date.now() / 1000) + SESSION_TTL_SECONDS);
|
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) {
|
function validSession(value) {
|
||||||
@@ -110,14 +113,11 @@ function passwordOk(candidate) {
|
|||||||
return given.length === expected.length && timingSafeEqual(given, expected);
|
return given.length === expected.length && timingSafeEqual(given, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sessionCookie(req) {
|
function sessionCookie(req, remember = false) {
|
||||||
const bits = [
|
const bits = [`${SESSION_COOKIE}=${sessionToken(remember)}`, "Path=/", "HttpOnly", "SameSite=Strict"];
|
||||||
`${SESSION_COOKIE}=${sessionToken()}`,
|
// Remember me persists the cookie; otherwise it is a browser-session cookie
|
||||||
"Path=/",
|
// (and the signed token itself expires after 12 hours).
|
||||||
"HttpOnly",
|
if (remember) bits.push(`Max-Age=${SESSION_LONG_SECONDS}`);
|
||||||
"SameSite=Strict",
|
|
||||||
`Max-Age=${SESSION_TTL_SECONDS}`,
|
|
||||||
];
|
|
||||||
// Tailscale terminates TLS and usually forwards the scheme; when it does, the
|
// 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.
|
// cookie is Secure. A direct plain-http LAN visit still works without it.
|
||||||
const forwarded = String(req.headers["x-forwarded-proto"] ?? "").split(",")[0].trim();
|
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;
|
input { width: 100%; padding: 11px 12px; border-radius: 9px; border: 1px solid #33333c;
|
||||||
background: #101013; color: #e8e8ea; font: inherit; }
|
background: #101013; color: #e8e8ea; font: inherit; }
|
||||||
input:focus { outline: 2px solid #8fa8c8; outline-offset: 1px; }
|
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;
|
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; }
|
background: #8fa8c8; color: #101013; font: inherit; font-weight: 650; cursor: pointer; }
|
||||||
.error { margin: 10px 0 0; color: #e08a8a; font-size: 13px; }
|
.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, """)}" />
|
<input type="hidden" name="next" value="${next.replace(/"/g, """)}" />
|
||||||
<label for="password">Password</label>
|
<label for="password">Password</label>
|
||||||
<input id="password" name="password" type="password" autocomplete="current-password" autofocus required />
|
<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>` : ""}
|
${error ? `<p class="error">${error}</p>` : ""}
|
||||||
<button type="submit">Sign in</button>
|
<button type="submit">Sign in</button>
|
||||||
</form>
|
</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>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
@@ -350,7 +354,8 @@ const server = createServer(async (req, res) => {
|
|||||||
.end(loginPage({ next: safeNext(fields.get("next")), error: "Wrong password." }));
|
.end(loginPage({ next: safeNext(fields.get("next")), error: "Wrong password." }));
|
||||||
return;
|
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();
|
res.writeHead(303, { Location: safeNext(fields.get("next")) }).end();
|
||||||
return;
|
return;
|
||||||
} else if (pathname === "/logout") {
|
} else if (pathname === "/logout") {
|
||||||
|
|||||||
Reference in New Issue
Block a user