commit
Fix the login system
diff --git a/public/static/app.js b/public/static/app.js
index c4c3b30..e967ddb 100644
--- a/public/static/app.js
+++ b/public/static/app.js
@@ -84,10 +84,6 @@ async function postJSON(url, body) {
} catch {
// non-JSON error page
}
- if (res.status === 401 && data.reauth) {
- window.location.href = data.reauth;
- throw new Error("redirecting");
- }
if (!res.ok) throw new Error(data.error || "Something went wrong. Please try again.");
return data;
}
diff --git a/src/env.ts b/src/env.ts
index d68f0cb..2509985 100644
--- a/src/env.ts
+++ b/src/env.ts
@@ -49,7 +49,8 @@ export type AppContext = Context<AppEnv>;
export const ADMIN_GROUP = "auth-admin";
export const TTL = {
- session: 60 * 60 * 24 * 14,
+ /** Signed-in sessions end this long after signing in; there is no re-auth prompt. */
+ session: 60 * 60 * 24,
pendingLogin: 60 * 10,
challenge: 60 * 5,
authCode: 60,
@@ -60,8 +61,6 @@ export const TTL = {
emailVerification: 60 * 60 * 24,
/** A used refresh token is honoured again for this long (racing app instances). */
refreshReuseGrace: 60,
- /** Changing sign-in methods or using /admin needs a sign-in this recent. */
- recentAuth: 60 * 30,
/** Retired signing keys stay in the JWKS this long so issued tokens verify. */
retiredKey: 60 * 60 * 24 * 2,
} as const;
diff --git a/src/lib/guards.tsx b/src/lib/guards.tsx
index cf44355..cbeec0c 100644
--- a/src/lib/guards.tsx
+++ b/src/lib/guards.tsx
@@ -5,7 +5,7 @@
*/
import { createMiddleware } from "hono/factory";
-import { TTL, issuerOrigin, now, type AppContext, type AppEnv } from "../env";
+import { issuerOrigin, type AppContext, type AppEnv } from "../env";
import { timingSafeEqualStr } from "./crypto";
import { form, safeReturn } from "./http";
import { ArrowLeft } from "pixelarticons/react";
@@ -15,10 +15,8 @@ function wantsJson(c: AppContext): boolean {
return (c.req.header("content-type") ?? "").includes("application/json");
}
-function loginUrl(returnTo: string, reauth = false): string {
- const params = new URLSearchParams({ return: returnTo });
- if (reauth) params.set("reauth", "1");
- return `/login?${params}`;
+function loginUrl(returnTo: string): string {
+ return `/login?${new URLSearchParams({ return: returnTo })}`;
}
/** Where to come back to after signing in: the page, or for a POST, the page it came from. */
@@ -81,24 +79,3 @@ export const requireCsrf = createMiddleware<AppEnv>(async (c, next) => {
await next();
});
-/**
- * Changing how an account signs in (or anything in /admin) needs a recent
- * sign-in, so a session lifted from an unlocked machine can't be used to lock
- * the owner out.
- */
-export function recentAuthRedirect(c: AppContext, returnTo: string): Response | null {
- const session = c.get("session");
- if (session && now() - session.authTime <= TTL.recentAuth) return null;
- const url = loginUrl(returnTarget(c, returnTo), true);
- if (wantsJson(c)) return c.json({ error: "Please confirm it's you.", reauth: url }, 401);
- return c.redirect(url);
-}
-
-export const requireRecentAuthForPosts = (returnTo: string) =>
- createMiddleware<AppEnv>(async (c, next) => {
- if (c.req.method === "POST") {
- const redirect = recentAuthRedirect(c, returnTo);
- if (redirect) return redirect;
- }
- await next();
- });
diff --git a/src/routes/account.tsx b/src/routes/account.tsx
index 31d3d22..002ea4d 100644
--- a/src/routes/account.tsx
+++ b/src/routes/account.tsx
@@ -8,11 +8,11 @@ import { Hono } from "hono";
import { Key, Save, Shield } from "pixelarticons/react";
import { renderSVG } from "uqr";
import type { RegistrationResponseJSON } from "@simplewebauthn/server";
-import { TTL, now, siteName, type AppContext, type AppEnv } from "../env";
+import { siteName, type AppContext, type AppEnv } from "../env";
import { audit } from "../lib/audit";
import { decrypt, encrypt } from "../lib/crypto";
import { form, readJson } from "../lib/http";
-import { recentAuthRedirect, requireCsrf, requireUser } from "../lib/guards";
+import { requireCsrf, requireUser } from "../lib/guards";
import { hashPassword, passwordProblem, PASSWORD_MIN } from "../lib/password";
import { generateTotpSecret, otpauthUri, verifyTotp } from "../lib/totp";
import { cleanText, emailProblem, pictureProblem } from "../lib/validate";
@@ -65,7 +65,6 @@ async function accountPage(c: AppContext, opts: { error?: string; status?: 400 }
const session = c.get("session")!;
const [passkeys, sessions] = await Promise.all([listPasskeys(c.env, user.id), listSessions(c.env, user.id)]);
const csrf = session.csrf;
- const stale = now() - session.authTime > TTL.recentAuth;
return render(
c,
@@ -75,11 +74,6 @@ async function accountPage(c: AppContext, opts: { error?: string; status?: 400 }
<Flash code={c.req.query("m")} />
<ErrorNote error={opts.error} />
<p class="notice error hidden" id="passkey-error" role="alert"></p>
- {stale && (
- <p class="notice">
- You signed in {relative(session.authTime)}. Changing how you sign in will ask you to confirm it's you first.
- </p>
- )}
<Section title="Profile" description="Shared with the apps you sign in to.">
<form method="post" action="/account/profile" class="stack">
@@ -278,15 +272,10 @@ account.post("/account/email/verify", async (c) => {
// --- passkeys --------------------------------------------------------------------
account.post("/account/passkeys/options", async (c) => {
- const reauth = recentAuthRedirect(c, "/account");
- if (reauth) return reauth;
return c.json(await registrationOptions(c.env, await me(c)));
});
account.post("/account/passkeys/verify", async (c) => {
- const reauth = recentAuthRedirect(c, "/account");
- if (reauth) return reauth;
-
const body = await readJson<{ id?: string; response?: RegistrationResponseJSON; name?: string }>(c);
if (!body?.id || !body.response) return c.json({ error: "Malformed request." }, 400);
@@ -307,9 +296,6 @@ account.post("/account/passkeys/:id/rename", async (c) => {
});
account.post("/account/passkeys/:id/delete", async (c) => {
- const reauth = recentAuthRedirect(c, "/account");
- if (reauth) return reauth;
-
const user = await me(c);
if (!user.password_hash && (await countPasskeys(c.env, user.id)) <= 1) {
return accountPage(c, {
@@ -325,9 +311,6 @@ account.post("/account/passkeys/:id/delete", async (c) => {
// --- password ------------------------------------------------------------------
account.post("/account/password", async (c) => {
- const reauth = recentAuthRedirect(c, "/account");
- if (reauth) return reauth;
-
const user = await me(c);
const body = await form(c);
const password = body.password ?? "";
@@ -343,9 +326,6 @@ account.post("/account/password", async (c) => {
});
account.post("/account/password/remove", async (c) => {
- const reauth = recentAuthRedirect(c, "/account");
- if (reauth) return reauth;
-
const user = await me(c);
if ((await countPasskeys(c.env, user.id)) === 0) {
return accountPage(c, { error: "Add a passkey before removing your password.", status: 400 });
@@ -358,9 +338,6 @@ account.post("/account/password/remove", async (c) => {
// --- TOTP ----------------------------------------------------------------------
account.post("/account/totp/start", async (c) => {
- const reauth = recentAuthRedirect(c, "/account");
- if (reauth) return reauth;
-
const user = await me(c);
if (!user.password_hash) return c.redirect("/account");
if (user.totp_secret) return c.redirect("/account");
@@ -410,9 +387,6 @@ async function totpSetupPage(c: AppContext, error?: string) {
account.get("/account/totp", (c) => totpSetupPage(c));
account.post("/account/totp/confirm", async (c) => {
- const reauth = recentAuthRedirect(c, "/account");
- if (reauth) return reauth;
-
const user = await me(c);
if (!user.totp_pending) return c.redirect("/account");
@@ -426,9 +400,6 @@ account.post("/account/totp/confirm", async (c) => {
});
account.post("/account/totp/disable", async (c) => {
- const reauth = recentAuthRedirect(c, "/account");
- if (reauth) return reauth;
-
const user = await me(c);
if (!user.totp_secret) return c.redirect("/account");
diff --git a/src/routes/admin.tsx b/src/routes/admin.tsx
index dd3284f..59a6963 100644
--- a/src/routes/admin.tsx
+++ b/src/routes/admin.tsx
@@ -10,7 +10,7 @@ import { AppWindows, Link, Save, UserPlus, Users } from "pixelarticons/react";
import { ADMIN_GROUP, issuer, type AppContext, type AppEnv } from "../env";
import { audit, recentAudit } from "../lib/audit";
import { form, formAll, lines } from "../lib/http";
-import { requireAdmin, requireCsrf, requireRecentAuthForPosts, requireUser } from "../lib/guards";
+import { requireAdmin, requireCsrf, requireUser } from "../lib/guards";
import {
cleanText,
clientIdProblem,
@@ -58,7 +58,7 @@ import { builtinReserved, listReserved, reserveUsername, unreserveUsername } fro
export const admin = new Hono<AppEnv>();
admin.use("/admin", requireUser, requireAdmin);
-admin.use("/admin/*", requireUser, requireAdmin, requireCsrf, requireRecentAuthForPosts("/admin"));
+admin.use("/admin/*", requireUser, requireAdmin, requireCsrf);
admin.get("/admin", (c) => c.redirect("/admin/users"));