/** * Delegation — macaroons-style capability tokens with caveats. * * A delegation is a signed token from issuer to subject granting a set of * capabilities under a set of caveats (time bounds, budget, audience, etc.). * Delegations are CHAINABLE: a holder can attenuate by adding more caveats, * but NEVER broaden scope. Chain verification checks that every link's * caveats monotonically narrow the previous. * * Delegations are bearer-adjacent: they name a subject DID, and the subject * must be able to sign a proof-of-possession to use them. Unlike classic * macaroons, we bind to a DID rather than pure HMAC chains, which is * cleaner in a world where every actor already has a keypair. * * Typical flow: * A has capability X * A delegates X to B with caveat "budget: 1000 credits" * B delegates X to C with additional caveat "expires: tomorrow" * C tries to use X → chain verified: signed by A → B → C, all caveats hold */ import { type CryptoProvider } from '../core/crypto-provider.js'; import { type DidMethodRegistry } from '../core/did-method.js'; import { type HistoricalKeyLookup } from './historical-key-lookup.js'; /** A caveat is a condition that must hold for the delegation to be valid. */ export type Caveat = { kind: 'expires-at'; timestamp: number; } | { kind: 'not-before'; timestamp: number; } | { kind: 'audience'; did: string; } | { kind: 'budget'; credits: number; } | { kind: 'max-invocations'; count: number; } | { kind: 'capabilities'; allow: string[]; } | { kind: 'custom'; key: string; value: string; } /** * Invocation requires a fresh StepUpAttestation that matches the * invoker, achieves at least `minTier`, and is no older than * `maxAgeMs` (if set) at check time. Verifiers that do not understand * this caveat kind fail closed (the `default:` arm rejects). */ | { kind: 'requires-stepup'; minTier: number; /** Optional freshness bound in milliseconds. Omit for no bound. */ maxAgeMs?: number; } /** * Invocation is only valid for one of the listed hosts. Hosts are * exact string matches; use multiple entries for multiple hosts. No * glob or regex — keep the matching surface small. */ | { kind: 'host-allowlist'; hosts: string[]; } /** * Invocation is only valid when the command argv matches one of the * listed patterns. Patterns are either exact argv arrays or a prefix * spec `{ prefix: string[] }` that matches if the invoked argv starts * with the listed prefix elements. Shell interpolation is NEVER * applied — matching is over literal argv. */ | { kind: 'command-allowlist'; patterns: Array<{ exact: string[]; } | { prefix: string[]; }>; } /** * Invocation must occur within one of the listed time windows. Each * window is `{ startHour, endHour }` in UTC (0-23). A window that * wraps midnight is expressed as `startHour > endHour`. */ | { kind: 'time-window'; windows: Array<{ startHourUtc: number; endHourUtc: number; }>; }; export interface Delegation { /** Opaque ID — used for revocation. */ id: string; /** DID of the issuer (who is granting). */ issuerDid: string; /** DID of the subject (who receives). */ subjectDid: string; /** Capabilities being granted (before caveats restrict them). */ capabilities: string[]; /** Caveats that must all hold for use. */ caveats: Caveat[]; /** When this delegation was issued. */ issuedAt: number; /** Random nonce to prevent replay. */ nonce: string; /** If this attenuates a previous delegation, its ID. */ parentId: string | null; /** Base64-encoded Ed25519 signature by the issuer. */ signature: string; /** Base64-encoded public key of the issuer (for verification). */ issuerPublicKey: string; } /** Context at verification time — who's invoking, what they want, spend so far. */ export interface InvocationContext { /** DID invoking the delegation (the holder/subject using the token). */ invokerDid: string; /** * DID of the service/resource being invoked (the VERIFIER's own identity). * Required if the delegation has an `audience` caveat. If absent when the * caveat is present, validation FAILS CLOSED — this closes audit limit #8 * where audience caveats could be silently ignored. */ audienceDid?: string; /** Capability being exercised. */ capability: string; /** Credits being spent on this invocation. */ creditsSpent?: number; /** Cumulative credits spent across all invocations of this delegation. */ cumulativeCreditsSpent?: number; /** Number of prior invocations (for max-invocations caveat). */ invocationCount?: number; /** Current time (defaults to Date.now()). */ now?: number; /** * Host being targeted by the invocation. Required if the delegation * has a `host-allowlist` caveat — absence fails closed. */ host?: string; /** * Command argv being invoked, as a literal array. Required if the * delegation has a `command-allowlist` caveat — absence fails closed. */ commandArgv?: string[]; /** * A pre-verified step-up attestation. Presence is required if the * delegation has a `requires-stepup` caveat. The verifier MUST have * already verified the attestation's signature and action-digest * binding BEFORE placing it here — this caveat check only compares * tier and age. See `verifyStepUpAttestation` in `stepup.ts`. */ stepUpAttestation?: { subjectDid: string; tierAchieved: number; /** Unix ms when the heart accepted and counter-signed the attestation. */ acceptedAt: number; }; } export declare function createDelegation(opts: { issuerDid: string; issuerPublicKey: string; issuerSigningKey: Uint8Array; subjectDid: string; capabilities: string[]; caveats?: Caveat[]; parentId?: string | null; provider?: CryptoProvider; }): Delegation; /** * Attenuate an existing delegation — subject narrows capability/caveats for * someone else. The new delegation's scope can ONLY be a subset of the parent. */ export declare function attenuateDelegation(opts: { parent: Delegation; newSubjectDid: string; newSubjectSigningKey: Uint8Array; newSubjectPublicKey: string; additionalCaveats?: Caveat[]; narrowedCapabilities?: string[]; provider?: CryptoProvider; }): Delegation; export type DelegationVerification = { valid: true; } | { valid: false; reason: string; }; /** * Verify a delegation's signature + integrity (NOT caveats against context). * * When `lookup` is provided, the verifier additionally confirms that the * issuer's public key was effective at the delegation's `issuedAt` timestamp * via the rotation subsystem. If the key is not found or was not effective, * the delegation is rejected (fail-closed). When `lookup` is omitted, * existing behavior is preserved (backward-compatible). */ export declare function verifyDelegationSignature(del: Delegation, provider?: CryptoProvider, registry?: DidMethodRegistry, lookup?: HistoricalKeyLookup): DelegationVerification; /** * Evaluates a custom caveat for a specific invocation context. * Return `{ valid: true }` to accept, `{ valid: false, reason }` to reject. * Callers MUST register a handler for every custom caveat key they issue; * unhandled keys cause `checkCaveats` to fail closed. */ export type CustomCaveatEvaluator = (caveat: Extract, ctx: InvocationContext) => DelegationVerification; /** * Check caveats against an invocation context. * * @param customCaveatEvaluator - Optional handler for `custom` caveat kinds. * When a `custom` caveat is encountered and no evaluator is provided, the * check fails closed per SOMA-CAPABILITIES-SPEC §Caveat Types. */ export declare function checkCaveats(del: Delegation, ctx: InvocationContext, customCaveatEvaluator?: CustomCaveatEvaluator): DelegationVerification; /** * Full verification: signature + caveats + subject matches invoker. * * @param customCaveatEvaluator - Optional handler for `custom` caveat kinds; * forwarded to {@link checkCaveats}. Unhandled custom caveats fail closed. */ export declare function verifyDelegation(del: Delegation, ctx: InvocationContext, provider?: CryptoProvider, registry?: DidMethodRegistry, lookup?: HistoricalKeyLookup, customCaveatEvaluator?: CustomCaveatEvaluator): DelegationVerification; //# sourceMappingURL=delegation.d.ts.map