The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
/**
* Where sign-in data lives: tables in the studio's SQLite database (AUTH_TABLES in src/server/db.ts): the install's
* cookie secret and Settings password, API tokens, accounts with their GPU links and sealed keys, and signed-out
* cookies. src/server/auth.ts reads and writes them; this file decides whether they can be used, and moves an older
* install's data/auth.json in.
*
* Moving auth.json in, once, on the first start of this build:
* - Everything goes in inside one transaction that first takes the database's write lock. Inside it the rows are
* counted and every account, token and setting is read back and compared with the file. The marker row
* (auth_migration) is written in the same transaction, so a crash leaves either nothing (the next start moves it again)
* or everything. A second server starting at the same time waits for the lock, then finds the marker.
* - A file that can't be read or parsed keeps today's safety: sign-in stays on, nobody is signed in, nothing is written.
* - A file whose data can't go in (two accounts with one email…) changes nothing either; sign-in fails closed and the
* proxy answers 503 (a calm "back in a moment" page), so a new release that can't start cleanly is never switched to.
*
* A server of an older build may still be running on the same data folder for a while (a deploy that starts the new
* server before it stops the old one). Taking auth.json away at once would leave that server with no password at all.
* So for 10 minutes ("bridge") the database leads and auth.json is kept as an exact copy of it: each change here is
* also written to the file, and a change the older server makes to the file is read back in. Once the file has been
* quiet for 3 minutes after that, it is put aside as auth.json.migrated-<UTC time> (kept, never deleted) and a note
* takes its place (TOMBSTONE): JSON that every older build reads as "sign-in on, nobody can sign in", so an older build
* pointed at this folder later never opens the studio. A new install gets the note with its first sign-in change.
*
* Going back to an older build: scripts/auth-export.mjs writes auth.json from the database again and marks the store
* "exported". The next start of this build moves that file back in (with what the older build changed in it) and
* bridges again.
*
* Any other auth.json found once the store was moved (a backup copied back, a file from elsewhere, a changed copy of the
* note, a link) is never read as sign-in data: the database stays the store and the site keeps serving. The file is set
* aside unread as auth.json.stray-<UTC time> (readable by this user only, never deleted here), the note goes back in its
* place, the log says so, and the owner sees a notice in Settings → Accounts and the admin API (its counts and its name
* only). Nothing in it signs anyone in, opens the studio, changes the password or the cookie secret. The accounts it lists
* that the database doesn't have keep their files while it is there (the account sweep skips them), so the owner can
* still choose it: a start with FUOBE_AUTH_REIMPORT=1 moves an auth.json in, as it is, over the database's sign-in data
* (on that start's first look only).
*/
import "server-only";
import type Database from "better-sqlite3";
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import type { Account, AccountGpu, ApiToken, StoredKey } from "./auth";
import { DATA_DIR, db } from "./db";
export const AUTH_FILE = path.join(DATA_DIR, "auth.json");
/** The file an older build keeps sign-in data in: what is moved in, and what is kept in step during the bridge. */
export interface AuthJson {
password?: string;
secret: string;
tokens: ApiToken[];
accounts?: Account[];
publicSignup?: boolean;
signedOut?: { hash: string; exp: number }[];
[extra: string]: unknown;
}
/**
* - fresh: nothing yet (a new install): no password, no accounts; the first change sets the store up.
* - bridge / done: the database is the store (see above).
* - unreadable: an auth.json that can't be read or parsed is waiting to be moved in.
* - failed: the database or the move failed. unreadable and failed both fail closed.
*/
export type AuthStoreMode = "fresh" | "bridge" | "done" | "unreadable" | "failed";
interface Marker {
source: "auth.json" | "new";
state: "bridge" | "done" | "exported";
started_at: number;
synced_at: number | null;
json_stat: string | null;
json_sha256: string | null;
counts_json: string | null;
backup_file: string | null;
retired_at: number | null;
/** exported: auth_install.version when scripts/auth-export.mjs wrote auth.json. */
exported_version: number | null;
}
interface Counts {
accounts: number;
tokens: number;
keys: number;
gpus: number;
signedOut: number;
}
/** auth.json's data can't be moved in as it is (never says which account: no email or secret goes in a log). */
class DataError extends Error {}
const envMs = (name: string, fallback: number) => {
const v = Number(process.env[name]);
return process.env[name]?.trim() && Number.isFinite(v) && v >= 0 ? v : fallback;
};
/** How long auth.json is kept in step after it was moved in (FUOBE_AUTH_BRIDGE_MS, for tests). */
const bridgeMs = () => envMs("FUOBE_AUTH_BRIDGE_MS", 10 * 60_000);
/** How long auth.json must have gone unchanged by anyone else before it is put aside. */
const quietMs = () => Math.min(3 * 60_000, bridgeMs());
declare global {
/** Tests: the store's clock. */
var __fuobeAuthClock: (() => number) | undefined;
/** Tests: called while moving auth.json in, after half the accounts (a crash in the middle). */
var __fuobeAuthImportFault: (() => void) | undefined;
var __fuobeAuthStoreTimer: NodeJS.Timeout | undefined;
/** FUOBE_AUTH_REIMPORT=1 was looked at by this process (it counts on the first look only). */
var __fuobeAuthReimportTaken: boolean | undefined;
}
const now = () => globalThis.__fuobeAuthClock?.() ?? Date.now();
/** How often a server whose store was moved looks at auth.json again (for a file put back, or an unfinished rename). */
const FILE_CHECK_MS = 60_000;
const s = {
/** The store was moved (done or exported) and auth.json was looked at, at fileCheckedAt. */
done: false,
fileCheckedAt: 0,
/** What that look found that keeps sign-in closed (an export that can't be read, or can't go back in). */
held: null as { mode: "unreadable" | "failed"; message: string } | null,
/** The auth.json (by its stat) last found unreadable, or whose data couldn't go in, and when. */
bad: null as { key: string; mode: "unreadable" | "failed"; at: number; data: boolean } | null,
/** The database couldn't be used: try again after a moment. */
dbFailedAt: 0,
/** In the bridge: an auth.json (by its stat) that couldn't be read back in. */
bridgeBad: null as string | null,
retireCheckedAt: 0,
warned: { unreadable: 0, failed: 0, stray: 0 },
writes: 0,
};
const HOUR = 60 * 60_000;
function warnOnce(kind: keyof typeof s.warned, message: string, error = false): void {
if (Date.now() - s.warned[kind] < HOUR) return;
s.warned[kind] = Date.now();
if (error) console.error(`[fuobe] ${message}`);
else console.warn(`[fuobe] ${message}`);
}
// ---------------------------------------------------------------------------
// Prepared statements
const statements = new WeakMap<Database.Database, Map<string, Database.Statement>>();
/** A prepared statement on the database, made once. */
export function sql(text: string): Database.Statement {
const d = db();
let m = statements.get(d);
if (!m) statements.set(d, (m = new Map()));
let st = m.get(text);
if (!st) m.set(text, (st = d.prepare(text)));
return st;
}
const marker = () => sql("SELECT * FROM auth_migration WHERE id = 1").get() as Marker | undefined;
const hasRows = () =>
!!(sql("SELECT EXISTS(SELECT 1 FROM auth_install) OR EXISTS(SELECT 1 FROM auth_accounts) OR EXISTS(SELECT 1 FROM auth_tokens) OR EXISTS(SELECT 1 FROM auth_signed_out) AS n").get() as { n: number }).n;
/** A transaction that takes the write lock first. Another server holding it longer than the busy timeout: try again. */
function immediate<T>(fn: () => T): T {
const d = db();
for (let i = 0; ; i++) {
try {
return d.transaction(fn).immediate();
} catch (e) {
if ((e as { code?: string }).code === "SQLITE_BUSY" && i < 5 && !d.inTransaction) continue;
throw e;
}
}
}
// ---------------------------------------------------------------------------
// auth.json on disk
/** Its stat as "mtime:size:inode", null when there is none, "error:<code>" when it can't even be looked at. */
function statKey(): string | null {
try {
const st = fs.statSync(AUTH_FILE);
return `${st.mtimeMs}:${st.size}:${st.ino}`;
} catch (e) {
const code = (e as NodeJS.ErrnoException).code;
return code === "ENOENT" ? null : `error:${code}`;
}
}
type FileRead = { data: AuthJson; key: string | null; sha: string } | { error: string };
/** The largest auth.json read as sign-in data (100,000 accounts are about 30 MB). */
const AUTH_READ_MAX = 128 * 1024 * 1024;
const NOFOLLOW = fs.constants.O_NOFOLLOW ?? 0;
/** A named pipe opens at once and reads as empty, instead of waiting for a writer. */
const NONBLOCK = fs.constants.O_NONBLOCK ?? 0;
/**
* A plain file's bytes and stat key, opened without waiting: a named pipe, a device or a folder is never read, nor a file
* over `max`. `follow`: through a symbolic link (an older install may keep auth.json behind one); otherwise a link isn't
* opened at all.
*/
function readBytes(file: string, follow: boolean, max: number): { raw: Buffer; key: string } | { error: string } {
let fd: number | undefined;
try {
if (!follow && !fs.lstatSync(file).isFile()) return { error: "not a plain file" };
fd = fs.openSync(file, fs.constants.O_RDONLY | NONBLOCK | (follow ? 0 : NOFOLLOW));
const st = fs.fstatSync(fd);
if (!st.isFile()) return { error: "not a plain file" };
if (st.size > max) return { error: "too big" };
return { raw: fs.readFileSync(fd), key: `${st.mtimeMs}:${st.size}:${st.ino}` };
} catch (e) {
return { error: (e as NodeJS.ErrnoException).code ?? "can't be read" };
} finally {
if (fd !== undefined) fs.closeSync(fd);
}
}
/** auth.json as sign-in data. Once the store was moved (`follow` false) a link in its place is never followed. */
function readFile(follow = true): FileRead {
const got = readBytes(AUTH_FILE, follow, AUTH_READ_MAX);
if ("error" in got) return got;
const parsed = parse(got.raw);
if ("error" in parsed) return parsed;
return { data: parsed.data, key: got.key, sha: crypto.createHash("sha256").update(got.raw).digest("hex") };
}
/** A file's text as a JSON object, or why it isn't (never the parser's message: it quotes the file). */
function parseObject(raw: Buffer): { data: AuthJson } | { error: string } {
let data: AuthJson | null;
try {
data = JSON.parse(raw.toString("utf8")) as AuthJson | null;
} catch {
return { error: raw.toString("utf8").startsWith(TEXT_TOMBSTONE_HEAD) ? "tombstone" : "not valid JSON" };
}
if (!data || typeof data !== "object" || Array.isArray(data)) return { error: "not an object" };
return { data };
}
/** What parse calls a changed copy of the note: the note's key with no accounts or tokens, but not the note itself. */
const LIKE_NOTE = "like the note";
/** A file's text as sign-in data, or why it isn't: "tombstone" for the note, LIKE_NOTE for a changed copy of it. */
function parse(raw: Buffer): { data: AuthJson } | { error: string } {
const parsed = parseObject(raw);
if ("error" in parsed) return parsed;
const kind = noteKind(parsed.data);
if (kind === "note") return { error: "tombstone" };
if (kind === "like") return { error: LIKE_NOTE };
return parsed;
}
/**
* The note left in place of auth.json once it was moved: JSON that every older build reads as "sign-in on, nobody can
* sign in". Builds from 2026-10-31 on read `unreadable: true` as a file they can't read: they fail closed and never
* write over it. Older ones see a password whose hash (1 byte) no scrypt result (32 bytes) can match, no secret (no
* cookie is valid) and no tokens. Each of them, and the account sweep, finds no accounts, and a missing secret makes
* the sweep stop instead of removing anyone's files.
*/
const TOMBSTONE_KEY = "fuobeSignInMovedTo";
const TOMBSTONE = `${JSON.stringify(
{
[TOMBSTONE_KEY]: "fuobe.db",
note: 'Sign-in data (the password, accounts and API tokens) is in the database, fuobe.db, in this folder. This file signs nobody in. To go back to an older Fuobe build, see "Sign-in data" in docs/SELF-HOSTING.md.',
password: "00:00",
secret: "",
tokens: [],
unreadable: true,
},
null,
2,
)}\n`;
/** The first form of the note (plain text), from test builds of this change. */
const TEXT_TOMBSTONE_HEAD = "This is not Fuobe's sign-in file any more.";
/**
* "note": the note as this build writes it, whatever else it holds: the password nobody can match, no secret, no tokens,
* no accounts, no public sign-up and `unreadable: true`, so every older build stays locked on it.
* "like": the note's key with no accounts, tokens or public sign-up, but changed (another password or a secret: an older
* build started with FUOBE_PASSWORD writes that, and so could anyone who can write in the folder). An older build could
* let someone in with it, so it is never left in place once the store was moved: it is set aside like any other file.
* null: anything else.
*/
function noteKind(data: AuthJson): "note" | "like" | null {
const some = (v: unknown) => Array.isArray(v) && v.length > 0;
if (data[TOMBSTONE_KEY] === undefined || some(data.accounts) || some(data.tokens) || data.publicSignup) return null;
const locked =
data.password === "00:00" && !data.secret && data.unreadable === true && Array.isArray(data.tokens) && (data.accounts === undefined || Array.isArray(data.accounts));
return locked ? "note" : "like";
}
/**
* Write a file the way auth.json was always written: a temporary file (a name no other server or container uses),
* then a rename, readable by this user only. Gives the file's stat key as written: the rename keeps it, and a stat
* after the rename could already see another server's file.
*/
function writeAtomic(file: string, text: string): string {
fs.mkdirSync(path.dirname(file), { recursive: true });
const tmp = `${file}.${process.pid}.${crypto.randomBytes(4).toString("hex")}.tmp`;
try {
fs.writeFileSync(tmp, text, { mode: 0o600 });
const st = fs.statSync(tmp);
fs.renameSync(tmp, file);
return `${st.mtimeMs}:${st.size}:${st.ino}`;
} catch (e) {
fs.rmSync(tmp, { force: true });
throw e;
}
}
function writeTombstone(): void {
try {
const got = readBytes(AUTH_FILE, false, 64 * 1024);
if (!("error" in got) && got.raw.toString("utf8") === TOMBSTONE) return;
writeAtomic(AUTH_FILE, TOMBSTONE);
} catch (e) {
console.warn(`[fuobe] Couldn't write the note in place of auth.json (${(e as NodeJS.ErrnoException).code ?? (e as Error).message})`);
}
}
// ---------------------------------------------------------------------------
// Rows <-> today's shapes
export interface AccountRow {
id: string;
email: string;
password_hash: string;
created_at: number;
password_changed_at: number | null;
last_login_at: number | null;
disabled: number;
sessions_after: number | null;
extra_json: string | null;
}
export interface GpuRow {
account_id: string;
mode: "link" | "computer" | null;
url: string | null;
token: string | null;
}
export interface KeyRow {
account_id: string;
provider: string;
sealed: string;
last4: string;
added_at: number;
rejected_at: number | null;
}
interface TokenRow {
id: string;
name: string;
hash: string;
prefix: string;
created_at: number;
last_used_at: number | null;
extra_json: string | null;
}
const extraOf = (json: string | null): Record<string, unknown> => (json ? (JSON.parse(json) as Record<string, unknown>) : {});
export function gpuFrom(g: GpuRow): AccountGpu {
if (g.mode === "computer") return { mode: "computer" };
return { ...(g.mode === "link" && { mode: "link" as const }), url: g.url ?? "", ...(g.token !== null && { token: g.token }) };
}
export function keyFrom(k: KeyRow): StoredKey {
return { sealed: k.sealed, last4: k.last4, addedAt: k.added_at, ...(k.rejected_at !== null && { rejectedAt: k.rejected_at }) };
}
/** An account as auth.json had it: optional fields only when set, `gpu` and `keys` only when there are some. */
export function accountFrom(r: AccountRow, gpu?: GpuRow, keys?: KeyRow[]): Account {
const a = { ...extraOf(r.extra_json), id: r.id, email: r.email, hash: r.password_hash, createdAt: r.created_at } as Account;
if (r.password_changed_at !== null) a.passwordChangedAt = r.password_changed_at;
if (r.last_login_at !== null) a.lastLoginAt = r.last_login_at;
if (r.disabled) a.disabled = true;
if (r.sessions_after !== null) a.sessionsAfter = r.sessions_after;
if (gpu) a.gpu = gpuFrom(gpu);
if (keys?.length) a.keys = Object.fromEntries(keys.map((k) => [k.provider, keyFrom(k)]));
return a;
}
const tokenFrom = (t: TokenRow): ApiToken => ({
...extraOf(t.extra_json),
id: t.id,
name: t.name,
hash: t.hash,
prefix: t.prefix,
createdAt: t.created_at,
...(t.last_used_at !== null && { lastUsedAt: t.last_used_at }),
});
const TOP_KNOWN = ["password", "secret", "tokens", "accounts", "publicSignup", "signedOut"];
const ACCOUNT_KNOWN = ["id", "email", "hash", "createdAt", "passwordChangedAt", "lastLoginAt", "disabled", "sessionsAfter", "gpu", "keys"];
const TOKEN_KNOWN = ["id", "name", "hash", "prefix", "createdAt", "lastUsedAt"];
function extraJson(o: object, known: string[]): string | null {
const rest = Object.fromEntries(Object.entries(o).filter(([k]) => !known.includes(k)));
return Object.keys(rest).length ? JSON.stringify(rest) : null;
}
/** Everything in the store, as auth.json holds it (accounts and tokens in the order they were added). */
export function exportJson(): AuthJson {
const inst = sql("SELECT secret, password_hash, public_signup, extra_json FROM auth_install WHERE id = 1").get() as
| { secret: string; password_hash: string | null; public_signup: number; extra_json: string | null }
| undefined;
const gpus = new Map((sql("SELECT * FROM auth_account_gpu").all() as GpuRow[]).map((g) => [g.account_id, g]));
const keys = new Map<string, KeyRow[]>();
for (const k of sql("SELECT * FROM auth_account_keys ORDER BY rowid").all() as KeyRow[]) keys.set(k.account_id, [...(keys.get(k.account_id) ?? []), k]);
const accounts = (sql("SELECT * FROM auth_accounts ORDER BY rowid").all() as AccountRow[]).map((r) => accountFrom(r, gpus.get(r.id), keys.get(r.id)));
const tokens = (sql("SELECT * FROM auth_tokens ORDER BY rowid").all() as TokenRow[]).map(tokenFrom);
const signedOut = sql("SELECT hash, exp FROM auth_signed_out ORDER BY rowid").all() as { hash: string; exp: number }[];
return {
...extraOf(inst?.extra_json ?? null),
...(inst?.password_hash != null && { password: inst.password_hash }),
secret: inst?.secret ?? "",
tokens,
...(accounts.length && { accounts }),
...(inst?.public_signup && { publicSignup: true }),
...(signedOut.length && { signedOut }),
};
}
/** Signed-out cookies that haven't expired, one per cookie (the latest expiry). */
function liveSignedOut(list: unknown, at: number): { hash: string; exp: number }[] {
if (list === undefined || list === null) return [];
if (!Array.isArray(list)) throw new DataError("signedOut is not a list");
const by = new Map<string, number>();
for (const e of list as { hash?: unknown; exp?: unknown }[]) {
if (!e || typeof e.hash !== "string" || typeof e.exp !== "number" || !Number.isFinite(e.exp)) throw new DataError("a signed-out entry is malformed");
if (e.exp > at) by.set(e.hash, Math.max(e.exp, by.get(e.hash) ?? 0));
}
return [...by].map(([hash, exp]) => ({ hash, exp }));
}
const isStr = (v: unknown): v is string => typeof v === "string";
const isNum = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v);
const optNum = (v: unknown, what: string): number | null => {
if (v === undefined || v === null) return null;
if (!isNum(v)) throw new DataError(`${what} is not a number`);
return v;
};
/** Put a parsed auth.json in the (empty) tables, checking each value's type. */
function insertAll(data: AuthJson, at: number, version: number): Counts {
if (data.secret !== undefined && !isStr(data.secret)) throw new DataError("the secret is not text");
if (data.password !== undefined && data.password !== null && !isStr(data.password)) throw new DataError("the password hash is not text");
sql("INSERT INTO auth_install (id, secret, password_hash, public_signup, extra_json, version, updated_at) VALUES (1, ?, ?, ?, ?, ?, ?)").run(
data.secret ?? "",
data.password ?? null,
data.publicSignup ? 1 : 0,
extraJson(data, TOP_KNOWN),
version,
at,
);
const counts: Counts = { accounts: 0, tokens: 0, keys: 0, gpus: 0, signedOut: 0 };
const tokens = data.tokens ?? [];
if (!Array.isArray(tokens)) throw new DataError("tokens is not a list");
const addToken = sql("INSERT INTO auth_tokens (id, name, hash, prefix, created_at, last_used_at, extra_json) VALUES (?, ?, ?, ?, ?, ?, ?)");
for (const t of tokens) {
if (!t || !isStr(t.id) || !isStr(t.name) || !isStr(t.hash) || !isStr(t.prefix) || !isNum(t.createdAt)) throw new DataError("an API token is malformed");
constrained(() => addToken.run(t.id, t.name, t.hash, t.prefix, t.createdAt, optNum(t.lastUsedAt, "a token's last use"), extraJson(t, TOKEN_KNOWN)), "two API tokens are the same");
counts.tokens++;
}
const accounts = data.accounts ?? [];
if (!Array.isArray(accounts)) throw new DataError("accounts is not a list");
const addAccount = sql(
"INSERT INTO auth_accounts (id, email, password_hash, created_at, password_changed_at, last_login_at, disabled, sessions_after, extra_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
);
const addGpu = sql("INSERT INTO auth_account_gpu (account_id, mode, url, token) VALUES (?, ?, ?, ?)");
const addKey = sql("INSERT INTO auth_account_keys (account_id, provider, sealed, last4, added_at, rejected_at) VALUES (?, ?, ?, ?, ?, ?)");
const half = Math.floor(accounts.length / 2);
accounts.forEach((a, i) => {
if (i === half) globalThis.__fuobeAuthImportFault?.();
if (!a || typeof a !== "object" || !isStr(a.id) || !a.id || !isStr(a.hash) || !isNum(a.createdAt)) throw new DataError(`account ${i + 1} is malformed`);
if (!isStr(a.email) || !a.email || a.email !== a.email.toLowerCase()) throw new DataError(`account ${i + 1} has an email that isn't lower-case`);
if (a.disabled !== undefined && a.disabled !== null && typeof a.disabled !== "boolean") throw new DataError(`account ${i + 1}'s disabled is not true or false`);
constrained(
() =>
addAccount.run(
a.id,
a.email,
a.hash,
a.createdAt,
optNum(a.passwordChangedAt, "a password change time"),
optNum(a.lastLoginAt, "a log-in time"),
a.disabled ? 1 : 0,
optNum(a.sessionsAfter, "a sign-out time"),
extraJson(a, ACCOUNT_KNOWN),
),
`account ${i + 1} has the id or the email of an account before it`,
);
counts.accounts++;
const g = a.gpu as AccountGpu | null | undefined;
if (g !== undefined && g !== null) {
if (typeof g !== "object") throw new DataError(`account ${i + 1}'s GPU is malformed`);
if (g.mode === "computer") addGpu.run(a.id, "computer", null, null);
else if ((g.mode === undefined || g.mode === "link") && isStr(g.url) && (g.token === undefined || isStr(g.token))) addGpu.run(a.id, g.mode ?? null, g.url, g.token ?? null);
else throw new DataError(`account ${i + 1}'s GPU is malformed`);
counts.gpus++;
}
if (a.keys !== undefined && a.keys !== null) {
if (typeof a.keys !== "object" || Array.isArray(a.keys)) throw new DataError(`account ${i + 1}'s keys are malformed`);
for (const [p, k] of Object.entries(a.keys as Record<string, StoredKey | null | undefined>)) {
if (k === undefined || k === null) continue;
if (!isStr(k.sealed) || !isStr(k.last4) || !isNum(k.addedAt)) throw new DataError(`account ${i + 1}'s ${p} key is malformed`);
addKey.run(a.id, p, k.sealed, k.last4, k.addedAt, optNum(k.rejectedAt, "a key's refusal time"));
counts.keys++;
}
}
});
const addSignedOut = sql("INSERT INTO auth_signed_out (hash, exp) VALUES (?, ?)");
for (const e of liveSignedOut(data.signedOut, at)) {
addSignedOut.run(e.hash, e.exp);
counts.signedOut++;
}
return counts;
}
function constrained(fn: () => unknown, what: string): void {
try {
fn();
} catch (e) {
if (String((e as { code?: string }).code).startsWith("SQLITE_CONSTRAINT")) throw new DataError(what);
throw e;
}
}
/** Sorted keys all the way down, so two values compare as text. */
function stable(v: unknown): string {
return JSON.stringify(v, (_k, x: unknown) =>
x && typeof x === "object" && !Array.isArray(x) ? Object.fromEntries(Object.entries(x).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) : x,
);
}
/** What must be the same after a round trip: a missing field and null, false or an empty list mean the same. */
function canon(data: AuthJson, at: number): string {
const rest = (o: object, known: string[]) => Object.fromEntries(Object.entries(o).filter(([k]) => !known.includes(k)));
const gpu = (g: AccountGpu | null | undefined) => (!g ? null : g.mode === "computer" ? { mode: "computer" } : { mode: g.mode ?? null, url: g.url, token: g.token ?? null });
const keys = (k: Account["keys"] | null | undefined) =>
Object.entries(k ?? {})
.filter(([, v]) => !!v)
.map(([p, v]) => ({ p, sealed: v!.sealed, last4: v!.last4, addedAt: v!.addedAt, rejectedAt: v!.rejectedAt ?? null }))
.sort((a, b) => (a.p < b.p ? -1 : 1));
return stable({
extra: rest(data, TOP_KNOWN),
password: data.password ?? null,
secret: data.secret ?? "",
publicSignup: !!data.publicSignup,
tokens: (data.tokens ?? []).map((t) => ({ ...rest(t, TOKEN_KNOWN), id: t.id, name: t.name, hash: t.hash, prefix: t.prefix, createdAt: t.createdAt, lastUsedAt: t.lastUsedAt ?? null })),
accounts: (data.accounts ?? []).map((a) => ({
extra: rest(a, ACCOUNT_KNOWN),
id: a.id,
email: a.email,
hash: a.hash,
createdAt: a.createdAt,
passwordChangedAt: a.passwordChangedAt ?? null,
lastLoginAt: a.lastLoginAt ?? null,
disabled: !!a.disabled,
sessionsAfter: a.sessionsAfter ?? null,
gpu: gpu(a.gpu),
keys: keys(a.keys),
})),
signedOut: liveSignedOut(data.signedOut, at).sort((a, b) => (a.hash < b.hash ? -1 : 1)),
});
}
/** Inside the transaction: the rows are what the file says, counted and read back. */
function verify(data: AuthJson, counts: Counts, at: number): void {
const n = (table: string) => (sql(`SELECT COUNT(*) AS n FROM ${table}`).get() as { n: number }).n;
const have: Counts = { accounts: n("auth_accounts"), tokens: n("auth_tokens"), keys: n("auth_account_keys"), gpus: n("auth_account_gpu"), signedOut: n("auth_signed_out") };
if (stable(have) !== stable(counts) || counts.accounts !== (data.accounts ?? []).length || counts.tokens !== (data.tokens ?? []).length) {
throw new DataError("the rows counted after the move differ from the file");
}
if (canon(exportJson(), at) !== canon(data, at)) throw new DataError("the data read back differs from the file");
}
function clearAll(): void {
for (const t of ["auth_account_keys", "auth_account_gpu", "auth_signed_out", "auth_accounts", "auth_tokens", "auth_install"]) sql(`DELETE FROM ${t}`).run();
}
// ---------------------------------------------------------------------------
// Moving auth.json in
function importFile(file: { data: AuthJson; key: string | null; sha: string }): void {
let counts: Counts | null = null;
immediate(() => {
if (marker()) return; // another server moved it in while this one waited for the lock
if (hasRows()) throw new DataError("the sign-in tables already hold rows, without a record of how they got there");
const at = now();
counts = insertAll(file.data, at, 1);
verify(file.data, counts, at);
sql(
"INSERT INTO auth_migration (id, source, state, started_at, synced_at, json_stat, json_sha256, counts_json) VALUES (1, 'auth.json', 'bridge', ?, ?, ?, ?, ?)",
).run(at, at, file.key, file.sha, JSON.stringify(counts));
});
if (counts) {
const c = counts as Counts;
console.info(
`[fuobe] Sign-in data moved from auth.json into the database: ${c.accounts} accounts, ${c.tokens} API tokens, ${c.keys} saved keys, ${c.gpus} GPU links. auth.json is kept in step for ${Math.round(bridgeMs() / 60_000)} minutes, then kept as auth.json.migrated-<time>.`,
);
}
}
/** Write the store to auth.json in its usual format (bridge only), and remember the file as this server left it. */
function exportFile(): void {
const text = JSON.stringify(exportJson(), null, 2);
const key = writeAtomic(AUTH_FILE, text);
sql("UPDATE auth_migration SET json_stat = ?, json_sha256 = ? WHERE id = 1").run(key, crypto.createHash("sha256").update(text).digest("hex"));
}
/**
* Bridge, inside a write transaction: auth.json is not as this store left it. An older server changed it: read it all
* back in. It is gone: write it again (that server would otherwise see no password). It can't be read: leave both.
*/
function syncFromFile(m: Marker): void {
const key = statKey();
if (key === m.json_stat || (key !== null && key === s.bridgeBad)) return;
if (key === null) {
exportFile();
return;
}
const file = readFile();
if ("error" in file) {
s.bridgeBad = key;
console.warn(`[fuobe] auth.json changed and can't be read back in (${file.error}): the database keeps sign-in as it was.`);
return;
}
if (file.sha === m.json_sha256) {
sql("UPDATE auth_migration SET json_stat = ? WHERE id = 1").run(key);
return;
}
const at = now();
const version = (sql("SELECT version FROM auth_install WHERE id = 1").get() as { version: number } | undefined)?.version ?? 0;
try {
// A savepoint: data that can't go in leaves the rows as they were, and the change that brought us here goes on.
db().transaction(() => {
clearAll();
const counts = insertAll(file.data, at, version + 1);
verify(file.data, counts, at);
})();
} catch (e) {
if (!(e instanceof DataError)) throw e;
s.bridgeBad = key;
console.warn(`[fuobe] auth.json changed but can't be read back in (${e.message}): the database keeps sign-in as it was.`);
return;
}
sql("UPDATE auth_migration SET synced_at = ?, json_stat = ?, json_sha256 = ? WHERE id = 1").run(at, key, file.sha);
}
/** Bridge: catch up with auth.json when it changed, and put it aside once its time is up. */
function bridgeTick(m: Marker): AuthStoreMode {
const key = statKey();
if (key !== m.json_stat && (key === null || key !== s.bridgeBad)) {
try {
immediate(() => {
const inner = marker();
if (inner?.state === "bridge") syncFromFile(inner);
});
} catch (e) {
if (!(e instanceof DataError)) console.warn(`[fuobe] Couldn't catch up with auth.json: ${(e as Error).message}`);
}
}
const t = now();
if (t - s.retireCheckedAt >= Math.min(60_000, bridgeMs())) {
s.retireCheckedAt = t;
const fresh = marker();
if (fresh?.state === "bridge" && t - fresh.started_at >= bridgeMs() && t - (fresh.synced_at ?? fresh.started_at) >= quietMs()) retire();
}
return s.done ? (s.held?.mode ?? "done") : "bridge";
}
/** "auth.json.<kind>-20261104T101500Z", or with -2, -3… when that is taken. */
function freeName(kind: "migrated" | "stray", at: number): string {
const stamp = new Date(at).toISOString().replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z");
for (let i = 1; ; i++) {
const name = `auth.json.${kind}-${stamp}${i > 1 ? `-${i}` : ""}`;
if (!lexists(path.join(DATA_DIR, name))) return name;
}
}
const backupName = (at: number) => freeName("migrated", at);
/** There is something under that name (a link counts, wherever it points). */
function lexists(file: string): boolean {
try {
fs.lstatSync(file);
return true;
} catch {
return false;
}
}
function retire(): void {
try {
immediate(() => {
const m = marker();
if (m?.state !== "bridge") return;
// Changed since the last look: read it in, and wait for another quiet spell.
if (statKey() !== m.json_stat) {
syncFromFile(m);
if (marker()?.synced_at !== m.synced_at) return;
}
const at = now();
sql("UPDATE auth_migration SET state = 'done', backup_file = ?, retired_at = ? WHERE id = 1").run(backupName(at), at);
});
} catch (e) {
console.warn(`[fuobe] Couldn't put auth.json aside yet: ${(e as Error).message}`);
return;
}
const m = marker();
if (m?.state === "done") afterMove(m);
}
/**
* FUOBE_AUTH_REIMPORT=1, on this process's first look at the store only (the start it was set for): a flag left in the
* server's settings never moves in an auth.json that turns up later. On globalThis, so every copy of this module in
* the process shares it.
*/
function takeReimport(): boolean {
if (globalThis.__fuobeAuthReimportTaken) return false;
globalThis.__fuobeAuthReimportTaken = true;
const asked = process.env.FUOBE_AUTH_REIMPORT?.trim() === "1";
if (asked) {
console.warn(
"[fuobe] FUOBE_AUTH_REIMPORT=1 is set: it counts for this start only (an auth.json in the data folder now replaces the database's sign-in data). Remove it from the server's settings.",
);
}
return asked;
}
const EXPORTED_BACK =
"Remove auth.json to keep the database's sign-in data, or start once with FUOBE_AUTH_REIMPORT=1 to replace the database's sign-in data with it. See \"Sign-in data\" in docs/SELF-HOSTING.md.";
/** Keep sign-in closed for what afterMove found, until the next look. */
function hold(mode: "unreadable" | "failed", message: string): AuthStoreMode {
s.held = { mode, message };
return failClosed(mode, message);
}
/**
* The store was moved (done), or written out for an older build (exported): look at auth.json, at most once a minute.
* - The note, or no file: as it should be (the note is written again when it is missing).
* - The file as the bridge last left it: put aside under its backup name (a crash between the database's change and
* that, or a copy put back that holds nothing new), and the note put in its place.
* - exported: the file scripts/auth-export.mjs wrote, maybe changed by the older build since: moved back in.
* - Any other auth.json (a backup copied back, a file from elsewhere, one that can't be read): never read as sign-in
* data, which could bring back a revoked token or an old password. It is set aside unread (setAside) and the site
* keeps serving from the database. On a start with FUOBE_AUTH_REIMPORT=1 (`asked`) it is moved in instead.
* Here auth.json is never read through a link, nor when it is a named pipe, a device or too big: those are set aside.
*/
function afterMove(m: Marker, asked = false): AuthStoreMode {
s.done = true;
s.fileCheckedAt = now();
s.held = null;
const file = lexists(AUTH_FILE) ? readFile(false) : null;
const isNote = (f: FileRead | null) => f === null || ("error" in f && f.error === "tombstone");
const note = isNote(file);
if (m.state === "exported") {
const like = (f: FileRead | null) => f !== null && "error" in f && f.error === LIKE_NOTE;
if (note || like(file)) {
// Back to done, only while auth.json is still not an export (scripts/auth-export.mjs puts its file in place while
// it holds the write lock, so an export that lands now is seen here).
let back = false;
let again: FileRead | null = file;
immediate(() => {
if (marker()?.state !== "exported") return;
again = lexists(AUTH_FILE) ? readFile(false) : null;
if (!isNote(again) && !like(again)) return;
sql("UPDATE auth_migration SET state = 'done', exported_version = NULL WHERE id = 1").run();
back = true;
});
if (!back) {
s.fileCheckedAt = 0; // look again at the next request
return "done";
}
if (like(again)) setAside("stray");
else writeTombstone();
return "done";
}
if ("error" in file!) return hold("unreadable", `${AUTH_FILE} can't be read (${file.error}).`);
return reimport(file!, "exported", asked);
}
if (note) {
// Missing, or an earlier form of the note: this build's note (written only when it differs).
writeTombstone();
return "done";
}
if (file && !("error" in file)) {
if (m.source === "auth.json" && file.sha === m.json_sha256) {
putAside(m);
return "done";
}
if (asked) return reimport(file, "asked", true);
}
setAside("error" in file! && file.error !== LIKE_NOTE ? "unreadable" : "stray");
return "done";
}
/**
* Why an auth.json was set aside: sign-in data, a file that isn't sign-in data, a symbolic link (never followed), or one
* FUOBE_AUTH_REIMPORT=1 couldn't move in.
*/
export type StrayReason = "stray" | "unreadable" | "link" | "import-failed";
/** What a set-aside file lists, for the owner's notice: counts only (null when it isn't sign-in data). */
export interface StrayCounts {
accounts: number;
tokens: number;
/** Accounts it lists whose id the database doesn't have. */
accountsNotInDatabase: number;
}
/** The names setAside gives (and the only ones the notice and the account sweep read back). */
const STRAY_NAME = /^auth\.json\.stray-\d{8}T\d{6}Z(?:-\d+)?$/;
/** A set-aside file bigger than this isn't read for its counts (it is read under the database's write lock). */
const STRAY_READ_MAX = 64 * 1024 * 1024;
/** Notices kept (the newest). */
const STRAY_ROWS_MAX = 100;
/** Account ids kept per file for the sweep. */
const STRAY_IDS_MAX = 100_000;
/** A plain file's bytes, never through a link (null: not a plain file, too big, or it can't be read). */
function readPlain(file: string): Buffer | null {
const got = readBytes(file, false, STRAY_READ_MAX);
return "error" in got ? null : got.raw;
}
/** Readable by this user only, through the file itself (never a link put in its place). Not on Windows. */
function keepPrivate(file: string): void {
if (process.platform === "win32") return;
let fd: number | undefined;
try {
fd = fs.openSync(file, fs.constants.O_RDONLY | NOFOLLOW | NONBLOCK);
if (fs.fstatSync(fd).isFile()) fs.fchmodSync(fd, 0o600);
} catch {
} finally {
if (fd !== undefined) fs.closeSync(fd);
}
}
/**
* A second name for auth.json can't be made private on its own: the file has other names already (a hard link to a file
* something else serves, whose mode must not change), or another user owns it (its mode can't be changed).
*/
function sharedFile(st: fs.Stats): boolean {
if (process.platform === "win32") return false;
return st.nlink > 2 || (typeof process.getuid === "function" && st.uid !== process.getuid());
}
const sameFile = (a: fs.Stats, b: fs.Stats) => a.dev === b.dev && a.ino === b.ino && a.size === b.size && a.mtimeMs === b.mtimeMs;
/** How many accounts and tokens a set-aside file lists, and the ids of its accounts the database doesn't have. */
function strayFacts(raw: Buffer | null): { counts: StrayCounts | null; ids: string[] } {
const parsed = raw ? parseObject(raw) : null;
if (!parsed || "error" in parsed) return { counts: null, ids: [] };
const list = (v: unknown): unknown[] => (Array.isArray(v) ? v : []);
const accounts = list(parsed.data.accounts);
const known = new Set((sql("SELECT id FROM auth_accounts").all() as { id: string }[]).map((r) => r.id));
const ids = new Set<string>();
for (const a of accounts) {
const id = a && typeof a === "object" ? (a as { id?: unknown }).id : undefined;
if (typeof id === "string" && id && id.length <= 64 && !known.has(id)) ids.add(id);
}
return { counts: { accounts: accounts.length, tokens: list(parsed.data.tokens).length, accountsNotInDatabase: ids.size }, ids: [...ids].slice(0, STRAY_IDS_MAX) };
}
/** The second name setAside made for auth.json: what auth.json was then (and the link's text, for a link). */
interface Kept {
target: string;
name: string;
kept: fs.Stats;
link: string | null;
}
/** auth.json is still the file setAside gave a second name (the same file, or the same link). */
function stillKept(current: fs.Stats, c: Kept): boolean {
if (c.link !== null) {
try {
return current.isSymbolicLink() && fs.readlinkSync(AUTH_FILE) === c.link && sameFile(current, c.kept);
} catch {
return false;
}
}
return !current.isSymbolicLink() && !current.isDirectory() && sameFile(current, c.kept);
}
/**
* An auth.json found once the store was moved, that isn't the note, the bridge's last copy or an export: kept as
* auth.json.stray-<UTC time>, unread as sign-in data, and the note put back. Under the database's write lock, so two
* servers never both act on one file. auth.json is never missing on the way (an older build would read "no file" as
* "no password"): the file gets its second name first (a hard link, or a copy), its notice row is written, and last the
* note replaces auth.json by a rename, only when auth.json is still the file that was kept (otherwise what is there now
* is looked at again next time). When a step fails, the transaction undoes the row, and the second name is removed
* again while auth.json still holds the same file, so a failure never piles up copies.
*/
function setAside(reason: StrayReason): void {
let made: { name: string; counts: StrayCounts | null; link: boolean } | null = null;
let created: Kept | null = null;
let swapped = false;
try {
immediate(() => {
created = null;
swapped = false;
made = null;
// Another server moved it in (FUOBE_AUTH_REIMPORT=1) or an export happened a moment ago: not this one's to do.
if (marker()?.state !== "done") return;
let before: fs.Stats;
try {
before = fs.lstatSync(AUTH_FILE);
} catch {
return;
}
// A folder named auth.json: nothing reads it, and nothing can be renamed over it.
if (before.isDirectory()) throw Object.assign(new Error("it is a folder"), { code: "a folder" });
const name = freeName("stray", now());
const target = path.join(DATA_DIR, name);
let link: string | null = null;
let kept = before;
if (before.isSymbolicLink()) {
// A link to a file elsewhere: the same link under the new name (what it points to is never touched or read).
link = fs.readlinkSync(AUTH_FILE);
fs.symlinkSync(link, target);
} else {
let linked = false;
try {
fs.linkSync(AUTH_FILE, target);
linked = true;
} catch (e) {
const code = (e as NodeJS.ErrnoException).code;
// A named pipe or a device is never copied (that would read it).
if (code === "EEXIST" || code === "ENOENT" || !before.isFile()) throw e;
// A file system without links: a copy, below.
}
if (linked) {
const st = fs.lstatSync(target);
if (sharedFile(st) && before.isFile()) {
// Its mode must stay as it is: a private copy instead.
fs.rmSync(target, { force: true });
linked = false;
} else kept = st;
}
if (!linked) fs.copyFileSync(AUTH_FILE, target, fs.constants.COPYFILE_EXCL);
}
created = { target, name, kept, link };
const raw = link !== null ? null : readPlain(target);
const parsed = raw ? parse(raw) : null;
if (parsed && "error" in parsed && parsed.error === "tombstone") {
// The note itself, put there a moment ago: nothing to keep.
fs.rmSync(target, { force: true });
created = null;
return;
}
keepPrivate(target);
const facts = strayFacts(raw);
const why: StrayReason = reason === "import-failed" ? reason : link !== null ? "link" : facts.counts ? "stray" : "unreadable";
sql("INSERT OR REPLACE INTO auth_stray_files (name, found_at, reason, counts_json, account_ids_json) VALUES (?, ?, ?, ?, ?)").run(
name,
now(),
why,
facts.counts && JSON.stringify(facts.counts),
facts.ids.length ? JSON.stringify(facts.ids) : null,
);
// Notices for files that are gone, and past the newest STRAY_ROWS_MAX.
for (const r of sql("SELECT name FROM auth_stray_files").all() as { name: string }[]) {
if (!STRAY_NAME.test(r.name) || !lexists(path.join(DATA_DIR, r.name))) sql("DELETE FROM auth_stray_files WHERE name = ?").run(r.name);
}
sql("DELETE FROM auth_stray_files WHERE name NOT IN (SELECT name FROM auth_stray_files ORDER BY found_at DESC, name DESC LIMIT ?)").run(STRAY_ROWS_MAX);
made = { name, counts: facts.counts, link: link !== null };
// Last: the note in place of auth.json (when this fails, the row above is undone).
let current: fs.Stats | null = null;
try {
current = fs.lstatSync(AUTH_FILE);
} catch {}
if (!current) {
writeTombstone();
swapped = true;
} else if (stillKept(current, created)) {
writeAtomic(AUTH_FILE, TOMBSTONE);
swapped = true;
}
});
} catch (e) {
const c = created as Kept | null;
const code = (e as NodeJS.ErrnoException).code ?? (e as Error).message;
let current: fs.Stats | null = null;
try {
current = fs.lstatSync(AUTH_FILE);
} catch {}
if (c && !swapped && current && stillKept(current, c)) {
// auth.json still holds what the second name holds: remove that name, and try again at the next look.
try {
fs.rmSync(c.target, { force: true });
} catch {}
} else if (c) {
warnOnce("stray", `An auth.json in the data folder was kept as ${c.name}, but its notice couldn't be saved (${code}). It is not read: sign-in goes on from the database.`);
return;
}
warnOnce("stray", `An auth.json is in the data folder but couldn't be set aside (${code}). It is not read: sign-in goes on from the database.`);
return;
}
if (made) {
const { name, counts, link } = made as { name: string; counts: StrayCounts | null; link: boolean };
const what = counts
? `it lists ${counts.accounts} accounts (${counts.accountsNotInDatabase} not in the database) and ${counts.tokens} API tokens`
: link
? "it is a symbolic link, never followed"
: "it isn't sign-in data Fuobe can read";
console.warn(
`[fuobe] An auth.json was found in the data folder after sign-in data moved into the database. It was not used: it is kept as ${name} (${what}), and sign-in goes on from the database. Delete that file when you don't need it, or see "Sign-in data" in docs/SELF-HOSTING.md to use it instead.`,
);
}
}
interface StrayRow {
name: string;
found_at: number;
reason: StrayReason;
counts_json: string | null;
}
/** The set-aside files still in the data folder, newest first (for the notices: never the account ids). */
function strayRows(): StrayRow[] {
return (sql("SELECT name, found_at, reason, counts_json FROM auth_stray_files ORDER BY found_at DESC, name DESC").all() as StrayRow[]).filter(
(r) => STRAY_NAME.test(r.name) && lexists(path.join(DATA_DIR, r.name)),
);
}
/** For the owner (GET /api/admin/accounts): each auth.json set aside unread that is still in the data folder. */
export interface SignInNotice {
file: string;
foundAt: number;
reason: StrayReason;
/** null: the file isn't sign-in data Fuobe can read, or is a link (never followed). */
counts: StrayCounts | null;
}
export function signInNotices(): SignInNotice[] {
try {
if (authStoreClosed()) return [];
return strayRows().map((r) => ({ file: r.name, foundAt: r.found_at, reason: r.reason, counts: r.counts_json ? (JSON.parse(r.counts_json) as StrayCounts) : null }));
} catch {
return [];
}
}
/**
* Accounts a set-aside auth.json lists that the database doesn't have, while that file is still there: the account
* sweep keeps their files (an older build may have signed them up), until the owner deletes the file or moves it in.
* Throws when it can't tell (the sweep then removes nothing).
*/
export function strayAccountIds(): Set<string> {
const ids = new Set<string>();
const rows = sql("SELECT name, account_ids_json FROM auth_stray_files WHERE account_ids_json IS NOT NULL").all() as { name: string; account_ids_json: string }[];
for (const r of rows) {
if (!STRAY_NAME.test(r.name) || !lexists(path.join(DATA_DIR, r.name))) continue;
for (const id of JSON.parse(r.account_ids_json) as string[]) ids.add(id);
}
return ids;
}
/** auth.json as the bridge last left it: kept under the backup name (never written over), and the note in its place. */
function putAside(m: Marker): void {
try {
if (m.backup_file) {
const backup = path.join(DATA_DIR, m.backup_file);
let made = true;
try {
// A link can't replace a file already there, so a second server finishing at the same moment can't put the
// note over the backup.
fs.linkSync(AUTH_FILE, backup);
} catch (e) {
const code = (e as NodeJS.ErrnoException).code;
if (code === "EEXIST") made = false;
else fs.copyFileSync(AUTH_FILE, backup, fs.constants.COPYFILE_EXCL); // a file system without links
}
if (made) {
if (process.platform !== "win32") fs.chmodSync(backup, 0o600);
console.info(
`[fuobe] auth.json was kept as ${m.backup_file}: sign-in data is in the database now. That file still holds the accounts, password hashes and sealed keys as they were at the move, including accounts deleted since: delete it once you won't go back to an older version.`,
);
}
}
writeTombstone();
} catch (e) {
console.warn(`[fuobe] Couldn't put auth.json aside: ${(e as NodeJS.ErrnoException).code ?? (e as Error).message}`);
}
}
/**
* Move auth.json in again over the store's rows, in one transaction, checked as the first move is. From an export, only
* when nothing changed in the database since (else the database's changes would be lost): sign-in fails closed instead.
* Then the bridge starts again (an older build may still run on the file), and puts the file aside under a new name.
*/
function reimport(file: { data: AuthJson; key: string | null; sha: string }, why: "exported" | "asked", asked: boolean): AuthStoreMode {
let counts: Counts | null = null;
try {
immediate(() => {
const m = marker();
if (m?.state !== "done" && m?.state !== "exported") return; // another server did it
const version = (sql("SELECT version FROM auth_install WHERE id = 1").get() as { version: number } | undefined)?.version ?? 0;
if (why === "exported" && m.exported_version !== version && !asked) {
throw new DataError("sign-in data changed in the database after auth.json was exported");
}
const at = now();
clearAll();
counts = insertAll(file.data, at, version + 1);
verify(file.data, counts, at);
sql(
"UPDATE auth_migration SET source = 'auth.json', state = 'bridge', started_at = ?, synced_at = ?, json_stat = ?, json_sha256 = ?, counts_json = ?, backup_file = NULL, retired_at = NULL, exported_version = NULL WHERE id = 1",
).run(at, at, file.key, file.sha, JSON.stringify(counts));
});
} catch (e) {
if (why === "asked" && e instanceof DataError) {
// Asked for, and its data can't go in: the database keeps its sign-in data, and the file is set aside.
console.error(`[fuobe] FUOBE_AUTH_REIMPORT=1: auth.json couldn't be moved into the database (${e.message}); nothing was changed.`);
setAside("import-failed");
return "done";
}
const how = e instanceof DataError ? e.message : `the database said: ${(e as Error).message}`;
return hold("failed", `auth.json couldn't be moved back into the database (${how}); nothing was changed. ${EXPORTED_BACK}`);
}
if (counts) {
const c = counts as Counts;
console.info(
`[fuobe] Sign-in data moved back in from auth.json (${why === "exported" ? "written for an older version" : "FUOBE_AUTH_REIMPORT=1"}): ${c.accounts} accounts, ${c.tokens} API tokens, ${c.keys} saved keys, ${c.gpus} GPU links. auth.json is kept in step for ${Math.round(bridgeMs() / 60_000)} minutes, then kept as auth.json.migrated-<time>.`,
);
}
s.done = false;
s.retireCheckedAt = 0;
const m = marker();
return m?.state === "bridge" ? bridgeTick(m) : m ? afterMove(m) : "fresh";
}
// ---------------------------------------------------------------------------
// The mode
function failClosed(kind: "unreadable" | "failed", message: string): AuthStoreMode {
warnOnce(kind, `${message} Sign-in stays on and nobody can sign in until it is fixed.`, kind === "failed");
return kind;
}
/** Where sign-in data stands now. Moves auth.json in when it is waiting, and keeps the bridge going. */
export function authStoreMode(): AuthStoreMode {
if (globalThis.__fuobeAuthTablesError) return failClosed("failed", `The sign-in tables in the database can't be used (${globalThis.__fuobeAuthTablesError}).`);
if (s.done && now() - s.fileCheckedAt < FILE_CHECK_MS) return s.held ? failClosed(s.held.mode, s.held.message) : "done";
if (s.dbFailedAt && Date.now() - s.dbFailedAt < 5_000) return "failed";
try {
db();
if (globalThis.__fuobeAuthTablesError) return authStoreMode();
const m = marker();
const asked = takeReimport();
if (m?.state === "done" || m?.state === "exported") return afterMove(m, asked);
s.done = false;
s.held = null;
if (m?.state === "bridge") return bridgeTick(m);
return beforeMove();
} catch (e) {
s.done = false;
s.dbFailedAt = Date.now();
return failClosed("failed", `The sign-in data in the database can't be read (${(e as Error).message}).`);
}
}
/** No marker yet: a new install, or an auth.json to move in. */
function beforeMove(): AuthStoreMode {
const key = statKey();
if (key === null) {
if (hasRows()) {
// Another server's first write (its marker and its rows, in one transaction) can land between the two reads.
const set = marker();
if (set) return set.state === "bridge" ? bridgeTick(set) : afterMove(set);
return failClosed("failed", "The database holds sign-in rows without a record of where they came from.");
}
return "fresh";
}
if (s.bad?.key === key && (s.bad.mode === "unreadable" || s.bad.data)) return s.bad.mode;
if (s.bad?.key === key && Date.now() - s.bad.at < 5_000) return s.bad.mode;
const file = readFile();
if ("error" in file) {
// Another server set the store up a moment ago (and left the note in place of auth.json).
const set = marker();
if (set) return set.state === "bridge" ? bridgeTick(set) : afterMove(set);
s.bad = { key, mode: "unreadable", at: Date.now(), data: false };
return failClosed(
"unreadable",
file.error === "tombstone" || file.error === LIKE_NOTE
? `${AUTH_FILE} says sign-in data is in the database, but the database has none: restore fuobe.db, or copy auth.json.migrated-<time> back to auth.json.`
: `${AUTH_FILE} can't be read (${file.error}).`,
);
}
try {
importFile(file);
s.bad = null;
} catch (e) {
const data = e instanceof DataError;
s.bad = { key, mode: "failed", at: Date.now(), data };
return failClosed("failed", `auth.json couldn't be moved into the database (${(e as Error).message}); nothing was changed.`);
}
const m = marker();
if (!m) return "fresh";
return m.state === "bridge" ? bridgeTick(m) : afterMove(m);
}
/** Sign-in can't be used (the database, or auth.json waiting to move in, can't be read): fail closed. */
export const authStoreClosed = (mode = authStoreMode()) => mode === "unreadable" || mode === "failed";
/**
* The store failed (not just an unreadable auth.json waiting): the database can't be used, an older install's auth.json
* couldn't move in, or an export came back after the database changed. The proxy answers every request with a calm
* "back in a moment" page (503, so a new release in this state is never switched to); the log says why.
*/
export const authStoreFailed = () => authStoreMode() === "failed";
const UNREADABLE = "The sign-in file (auth.json) can't be read. Fix or restore it: Fuobe won't write over it.";
const FAILED = "The sign-in data can't be read. Fix or restore it: Fuobe won't write over it.";
/** An older server changed auth.json while a change here was being made: the change is made again on top of it. */
class FileMoved extends Error {}
/**
* Change sign-in data: `fn` runs inside a transaction that holds the write lock. A new install is set up first; during
* the bridge auth.json is caught up with first and written after, in the same transaction. When an older server
* changes the file in between, the transaction is undone and run again on top of that change (up to 3 times).
*/
export function authWrite<T>(fn: () => T): T {
const mode = authStoreMode();
if (mode === "unreadable") throw new Error(UNREADABLE);
if (mode === "failed") throw new Error(FAILED);
let fresh = false;
let out!: T;
for (let attempt = 1; ; attempt++) {
try {
out = immediate(() => {
const m = marker();
const at = now();
if (!m) {
if (statKey() !== null || hasRows()) throw new Error("The sign-in data changed while saving: try again.");
sql("INSERT INTO auth_migration (id, source, state, started_at) VALUES (1, 'new', 'done', ?)").run(at);
fresh = true;
} else if (m.state === "bridge") {
syncFromFile(m);
} else if (m.state === "exported") {
throw new Error("The sign-in data changed while saving: try again.");
}
sql("INSERT OR IGNORE INTO auth_install (id, secret, updated_at) VALUES (1, '', ?)").run(at);
const r = fn();
sql("UPDATE auth_install SET version = version + 1, updated_at = ? WHERE id = 1").run(at);
const after = marker();
if (after?.state === "bridge") {
if (attempt < 3 && statKey() !== after.json_stat) throw new FileMoved();
exportFile();
}
return r;
});
break;
} catch (e) {
if (e instanceof FileMoved) continue;
throw e;
}
}
if (fresh) {
s.done = true;
s.fileCheckedAt = now();
s.held = null;
writeTombstone();
}
s.writes++;
return out;
}
/** Changes with every change to sign-in data, by this server or another one on the same database. */
export function authStoreVersion(): number {
try {
if (authStoreClosed()) return -1 - s.writes;
return (sql("SELECT version FROM auth_install WHERE id = 1").get() as { version: number } | undefined)?.version ?? 0;
} catch {
return -1 - s.writes;
}
}
/**
* On start: move auth.json in now (not on the first request), and keep checking the bridge and auth.json at least once a
* minute (every 30 s, so a look never falls a few milliseconds short of FILE_CHECK_MS and waits a second minute).
*/
export function ensureAuthStore(): AuthStoreMode {
const mode = authStoreMode();
if (!globalThis.__fuobeAuthStoreTimer) {
globalThis.__fuobeAuthStoreTimer = setInterval(() => {
try {
authStoreMode();
} catch {}
}, FILE_CHECK_MS / 2);
globalThis.__fuobeAuthStoreTimer.unref?.();
}
return mode;
}