The runtime app-registration writer (POST /v1/apps) was deleted in an earlier pass because it minted a client_secret nobody verified. This pass kept resolveAppProfile's registered-app fallback and listAppProfiles on the theory that AppSlug is an open string and dynamic registration was a live extension point. Soul falsified that: every HTTP route enumerates the four built-in slugs in its own JSON schema (app.ts's `enum: [...appSlugs]` at every appSlug param/body field), so a registered row could never be resolved from a request. With the writer already gone, this was unreachable code behind a dead writer -- delete it rather than keep authenticating a door nobody can open. Deleted: src/app-registry.ts entirely (resolveAppProfile, listAppProfiles, isBuiltInApp, registeredAppToProfile, returnOriginsFromRedirectUris); StoredRegisteredApp and the findRegisteredApp/listRegisteredApps HeimdallStore methods (store/types.ts); their InMemoryStore and PostgresStore implementations, including seedRegisteredApp and mapRegisteredApp; the registered_apps table (store/schema.ts now drops it on any store built before this schema); tests/app-registry.test.ts. Every call site (app.ts x5, claims.ts, private-command-plane.ts) now calls the existing built-in-only getAppProfile (app-profiles.ts) directly -- sync, no store needed. If dynamic app registration is wanted later it returns as a designed, authenticated feature, not by resurrecting this vestige. Net -305 lines against the prior commit. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
181 lines
5.4 KiB
TypeScript
181 lines
5.4 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import {
|
|
type AccessClaimPayload,
|
|
type AppSlug,
|
|
type LinkedIdentityInput,
|
|
type RefreshTokenPayload,
|
|
} from "./contracts.js";
|
|
import { evaluateSharedCapabilities, getAppProfile, type AppProfile } from "./app-profiles.js";
|
|
import { type HeimdallConfig } from "./config.js";
|
|
import { identityFacts } from "./facts.js";
|
|
import { signJwt, type RuntimeKeyMaterial } from "./signing.js";
|
|
import { type CreateSessionInput, type HeimdallStore } from "./store/types.js";
|
|
|
|
function nowEpochSeconds(): number {
|
|
return Math.floor(Date.now() / 1000);
|
|
}
|
|
|
|
function clampTtlSeconds(value: number, fallback: number): number {
|
|
if (!Number.isFinite(value)) {
|
|
return fallback;
|
|
}
|
|
|
|
return Math.max(60, Math.min(86_400, Math.trunc(value)));
|
|
}
|
|
|
|
function normalizeFacts(accountId: string, linkedIdentities: LinkedIdentityInput[], facts: string[]): string[] {
|
|
const normalized = new Set(facts);
|
|
|
|
if (accountId.trim() || linkedIdentities.length) {
|
|
normalized.add(identityFacts.authenticated);
|
|
}
|
|
|
|
return [...normalized].sort();
|
|
}
|
|
|
|
export interface IssueAccessClaimInput {
|
|
appSlug: AppSlug;
|
|
accountId: string;
|
|
displayName?: string;
|
|
linkedIdentities: LinkedIdentityInput[];
|
|
facts: string[];
|
|
sessionId?: string;
|
|
accessRevision?: number;
|
|
ttlSeconds?: number;
|
|
}
|
|
|
|
export interface IssuedAccessClaimResult {
|
|
session: {
|
|
accountId: string;
|
|
sessionId: string;
|
|
appSlug: AppSlug;
|
|
accessRevision: number;
|
|
expiresAt: string;
|
|
};
|
|
accessToken: string;
|
|
refreshToken: string;
|
|
refresh: {
|
|
expiresAt: string;
|
|
};
|
|
claimSet: AccessClaimPayload;
|
|
verification: {
|
|
issuer: string;
|
|
jwksUri: string;
|
|
alg: string;
|
|
kid: string;
|
|
};
|
|
sharedCapabilities: string[];
|
|
hybridCapabilities: AppProfile["capabilities"];
|
|
}
|
|
|
|
export async function issueAccessClaim(options: {
|
|
config: HeimdallConfig;
|
|
keys: RuntimeKeyMaterial;
|
|
store: HeimdallStore;
|
|
input: IssueAccessClaimInput;
|
|
}): Promise<IssuedAccessClaimResult> {
|
|
const profile = getAppProfile(options.input.appSlug);
|
|
if (!profile) {
|
|
// An unregistered audience must not receive a signed claim: the token
|
|
// would name an app no verifier can resolve a profile for.
|
|
throw new Error(`Cannot issue an access claim for unknown app '${options.input.appSlug}'.`);
|
|
}
|
|
const facts = normalizeFacts(options.input.accountId, options.input.linkedIdentities, options.input.facts);
|
|
const factSet = new Set(facts);
|
|
const sharedCapabilities = evaluateSharedCapabilities(profile, {
|
|
accountId: options.input.accountId,
|
|
facts: factSet,
|
|
identities: options.input.linkedIdentities,
|
|
});
|
|
|
|
const issuedAt = nowEpochSeconds();
|
|
const ttlSeconds = clampTtlSeconds(
|
|
options.input.ttlSeconds ?? options.config.sessionTtlSeconds,
|
|
options.config.sessionTtlSeconds
|
|
);
|
|
const expiresAtEpoch = issuedAt + ttlSeconds;
|
|
const refreshTtlSeconds = Math.max(ttlSeconds, Math.trunc(options.config.refreshTtlSeconds));
|
|
const refreshExpiresAtEpoch = issuedAt + refreshTtlSeconds;
|
|
const sessionId = options.input.sessionId ?? randomUUID();
|
|
const accessRevision = options.input.accessRevision ?? 1;
|
|
|
|
const claimSet: AccessClaimPayload = {
|
|
iss: options.config.issuer,
|
|
aud: profile.slug,
|
|
sub: options.input.accountId,
|
|
sid: sessionId,
|
|
jti: randomUUID(),
|
|
iat: issuedAt,
|
|
nbf: issuedAt,
|
|
exp: expiresAtEpoch,
|
|
typ: "heimdall_access",
|
|
account_id: options.input.accountId,
|
|
access_revision: accessRevision,
|
|
app: {
|
|
slug: profile.slug,
|
|
profile_version: profile.profileVersion,
|
|
},
|
|
facts,
|
|
capabilities: sharedCapabilities,
|
|
identities: options.input.linkedIdentities,
|
|
};
|
|
|
|
if (options.input.displayName) {
|
|
claimSet.display_name = options.input.displayName;
|
|
}
|
|
|
|
const accessToken = signJwt(claimSet as unknown as Record<string, unknown>, options.keys);
|
|
const refreshClaimSet: RefreshTokenPayload = {
|
|
iss: options.config.issuer,
|
|
aud: profile.slug,
|
|
sub: options.input.accountId,
|
|
sid: sessionId,
|
|
jti: randomUUID(),
|
|
iat: issuedAt,
|
|
nbf: issuedAt,
|
|
exp: refreshExpiresAtEpoch,
|
|
typ: "heimdall_refresh",
|
|
account_id: options.input.accountId,
|
|
access_revision: accessRevision,
|
|
app: {
|
|
slug: profile.slug,
|
|
profile_version: profile.profileVersion,
|
|
},
|
|
};
|
|
const refreshToken = signJwt(refreshClaimSet as unknown as Record<string, unknown>, options.keys);
|
|
const sessionRecord: CreateSessionInput = {
|
|
id: sessionId,
|
|
accountId: options.input.accountId,
|
|
appSlug: profile.slug,
|
|
createdAt: new Date(issuedAt * 1000).toISOString(),
|
|
lastSeenAt: new Date(issuedAt * 1000).toISOString(),
|
|
expiresAt: new Date(refreshExpiresAtEpoch * 1000).toISOString(),
|
|
accessRevision,
|
|
claimsJson: claimSet as unknown as Record<string, unknown>,
|
|
};
|
|
await options.store.createSession(sessionRecord);
|
|
|
|
return {
|
|
session: {
|
|
accountId: options.input.accountId,
|
|
sessionId,
|
|
appSlug: profile.slug,
|
|
accessRevision,
|
|
expiresAt: sessionRecord.expiresAt,
|
|
},
|
|
accessToken,
|
|
refreshToken,
|
|
refresh: {
|
|
expiresAt: sessionRecord.expiresAt,
|
|
},
|
|
claimSet,
|
|
verification: {
|
|
issuer: options.config.issuer,
|
|
jwksUri: `${options.config.publicBaseUrl}/.well-known/jwks.json`,
|
|
alg: options.keys.alg,
|
|
kid: options.keys.kid,
|
|
},
|
|
sharedCapabilities,
|
|
hybridCapabilities: profile.capabilities.filter((capability) => capability.mode === "hybrid"),
|
|
};
|
|
}
|