Files
kai d1bf5196dd
release / which version (push) Successful in 1s
release / olares chart · sanity (push) Successful in 3s
release / app (push) Successful in 1m7s
release / tag and release (push) Successful in 2s
Own sign-in with passkeys, public entrance, market (26.11.2) (#3)
2026-11-06 19:32:34 +00:00

90 lines
3.5 KiB
TypeScript
Raw Permalink 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 });