Every app profile carried an evaluateSharedCapabilities function beside a sharedRule string describing what that function did. Two representations of one rule, free to drift, and the reason a profile could never arrive at runtime: you cannot accept a function from a stranger and run it inside the auth authority. The rules were never more than a disjunction. Every sharedRule across all four built-in profiles is "a || b || c" over a closed vocabulary, and one of them is a bare reference to another capability. So the executable form is a list, not an expression language, and anyOf is now the only representation. The readable form is derived from it rather than stored beside it. Evaluation runs to a fixed point because capabilities may reference each other: repixelizer's queue_submit derives from app_access, and a single pass grants it only when the rules happen to be declared in dependency order. It terminates because the granted set only grows and is bounded by the rule count. validateCapabilityRules is the gate that makes runtime registration acceptable. A term must name a fact Heimdall itself produces or another capability in the same profile. A registered app selects from what Heimdall has already decided is true about an account; it cannot invent a fact to grant itself something, and it cannot reach into another app's capabilities. An unknown term is refused rather than treated as false, because a rule that silently never fires becomes a support ticket instead of an error. The four built-in profiles are now seed data rather than special cases: a registered app produces the same shape and runs through the same evaluator. Behaviour is unchanged. 74 tests pass, 18 of them new, covering derived capabilities in both declaration orders, hybrid capabilities never being granted from shared rules, and every rejection path in the validator. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
201 lines
6.1 KiB
TypeScript
201 lines
6.1 KiB
TypeScript
import {
|
|
evaluateCapabilityRules,
|
|
type CapabilityDefinition,
|
|
type CapabilityMode,
|
|
} from "./capability-rules.js";
|
|
import { type AppSlug, type LinkedIdentityInput, type Provider } from "./contracts.js";
|
|
import { entitlementFacts, grantFacts, identityFacts } from "./facts.js";
|
|
|
|
export type { CapabilityDefinition, CapabilityMode };
|
|
|
|
export interface ClaimEvaluationContext {
|
|
accountId: string;
|
|
facts: Set<string>;
|
|
identities: LinkedIdentityInput[];
|
|
}
|
|
|
|
/**
|
|
* An app's auth profile. Entirely data.
|
|
*
|
|
* It used to carry an evaluateSharedCapabilities function beside a sharedRule
|
|
* string that described what the function did — two representations of one rule,
|
|
* free to drift, and the reason a profile could not arrive at runtime. Every
|
|
* rule was a disjunction over a closed vocabulary, so anyOf now carries it and
|
|
* evaluation is a free function over the data.
|
|
*/
|
|
export interface AppProfile {
|
|
slug: AppSlug;
|
|
displayName: string;
|
|
profileVersion: string;
|
|
identityProviders: Provider[];
|
|
entitlementSources: Provider[];
|
|
managedConnectionProviders: Provider[];
|
|
capabilities: CapabilityDefinition[];
|
|
}
|
|
|
|
/** The membership signal shared by every app that gates on GameCult membership. */
|
|
const gameCultMembership = [
|
|
entitlementFacts.appAccess,
|
|
grantFacts.globalMember,
|
|
grantFacts.appAccess,
|
|
];
|
|
|
|
const repixelizerProfile: AppProfile = {
|
|
slug: "repixelizer",
|
|
displayName: "Repixelizer",
|
|
profileVersion: "2026-04-26.1",
|
|
identityProviders: ["discord", "patreon"],
|
|
entitlementSources: ["discord", "patreon"],
|
|
managedConnectionProviders: [],
|
|
capabilities: [
|
|
{
|
|
key: "app_access",
|
|
mode: "shared",
|
|
summary: "May load the protected hosted GUI.",
|
|
anyOf: gameCultMembership,
|
|
},
|
|
{
|
|
key: "queue_submit",
|
|
mode: "shared",
|
|
summary: "May create a repixelizer job.",
|
|
anyOf: ["app_access"],
|
|
},
|
|
{
|
|
key: "job_read_own",
|
|
mode: "hybrid",
|
|
summary: "May read own job state, event stream, and final output.",
|
|
localRequirement: "Host app must combine app_access with job ownership.",
|
|
},
|
|
{
|
|
key: "job_cancel_own",
|
|
mode: "hybrid",
|
|
summary: "May cancel own queued or running job.",
|
|
localRequirement: "Host app must combine app_access with job ownership.",
|
|
},
|
|
{
|
|
key: "admin_access",
|
|
mode: "shared",
|
|
summary: "May inspect grant/admin surfaces.",
|
|
anyOf: [grantFacts.operator, grantFacts.adminAccess],
|
|
},
|
|
],
|
|
};
|
|
|
|
const streampixelsProfile: AppProfile = {
|
|
slug: "streampixels",
|
|
displayName: "StreamPixels",
|
|
profileVersion: "2026-04-26",
|
|
identityProviders: ["twitch", "youtube"],
|
|
entitlementSources: [],
|
|
managedConnectionProviders: ["twitch", "youtube"],
|
|
capabilities: [
|
|
{
|
|
key: "viewer_access",
|
|
mode: "shared",
|
|
summary: "Authenticated viewer session for control-plane surfaces.",
|
|
anyOf: [identityFacts.authenticated],
|
|
},
|
|
{
|
|
key: "creator_access",
|
|
mode: "hybrid",
|
|
summary: "Creator-scoped route access.",
|
|
localRequirement:
|
|
"Host app must combine authenticated session claims with local creator membership.",
|
|
},
|
|
{
|
|
key: "creator_admin",
|
|
mode: "hybrid",
|
|
summary: "Creator-scoped write/admin access.",
|
|
localRequirement:
|
|
"Host app must combine authenticated session claims with local creator admin membership.",
|
|
},
|
|
{
|
|
key: "operator_access",
|
|
mode: "shared",
|
|
summary: "Global operator access.",
|
|
anyOf: [grantFacts.operator],
|
|
},
|
|
],
|
|
};
|
|
|
|
const bifrostProfile: AppProfile = {
|
|
slug: "bifrost",
|
|
displayName: "Bifrost",
|
|
profileVersion: "2026-06-08",
|
|
identityProviders: ["discord", "patreon"],
|
|
entitlementSources: ["discord", "patreon"],
|
|
managedConnectionProviders: [],
|
|
capabilities: [
|
|
{
|
|
key: "member_access",
|
|
mode: "shared",
|
|
summary: "May enter the Bifrost member alpha through a Heimdall-verified GameCult membership signal.",
|
|
anyOf: gameCultMembership,
|
|
},
|
|
],
|
|
};
|
|
|
|
const ghostlightProfile: AppProfile = {
|
|
slug: "ghostlight",
|
|
displayName: "Ghostlight Dungeon",
|
|
profileVersion: "2026-08-22",
|
|
identityProviders: ["discord"],
|
|
entitlementSources: ["discord"],
|
|
managedConnectionProviders: [],
|
|
capabilities: [
|
|
{
|
|
key: "app_access",
|
|
mode: "shared",
|
|
summary: "May enter Ghostlight Dungeon after Heimdall verifies the app-supplied GameCult Discord role policy.",
|
|
anyOf: gameCultMembership,
|
|
},
|
|
{
|
|
key: "campaign_play",
|
|
mode: "hybrid",
|
|
summary: "May play campaigns owned by the authenticated Ghostlight account.",
|
|
localRequirement: "Ghostlight must combine app_access with local campaign ownership.",
|
|
},
|
|
],
|
|
};
|
|
|
|
/**
|
|
* Profiles that ship with Heimdall. These are seed data, not special cases:
|
|
* a registered app produces the same shape and is evaluated by the same code.
|
|
*/
|
|
export const builtInAppProfiles: Record<AppSlug, AppProfile> = {
|
|
repixelizer: repixelizerProfile,
|
|
streampixels: streampixelsProfile,
|
|
bifrost: bifrostProfile,
|
|
ghostlight: ghostlightProfile,
|
|
};
|
|
|
|
export const appProfiles = builtInAppProfiles;
|
|
|
|
export function getAppProfile(appSlug: AppSlug): AppProfile {
|
|
return appProfiles[appSlug];
|
|
}
|
|
|
|
/** Shared capabilities the account holds for this app, from the profile's rules. */
|
|
export function evaluateSharedCapabilities(profile: AppProfile, context: ClaimEvaluationContext): string[] {
|
|
return evaluateCapabilityRules(profile.capabilities, context.facts);
|
|
}
|
|
|
|
export function serializeAppProfile(profile: AppProfile): Record<string, unknown> {
|
|
return {
|
|
slug: profile.slug,
|
|
displayName: profile.displayName,
|
|
profileVersion: profile.profileVersion,
|
|
identityProviders: profile.identityProviders,
|
|
entitlementSources: profile.entitlementSources,
|
|
managedConnectionProviders: profile.managedConnectionProviders,
|
|
capabilities: profile.capabilities,
|
|
};
|
|
}
|
|
|
|
export function supportsProvider(profile: AppProfile, provider: Provider): boolean {
|
|
return (
|
|
profile.identityProviders.includes(provider) ||
|
|
profile.managedConnectionProviders.includes(provider) ||
|
|
profile.entitlementSources.includes(provider)
|
|
);
|
|
}
|