mirror of
https://github.com/bitbaum/morulia.git
synced 2026-12-04 05:48:23 +01:00
* feat: the shared bitbaum sign-in (OIDC provider, pairwise subjects) One OIDC provider holding the Google, Apple, GitHub, X and Meta credentials once, for every accountkit app (fleet STACK.md, "Identity"): - better-auth 1.7 + @better-auth/oauth-provider + jwt (RS256); discovery at the origin, authorize with PKCE S256 required, token with client_secret_post, userinfo, JWKS - pairwise subject per app (HMAC of redirect host + user); the email scope is answered with a per-app .invalid placeholder - upstream providers each on only when all their env is set, asked for no email and no profile; provider tokens are never stored - consent-free first-party clients from a registry (lib/clients.ts), synced at start from per-app secrets in env - one pairwise-subject derivation (lib/subject.ts), pinned to a literal, with a persona input for future non-root lives only - tests: PKCE flow end to end against fake upstreams, pairwise sub differs per app and is stable per app, two providers reporting one verified address stay two people, and accountkit's real runtime signs in with SIGNIN_* env alone Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9 * build: standalone output for the self-host deploy loki deploy.sh ships .next/standalone and stops without it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9 * ci: browser smoke on the production build The fleet rollout gate holds any consumer of a shared package (accountkit, design-tokens, limitkit) until ci.yml and browser-smoke.yml pass on the commit. This one starts the production build against PostgreSQL built from drizzle/ and drives Chromium through a real authorize request from Solon: health with the database up, discovery for the given issuer, and the app's sign-in page at 390px with no sideways scroll and no console errors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9 --------- Co-authored-by: Claude <noreply@anthropic.com>
63 lines
2.7 KiB
TypeScript
63 lines
2.7 KiB
TypeScript
import { createHmac } from "node:crypto";
|
|
|
|
/**
|
|
* The pairwise subject an app sees for a person: the ONE statement of how it
|
|
* is derived (OIDC Core 8.1).
|
|
*
|
|
* root: base64( HMAC-SHA256( SIGNIN_PAIRWISE_SECRET, "<sector>.<userId>" ) )
|
|
* persona: base64( HMAC-SHA256( SIGNIN_PAIRWISE_SECRET, "<sector>.<userId>|persona|<personaId>" ) )
|
|
*
|
|
* `sector` is the host of the app's redirect URI. The root form is exactly
|
|
* what @better-auth/oauth-provider issues today (its computePairwiseSub); the
|
|
* flow tests prove the two agree on every token, and test/subject.test.ts pins
|
|
* it to a literal, so nothing can change an existing person's identifier in
|
|
* an existing app without failing CI.
|
|
*
|
|
* Personas (future, not built): a person may one day hold several "lives"
|
|
* under one root, each with its own subject per app. The root life is the
|
|
* account itself — `persona` undefined, or equal to `userId` — and keeps
|
|
* today's value byte for byte. Only a non-root persona adds its id as a
|
|
* further HMAC input. `|` cannot occur in a host or in a better-auth id, so a
|
|
* persona subject can never equal any root subject's input.
|
|
*
|
|
* A life's id lives in its own id space: `life_<uuid>`. better-auth user ids
|
|
* are alphanumeric and never carry that prefix, so a life id can never equal a
|
|
* user id and silently fall back to the root subject. Anything else that is
|
|
* not this user's own id is refused, never hashed.
|
|
*/
|
|
export const LIFE_PREFIX = "life_";
|
|
const LIFE_ID = /^life_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
|
|
export function isLifeId(value: string): boolean {
|
|
return LIFE_ID.test(value);
|
|
}
|
|
export interface SubjectInput {
|
|
secret: string;
|
|
/** The app's redirect host, e.g. "heidi.orangecat.ch". */
|
|
sector: string;
|
|
userId: string;
|
|
/** A life under this account: `life_<uuid>`. Undefined or `userId` = the root. */
|
|
persona?: string;
|
|
}
|
|
|
|
export function isRootPersona(userId: string, persona: string | undefined): boolean {
|
|
return persona === undefined || persona === userId;
|
|
}
|
|
|
|
export function pairwiseSub({ secret, sector, userId, persona }: SubjectInput): string {
|
|
if (!secret || !sector || !userId) throw new Error("pairwiseSub: secret, sector and userId are required");
|
|
const root = `${sector}.${userId}`;
|
|
let input = root;
|
|
if (!isRootPersona(userId, persona)) {
|
|
if (!persona || !isLifeId(persona))
|
|
throw new Error(`pairwiseSub: a persona must be undefined, the user id, or ${LIFE_PREFIX}<uuid>`);
|
|
input = `${root}|persona|${persona}`;
|
|
}
|
|
return createHmac("sha256", secret).update(input).digest("base64");
|
|
}
|
|
|
|
/** The sector of a client: the host of its first redirect URI (as the plugin computes it). */
|
|
export function sectorOf(redirectUri: string): string {
|
|
return new URL(redirectUri).host;
|
|
}
|