feat(webui): rememberMe login flag with 30-day persistent cookie
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, describe, expect, it, vi } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
vi.mock("server-only", () => ({}));
|
||||
|
||||
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "evosci-login-"));
|
||||
const ORIGINAL_ENV = {
|
||||
EVOSCIENTIST_DATA_DIR: process.env.EVOSCIENTIST_DATA_DIR,
|
||||
WEBUI_AUTH_ENABLED: process.env.WEBUI_AUTH_ENABLED,
|
||||
WEBUI_AUTH_SECRET: process.env.WEBUI_AUTH_SECRET,
|
||||
WEBUI_AUTH_USERNAME: process.env.WEBUI_AUTH_USERNAME,
|
||||
WEBUI_AUTH_PASSWORD: process.env.WEBUI_AUTH_PASSWORD,
|
||||
WEBUI_AUTH_REMEMBER_TTL_DAYS: process.env.WEBUI_AUTH_REMEMBER_TTL_DAYS,
|
||||
WEBUI_AUTH_SESSION_TTL_HOURS: process.env.WEBUI_AUTH_SESSION_TTL_HOURS,
|
||||
};
|
||||
|
||||
process.env.EVOSCIENTIST_DATA_DIR = dataDir;
|
||||
process.env.WEBUI_AUTH_ENABLED = "true";
|
||||
process.env.WEBUI_AUTH_SECRET = "test-secret-with-at-least-32-characters";
|
||||
process.env.WEBUI_AUTH_USERNAME = "admin";
|
||||
process.env.WEBUI_AUTH_PASSWORD = "bootstrap-password";
|
||||
delete process.env.WEBUI_AUTH_REMEMBER_TTL_DAYS;
|
||||
delete process.env.WEBUI_AUTH_SESSION_TTL_HOURS;
|
||||
|
||||
const { POST } = await import("./route");
|
||||
const { decodeSessionPayload } = await import("@/lib/server/auth");
|
||||
const { closeUserStoreForTests } = await import("@/lib/server/userStore");
|
||||
|
||||
const REMEMBER_TTL = 30 * 24 * 60 * 60;
|
||||
const SESSION_TTL = 12 * 60 * 60;
|
||||
|
||||
afterAll(() => {
|
||||
closeUserStoreForTests();
|
||||
for (const [key, value] of Object.entries(ORIGINAL_ENV)) {
|
||||
if (value === undefined) delete process.env[key];
|
||||
else process.env[key] = value;
|
||||
}
|
||||
fs.rmSync(dataDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function loginRequest(body: unknown): NextRequest {
|
||||
return new NextRequest("http://localhost/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
async function loginWith(body: unknown) {
|
||||
const res = await POST(
|
||||
loginRequest({ username: "admin", password: "bootstrap-password", ...(body as object) })
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
const setCookie = res.headers.get("set-cookie") ?? "";
|
||||
const token = setCookie.split(";")[0].split("=").slice(1).join("=");
|
||||
const payload = decodeSessionPayload(token);
|
||||
expect(payload).not.toBeNull();
|
||||
return { setCookie, expiresIn: payload!.expiresAt - Math.floor(Date.now() / 1000) };
|
||||
}
|
||||
|
||||
describe("POST /api/auth/login rememberMe", () => {
|
||||
it("issues a 30-day persistent cookie when rememberMe is true", async () => {
|
||||
const { setCookie, expiresIn } = await loginWith({ rememberMe: true });
|
||||
expect(setCookie).toContain(`Max-Age=${REMEMBER_TTL}`);
|
||||
expect(expiresIn).toBeGreaterThan(REMEMBER_TTL - 10);
|
||||
expect(expiresIn).toBeLessThanOrEqual(REMEMBER_TTL);
|
||||
});
|
||||
|
||||
it("issues a session cookie with a 12h server-side expiry when rememberMe is omitted", async () => {
|
||||
const { setCookie, expiresIn } = await loginWith({});
|
||||
expect(setCookie).not.toContain("Max-Age");
|
||||
expect(expiresIn).toBeGreaterThan(SESSION_TTL - 10);
|
||||
expect(expiresIn).toBeLessThanOrEqual(SESSION_TTL);
|
||||
});
|
||||
|
||||
it("treats non-boolean rememberMe as not remembered", async () => {
|
||||
const { setCookie } = await loginWith({ rememberMe: "yes" });
|
||||
expect(setCookie).not.toContain("Max-Age");
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,8 @@ import { type NextRequest, NextResponse } from "next/server";
|
||||
import {
|
||||
isAuthenticationEnabled,
|
||||
isSafeReturnPath,
|
||||
rememberTtlSeconds,
|
||||
sessionTtlSeconds,
|
||||
} from "@/lib/auth";
|
||||
import {
|
||||
AuthConfigurationError,
|
||||
@@ -34,6 +36,7 @@ export async function POST(request: NextRequest) {
|
||||
username?: unknown;
|
||||
password?: unknown;
|
||||
next?: unknown;
|
||||
rememberMe?: unknown;
|
||||
} | null;
|
||||
const username =
|
||||
typeof body?.username === "string" ? body.username.trim() : "";
|
||||
@@ -58,7 +61,9 @@ export async function POST(request: NextRequest) {
|
||||
{ status: 401, headers: NO_STORE }
|
||||
);
|
||||
}
|
||||
const { token, payload } = createSession(user.username, user.role);
|
||||
const rememberMe = body?.rememberMe === true;
|
||||
const ttlSeconds = rememberMe ? rememberTtlSeconds() : sessionTtlSeconds();
|
||||
const { token, payload } = createSession(user.username, user.role, ttlSeconds);
|
||||
await recordLogin(payload);
|
||||
const response = NextResponse.json(
|
||||
{
|
||||
@@ -66,7 +71,10 @@ export async function POST(request: NextRequest) {
|
||||
},
|
||||
{ headers: NO_STORE }
|
||||
);
|
||||
response.cookies.set({ ...authCookieOptions(), value: token });
|
||||
response.cookies.set({
|
||||
...authCookieOptions(rememberMe ? ttlSeconds : null),
|
||||
value: token,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
const message =
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
AUTH_COOKIE_NAME,
|
||||
authSecret,
|
||||
isAuthenticationEnabled,
|
||||
sessionTtlSeconds,
|
||||
} from "@/lib/auth";
|
||||
import { webuiDataDir } from "./dataDir";
|
||||
import {
|
||||
|
||||
Reference in New Issue
Block a user