Files
lunch/lib/auth/codes.ts
T
Kai BöhmandClaude Opus 5.5 f423dd67ce
ci / olares chart · sanity (pull_request) Successful in 3s
ci / app · types + tests + build (pull_request) Successful in 3m7s
markt / eintrag (pull_request) Successful in 29s
release / which version (pull_request) Successful in 3s
release / olares chart · sanity (pull_request) Successful in 4s
release / app (pull_request) Successful in 59s
release / tag and release (pull_request) Skipped
Own sign-in with passkeys, open the entrance, market entry (26.11.2)
Setup with a code from /app/data, passkeys with required user
verification, sessions as hashed cookies bound to the passkey they
started with, single-use invitation and device links, removing a
passkey or a member, and a recovery code for the owner. Cross-household
lookups before sign-in run in withAuth() (app.auth), allowed only on the
sign-in tables; a test pins that list. State-changing routes check the
session and the Origin header.

Security check with the login-sicherheit skill before the first
invitation: docs/sicherheit/login-2026-11-06.md (no open findings above
low). Entrance goes public now that the own sign-in exists; markt.yml
creates the Lunch entry in aimighty-market and proves it with wrangler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HUc2nv7rJVU2gp3Bgtha3q
2026-11-06 19:26:36 +00:00

90 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import "server-only";
import { randomInt } from "node:crypto";
import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
import path from "node:path";
import { safeEqual } from "./tokens";
// Die Erstinstallation: Wer Lunch zum ersten Mal öffnet, braucht einen Code,
// der nur in den Dateien der Box steht (Data › lunch › lunch-einrichten.txt).
// So kann niemand aus dem Internet die leere App als Erster in Besitz nehmen,
// sobald der Entrance öffentlich ist — Muster Rocket app/einrichtung.py.
const ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // ohne 0/O, 1/I
export const SETUP_FILE = "lunch-einrichten.txt";
function dataDir(): string {
return process.env.APP_DATA_DIR ?? path.join(process.cwd(), ".data");
}
/** Großbuchstaben, ohne Leer- und Bindestriche — so, wie man ihn abtippt. */
export function normalizeCode(input: string): string {
return input.toUpperCase().replace(/[\s\-–]/g, "");
}
function format(raw: string): string {
return raw.match(/.{4}/g)!.join("-");
}
function newCode(): string {
let raw = "";
for (let i = 0; i < 16; i++) raw += ALPHABET[randomInt(ALPHABET.length)];
return format(raw);
}
async function write(file: string, text: string, flag: "wx" | "w"): Promise<void> {
await mkdir(dataDir(), { recursive: true });
await writeFile(path.join(dataDir(), file), text, { mode: 0o600, flag }).catch(() => undefined);
}
async function readCode(file: string, maxAgeMs?: number): Promise<string | null> {
try {
const full = path.join(dataDir(), file);
if (maxAgeMs !== undefined && Date.now() - (await stat(full)).mtimeMs > maxAgeMs) return null;
const text = await readFile(full, "utf8");
const line = text.split("\n").find((l) => /^[A-Z2-9]{4}(-[A-Z2-9]{4}){3}$/.test(l.trim()));
return line ? line.trim() : null;
} catch {
return null;
}
}
async function matches(file: string, input: string, maxAgeMs?: number): Promise<boolean> {
const code = await readCode(file, maxAgeMs);
if (!code || !input) return false;
return safeEqual(normalizeCode(code), normalizeCode(input));
}
/** Legt den Einrichtungscode an, falls er fehlt (16 Zeichen, rund 80 Bit). */
export async function ensureSetupCode(): Promise<void> {
await write(
SETUP_FILE,
`Einrichtungscode für Was gibt's? (lunch)\n\n${newCode()}\n\n` +
"Diesen Code beim ersten Öffnen der App eingeben. Die Datei verschwindet,\n" +
"sobald der Haushalt eingerichtet ist.\n",
"wx",
);
}
export const setupCodeMatches = (input: string) => matches(SETUP_FILE, input);
export const removeSetupCode = () => rm(path.join(dataDir(), SETUP_FILE), { force: true });
// Rückweg, wenn die Person, die eingerichtet hat, keinen Passkey mehr hat
// (Handy verloren). Den Code kann jede anfordern; lesen kann ihn nur, wer an
// die Dateien der Box kommt — dieselbe Wand wie bei der Einrichtung. Er gilt
// eine Stunde, ein neuer ersetzt den alten.
export const RECOVERY_FILE = "lunch-wiederherstellen.txt";
const RECOVERY_MS = 60 * 60 * 1000;
export async function writeRecoveryCode(): Promise<void> {
await write(
RECOVERY_FILE,
`Wiederherstellungscode für Was gibt's? (lunch)\n\n${newCode()}\n\n` +
"Gilt eine Stunde. Damit bekommt die Person, die den Haushalt eingerichtet\n" +
"hat, einen neuen Passkey. Wer diese Datei nicht angefordert hat, kann sie löschen.\n",
"w",
);
}
export const recoveryCodeMatches = (input: string) => matches(RECOVERY_FILE, input, RECOVERY_MS);
export const removeRecoveryCode = () => rm(path.join(dataDir(), RECOVERY_FILE), { force: true });