Heimdall/src/contracts.ts

171 lines
4.3 KiB
TypeScript
Raw Normal View History

export const providers = ["discord", "patreon", "github", "twitch", "youtube", "spotify"] as const;
2026-04-26 12:25:11 +01:00
export type Provider = (typeof providers)[number];
Register apps at runtime instead of at build time App slugs were a compile-time union of four names. That was fine while every app was ours and it blocks what Heimdall is for: an app it has never heard of, running on someone else's machine, registering against a self-hosted instance without forking and rebuilding the auth authority. A creator self-hosting Erycina alongside Heimdall would otherwise maintain a patched auth server forever. AppSlug is now a string, which it always was on the wire; the union was a narrowing the type system could enforce only because the set was closed. Membership moved from appSlugs.includes to the registry, and where no store is reachable — the offline token verifier is the case that matters — it is a shape check, isAppSlug, rather than a false claim to know what exists. A host app verifying its own tokens should compare aud against its own slug, which it knows and Heimdall's built-in list does not. Registration is data end to end. Storage carries the profile and a hash of the client secret rather than the secret. Built-in profiles resolve first and their slugs are refused at registration, in both places, so a registration cannot shadow a shipped app and inherit its audience. POST /v1/apps answers in the RFC 7591 shape so an ordinary OAuth client library can read it, returning client_secret exactly once. It is gated on an operator secret and closed entirely until one is configured: an open registration endpoint on an OAuth provider lets anyone create a client that starts Discord and Patreon flows under the instance's identity, which is a phishing surface wearing the operator's name. A fresh instance is not accidentally open. Redirect URIs are validated rather than stored as given. They are where an authorization code is delivered, so a permissive one is a token-theft primitive: absolute, no fragment, https unless loopback. Six call sites assumed an app profile always exists. They now answer 404 for an app nobody registered, and claim issuance refuses outright rather than signing a token whose audience no verifier can resolve. 108 tests pass, 34 of them new. Zero type errors. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 00:11:41 +02:00
/** Apps shipped with Heimdall. Seed data, not the set of permissible apps. */
Retire the Spotiverse profile; keep the spotify provider intact for restore Spotiverse was lost with the E: drive and has no repository in the swarm, but /v1/oauth/spotify/start stayed publicly reachable and GC_ACCESS_PROVIDER_SPOTIFY_CLIENT_ID/_SECRET stayed configured on yggdrasil. That path would have run a real OAuth handshake against a real secret and taken custody of a user's Spotify refresh token for a consumer that could not use it. The seam is supportsProvider(profile, provider): a provider with no app behind it is unreachable. So the registry entry is what had to go, not the provider. Removed: spotiverseProfile and its registry entry, the spotiverse slug from appSlugs, the hardcoded callback arm in app.ts, and three spotiverse-specific tests. Removed from the host: the two spotify env vars, backed up to /root/heimdall-service.env.bak-20260906-spotify. Nine other provider vars and the file's 0600 heimdall:heimdall ownership are untouched. Kept deliberately: the entire spotify provider -- the providers.ts catalog entry, spotifyRuntime and its token/profile exchange in oauth.ts, and "spotify" in the providers union. It is intact and unreferenced, which is what makes restoring a registry change rather than protocol work. The capability is wanted back; a Persona managing the queue during a stream is the motivating case. docs/app-profiles/spotiverse.md is now the restore spec rather than an archive: what was removed, what was kept, the base commit 5a15f71 to recover the profile verbatim, and the daemon's callback URL (http://127.0.0.1:8796/auth/heimdall/callback), which existed only in a hardcoded allowlist arm and would otherwise have been lost with it. Before deleting the three tests I checked their routes keep coverage: managed-credential resolve is exercised twice via streampixels, and backend_callback handoff at three other sites. The generic untrusted_backend_callback negative test was retargeted to bifrost rather than deleted. 35 pass, 9 files; tsc clean. Verified the deployed build still boots without the vars -- readProviderConfig returns an empty config rather than throwing, so the running release fails that route at request time instead of at startup. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 01:47:10 +02:00
export const appSlugs = ["repixelizer", "streampixels", "bifrost", "ghostlight"] as const;
2026-04-26 12:25:11 +01:00
Register apps at runtime instead of at build time App slugs were a compile-time union of four names. That was fine while every app was ours and it blocks what Heimdall is for: an app it has never heard of, running on someone else's machine, registering against a self-hosted instance without forking and rebuilding the auth authority. A creator self-hosting Erycina alongside Heimdall would otherwise maintain a patched auth server forever. AppSlug is now a string, which it always was on the wire; the union was a narrowing the type system could enforce only because the set was closed. Membership moved from appSlugs.includes to the registry, and where no store is reachable — the offline token verifier is the case that matters — it is a shape check, isAppSlug, rather than a false claim to know what exists. A host app verifying its own tokens should compare aud against its own slug, which it knows and Heimdall's built-in list does not. Registration is data end to end. Storage carries the profile and a hash of the client secret rather than the secret. Built-in profiles resolve first and their slugs are refused at registration, in both places, so a registration cannot shadow a shipped app and inherit its audience. POST /v1/apps answers in the RFC 7591 shape so an ordinary OAuth client library can read it, returning client_secret exactly once. It is gated on an operator secret and closed entirely until one is configured: an open registration endpoint on an OAuth provider lets anyone create a client that starts Discord and Patreon flows under the instance's identity, which is a phishing surface wearing the operator's name. A fresh instance is not accidentally open. Redirect URIs are validated rather than stored as given. They are where an authorization code is delivered, so a permissive one is a token-theft primitive: absolute, no fragment, https unless loopback. Six call sites assumed an app profile always exists. They now answer 404 for an app nobody registered, and claim issuance refuses outright rather than signing a token whose audience no verifier can resolve. 108 tests pass, 34 of them new. Zero type errors. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 00:11:41 +02:00
export type BuiltInAppSlug = (typeof appSlugs)[number];
/**
* An app identifier. Always a string on the wire; it was narrowed to a union
* only because every app was known at build time. Validity is decided by the
* app registry, not the type system.
*/
export type AppSlug = string;
/**
* Shape check only. Whether an app exists is a registry question, and callers
* without a store — the offline token verifier, for one — can only check that
* an identifier is well formed. A host app verifying its own tokens should
* compare aud against its own slug rather than trusting a shape.
*/
export function isAppSlug(value: unknown): value is AppSlug {
return typeof value === "string" && /^[a-z][a-z0-9_-]{1,62}$/.test(value);
}
2026-04-26 12:25:11 +01:00
export const oauthModes = ["sign_in", "link", "connect"] as const;
export type OAuthMode = (typeof oauthModes)[number];
export const connectionKinds = ["account", "creator", "workspace", "project", "channel"] as const;
export type ConnectionKind = (typeof connectionKinds)[number];
2026-04-26 17:00:57 +01:00
export const oauthHandoffKinds = ["browser_completion", "backend_callback"] as const;
export type OAuthHandoffKind = (typeof oauthHandoffKinds)[number];
2026-04-26 12:25:11 +01:00
export interface OAuthConnectionBinding {
kind: ConnectionKind;
targetId: string;
summary?: string;
}
2026-04-26 17:00:57 +01:00
export interface OAuthBrowserCompletionHandoff {
kind: "browser_completion";
attemptId?: string;
2026-04-26 17:00:57 +01:00
}
export interface OAuthBackendCallbackHandoff {
kind: "backend_callback";
attemptId: string;
callbackUrl: string;
}
export type OAuthHandoff = OAuthBrowserCompletionHandoff | OAuthBackendCallbackHandoff;
2026-04-29 13:22:01 +01:00
export interface DiscordRoleEntitlementPolicy {
kind: "discord_role_access";
guildId: string;
allowedRoleIds: string[];
}
2026-04-29 14:36:03 +01:00
export interface PatreonMembershipEntitlementPolicy {
kind: "patreon_membership_access";
requiredTierTitle: string;
}
export type OAuthEntitlementPolicy = DiscordRoleEntitlementPolicy | PatreonMembershipEntitlementPolicy;
2026-04-29 13:22:01 +01:00
2026-04-26 12:25:11 +01:00
export interface OAuthStartRequest {
appSlug: AppSlug;
mode: OAuthMode;
returnTo: string;
connection?: OAuthConnectionBinding;
2026-04-26 17:00:57 +01:00
handoff?: OAuthHandoff;
2026-04-26 12:25:11 +01:00
requestedScopes?: string[];
2026-04-29 13:22:01 +01:00
entitlementPolicy?: OAuthEntitlementPolicy;
2026-04-26 12:25:11 +01:00
}
2026-04-26 12:50:07 +01:00
export interface OAuthStatePayload {
iss: string;
sub: AppSlug;
aud: Provider;
jti: string;
iat: number;
nbf: number;
exp: number;
typ: "heimdall_oauth_state";
provider: Provider;
app_slug: AppSlug;
mode: OAuthMode;
return_to: string;
connection: OAuthConnectionBinding | null;
2026-04-26 17:00:57 +01:00
handoff: OAuthHandoff | null;
2026-04-29 13:22:01 +01:00
entitlement_policy: OAuthEntitlementPolicy | null;
2026-04-26 12:50:07 +01:00
}
2026-04-26 12:25:11 +01:00
export interface LinkedIdentityInput {
provider: Provider;
providerUserId: string;
username?: string;
displayName?: string;
}
export interface RedeemAuthCompletionRequest {
completionCode: string;
}
export type HeimdallAuthAttemptStatus = "pending" | "completed" | "denied" | "consumed" | "expired";
export interface HeimdallAuthAttempt {
schema: "heimdall.auth_attempt.v1";
handle: string;
appSlug: AppSlug;
provider: Provider;
mode: OAuthMode;
returnTo: string;
status: HeimdallAuthAttemptStatus;
createdAt: string;
expiresAt: string;
completedAt?: string;
consumedAt?: string;
denialCode?: string;
}
export interface RefreshSessionRequest {
refreshToken: string;
entitlementPolicy?: OAuthEntitlementPolicy;
entitlementPolicies?: OAuthEntitlementPolicy[];
}
2026-04-26 12:25:11 +01:00
export interface AccessClaimPayload {
iss: string;
aud: AppSlug;
sub: string;
sid: string;
jti: string;
iat: number;
nbf: number;
exp: number;
typ: "heimdall_access";
account_id: string;
access_revision: number;
display_name?: string;
app: {
slug: AppSlug;
profile_version: string;
};
facts: string[];
capabilities: string[];
identities: LinkedIdentityInput[];
}
export interface RefreshTokenPayload {
iss: string;
aud: AppSlug;
sub: string;
sid: string;
jti: string;
iat: number;
nbf: number;
exp: number;
typ: "heimdall_refresh";
account_id: string;
access_revision: number;
app: {
slug: AppSlug;
profile_version: string;
};
}