mirror of
https://github.com/bitbaum/morulia.git
synced 2026-12-04 06:29:29 +01:00
Redirect URIs are stored primary-first, then the extras sorted. The sector still comes from the first redirect URI, i.e. the primary origin. A host may belong to one app only, primary or extra. Co-authored-by: Claude <noreply@anthropic.com>
169 lines
9.0 KiB
TypeScript
169 lines
9.0 KiB
TypeScript
import { SIGNIN_PROVIDER_ID } from "@bitbaum/accountkit/auth";
|
|
|
|
/**
|
|
* The apps that may use this service. Every one is a first-party,
|
|
* confidential OIDC client: no consent screen (the person chose the button in
|
|
* the app), client_secret_post, PKCE required, a pairwise subject.
|
|
*
|
|
* An app has ONE primary origin and may list extra ones (`alsoOrigins`, e.g.
|
|
* during a move to a new domain). The pairwise subject is derived from the
|
|
* host of the FIRST redirect URI (OIDC Core 8.1), which is always the primary
|
|
* origin's, so the sector never depends on the extras: "the same person in
|
|
* the same app" stays stable and "the same person in two apps" unlinkable.
|
|
* A host may belong to only one app, primary or extra.
|
|
*
|
|
* The redirect URI is the one accountkit's generic OIDC provider sends:
|
|
* `<origin>/api/auth/callback/bitbaum-signin`. The `id` is the app's
|
|
* SIGNIN_CLIENT_ID. Its secret is written on the box by loki
|
|
* `app-secrets.sh` into both env files (SIGNIN_CLIENT_SECRET_<ID> here,
|
|
* SIGNIN_CLIENT_SECRET there) and is never printed; lib/sync-clients.ts
|
|
* stores its hash at start. A row without a secret yet is stored switched off.
|
|
*
|
|
* Hosts come from bitbaum/loki scripts/hetzner/apps.conf (the serve register):
|
|
* each app's address, which for vitareba is vita.orangecat.ch. The list is
|
|
* every fleet app with a sign-in page (fleet docs/auth-parity-2026-11-01.md),
|
|
* whatever its runtime, so the client exists before the app wires the button.
|
|
* Adding an app is one row here and one pair row in loki app-secrets.sh.
|
|
*/
|
|
export interface ClientApp {
|
|
/** SIGNIN_CLIENT_ID in the app's env; the apps.conf key or repo slug. */
|
|
id: string;
|
|
/** Shown on the sign-in page: "Continue to <name>". */
|
|
name: string;
|
|
/** The app's public origin, exactly as its BETTER_AUTH_URL / site.url. */
|
|
origin: string;
|
|
/**
|
|
* Extra origins of the same app (a move between domains). Each gets its own
|
|
* redirect URI, after the primary's; the sector still comes from `origin`.
|
|
* Same rules as `origin`; a host here may not be any other app's host.
|
|
*/
|
|
alsoOrigins?: string[];
|
|
/**
|
|
* The app's own icon, on its own origin, shown beside "Continue to <name>".
|
|
* Taken from the icon the app's live home page links (checked 2026-11-10).
|
|
* Absent → a letter tile in the neutral colours.
|
|
*/
|
|
logo?: string;
|
|
/**
|
|
* The app's accent, `#rrggbb`, from a colour the app itself ships (its icon
|
|
* or its design tokens; the source is noted on the row). Used only for the
|
|
* page's edge and the logo tile, never for text, so contrast is never at
|
|
* stake. Absent → the neutral page.
|
|
*/
|
|
accent?: string;
|
|
}
|
|
|
|
export const CLIENT_APPS: readonly ClientApp[] = [
|
|
// accent: the fill of its icon.svg
|
|
{ id: "aoz-begleitung", name: "AOZ Begleitung", origin: "https://aoz.orangecat.ch", logo: "https://aoz.orangecat.ch/icon.svg", accent: "#e63946" },
|
|
{ id: "biaslens", name: "BiasLens", origin: "https://biaslens.orangecat.ch", logo: "https://biaslens.orangecat.ch/icon" },
|
|
{ id: "botsmann", name: "Botsmann", origin: "https://botsmann.orangecat.ch", logo: "https://botsmann.orangecat.ch/icon.svg" },
|
|
// accent: the fill of its icon.svg
|
|
{ id: "causius", name: "Causius", origin: "https://causius.orangecat.ch", logo: "https://causius.orangecat.ch/icon.svg", accent: "#ff5c00" },
|
|
{ id: "datacat", name: "DataCat", origin: "https://datacat.orangecat.ch", logo: "https://datacat.orangecat.ch/icon.svg" },
|
|
// accent: the fill of its icon.svg
|
|
{ id: "diplodoctor", name: "Diplodoctor", origin: "https://diplodoctor.orangecat.ch", logo: "https://diplodoctor.orangecat.ch/icon.svg", accent: "#ff5c00" },
|
|
{ id: "evig", name: "evig", origin: "https://evig.orangecat.ch", logo: "https://evig.orangecat.ch/apple-icon.png" },
|
|
// Moving to fragheidi.ch; the old address stays registered during the move.
|
|
{ id: "heidi", name: "Heidi", origin: "https://fragheidi.ch", alsoOrigins: ["https://heidi.orangecat.ch"], logo: "https://fragheidi.ch/icon.svg" },
|
|
{ id: "hirnli", name: "hirnli", origin: "https://hirnli.orangecat.ch", logo: "https://hirnli.orangecat.ch/favicon.ico" },
|
|
{ id: "keller", name: "Keller", origin: "https://keller.orangecat.ch" },
|
|
// accent: the stroke of its icon.svg
|
|
{ id: "kivvi", name: "Kivvi", origin: "https://kivvi.orangecat.ch", logo: "https://kivvi.orangecat.ch/icon.svg", accent: "#4a8c2a" },
|
|
{ id: "loki", name: "Loki", origin: "https://loki.orangecat.ch", logo: "https://loki.orangecat.ch/icon.svg" },
|
|
// accent: lib/config/design-tokens.ts `accent`
|
|
{ id: "petvity", name: "Petvity", origin: "https://petvity.orangecat.ch", logo: "https://petvity.orangecat.ch/icon.svg", accent: "#b8502a" },
|
|
{ id: "printcraft", name: "PrintCraft", origin: "https://printcraft.orangecat.ch", logo: "https://printcraft.orangecat.ch/icon.svg" },
|
|
{ id: "reparaturbonus-zh", name: "Reparaturbonus Zürich", origin: "https://reparaturbonus.orangecat.ch", logo: "https://reparaturbonus.orangecat.ch/logo/favicon_48.png" },
|
|
// accent: src/app/globals.css `--accent`
|
|
{ id: "skif", name: "Skif", origin: "https://skif.orangecat.ch", logo: "https://skif.orangecat.ch/icon.svg", accent: "#2f5d50" },
|
|
{ id: "solon", name: "Solon", origin: "https://solon.orangecat.ch", logo: "https://solon.orangecat.ch/favicon.ico" },
|
|
// accent: the fill of its icon.svg
|
|
{ id: "substrata", name: "Substrata", origin: "https://substrata.orangecat.ch", logo: "https://substrata.orangecat.ch/icon.svg", accent: "#4f8a80" },
|
|
{ id: "surf-your-life", name: "Surf Your Life", origin: "https://surf-your-life.orangecat.ch", logo: "https://surf-your-life.orangecat.ch/icon.svg" },
|
|
// accent: the fill of its icon.svg
|
|
{ id: "vitareba", name: "Vita", origin: "https://vita.orangecat.ch", logo: "https://vita.orangecat.ch/icon.svg", accent: "#2a7a8a" },
|
|
];
|
|
|
|
export const CALLBACK_PATH = `/api/auth/callback/${SIGNIN_PROVIDER_ID}`;
|
|
|
|
export function redirectUri(app: Pick<ClientApp, "origin">): string {
|
|
return `${app.origin}${CALLBACK_PATH}`;
|
|
}
|
|
|
|
/** Every origin of an app: the primary first, then the extras sorted, without repeats. */
|
|
export function allOrigins(app: Pick<ClientApp, "origin" | "alsoOrigins">): string[] {
|
|
const extras = [...new Set(app.alsoOrigins ?? [])].filter((o) => o !== app.origin).sort();
|
|
return [app.origin, ...extras];
|
|
}
|
|
|
|
/** Every redirect URI of an app; the primary origin's is first (it sets the sector). */
|
|
export function redirectUris(app: Pick<ClientApp, "origin" | "alsoOrigins">): string[] {
|
|
return allOrigins(app).map((origin) => redirectUri({ origin }));
|
|
}
|
|
|
|
/** Whether `uri` is exactly one of the app's registered redirect URIs. */
|
|
export function isRegisteredRedirect(app: Pick<ClientApp, "origin" | "alsoOrigins">, uri: string): boolean {
|
|
return redirectUris(app).includes(uri);
|
|
}
|
|
|
|
/** What is wrong with a registry, as sentences; empty when it is sound. */
|
|
export function registryProblems(apps: readonly ClientApp[]): string[] {
|
|
const problems: string[] = [];
|
|
const ids = new Set<string>();
|
|
const hosts = new Map<string, string>();
|
|
for (const app of apps) {
|
|
if (!/^[a-z0-9][a-z0-9-]{1,62}$/.test(app.id))
|
|
problems.push(`${app.id}: id must be a lowercase slug`);
|
|
if (ids.has(app.id)) problems.push(`${app.id}: listed twice`);
|
|
ids.add(app.id);
|
|
let url: URL | null = null;
|
|
const claim = (label: string, origin: string): URL | null => {
|
|
let parsed: URL;
|
|
try {
|
|
parsed = new URL(origin);
|
|
} catch {
|
|
problems.push(`${app.id}: ${label} is not a URL`);
|
|
return null;
|
|
}
|
|
const local = parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1";
|
|
if (parsed.protocol !== "https:" && !local)
|
|
problems.push(`${app.id}: ${label} must be https`);
|
|
if (parsed.origin !== origin)
|
|
problems.push(`${app.id}: ${label} must have no path or trailing slash`);
|
|
const owner = hosts.get(parsed.host);
|
|
if (owner !== undefined && owner !== app.id)
|
|
problems.push(`${app.id}: ${parsed.host} is already another app's host`);
|
|
else if (owner === app.id)
|
|
problems.push(`${app.id}: ${parsed.host} is listed twice`);
|
|
hosts.set(parsed.host, app.id);
|
|
return parsed;
|
|
};
|
|
url = claim("origin", app.origin);
|
|
for (const extra of app.alsoOrigins ?? []) claim("alsoOrigins entry", extra);
|
|
if (!app.name.trim()) problems.push(`${app.id}: name is empty`);
|
|
if (app.logo !== undefined) {
|
|
let logo: URL | null = null;
|
|
try {
|
|
logo = new URL(app.logo);
|
|
} catch {
|
|
problems.push(`${app.id}: logo is not a URL`);
|
|
}
|
|
// The app's own icon only: a logo from anywhere else is a third party
|
|
// learning who is signing in to what.
|
|
if (logo && url && logo.origin !== url.origin)
|
|
problems.push(`${app.id}: logo must be on the app's own origin`);
|
|
}
|
|
if (app.accent !== undefined && !/^#[0-9a-f]{6}$/.test(app.accent))
|
|
problems.push(`${app.id}: accent must be #rrggbb, lowercase`);
|
|
}
|
|
return problems;
|
|
}
|
|
|
|
export function clientApp(
|
|
id: string | null | undefined,
|
|
apps: readonly ClientApp[] = CLIENT_APPS,
|
|
): ClientApp | null {
|
|
return apps.find((a) => a.id === id) ?? null;
|
|
}
|