import type { CredentialPayload } from './credential.js'; import type { RemoteExecute } from './remote-execute.js'; import type { HostRule } from './packRegistry.js'; /** * HOW A VENDOR AUTHENTICATES — a fact about the vendor, declared by the pack (`TwinPack.auth`) and * applied here (docs/concepts/the-model.md#the-rules, rule 5: the executor "applies the credential by strategy"). * * The executor dispatches on the STRATEGY NAME and never on vendor identity, and imports no pack: * architecture invariant A2's own instruction is that vendor-specific knowledge lives in the * descriptor rather than a kernel switch table. Absent means header replacement (`credential.headers`). */ export type TwinAuthStrategy = /** A vendor of lanes whose lanes authenticate apart (Cloudflare's API by bearer, its R2 by SigV4): a lane named takes * its own strategy, every other call the sealed headers; the request names its lane (`RemoteExecuteRequest.lane`). */ { in: 'lanes'; lanes: Record; } | { in: 'choice'; choices: Array<{ credential: string; strategy: TwinAuthStrategy; }>; } | { in: 'signature'; algorithm: 'oauth1-hmac-sha1'; } /** the key rides in the query string (`?key=…`, `?appid=…`) — no header will do */ | { in: 'query'; name: string; } /** the key rides in the path (Telegram's `/bot/METHOD_NAME`): `pattern`'s first group is its place */ | { in: 'path'; pattern: string; } /** AWS Signature Version 4 — a signature computed per request over the whole canonical request. * `scope` answers which region and service THIS request is for, because a pack may route several * services over one executor (aws routes six); it is pure and is handed no secret. */ | { in: 'signature'; algorithm: 'aws-sigv4'; scope: { region: string; service: string; } | ((req: { method: string; path: string; host: string; headers: Record; body?: string | Uint8Array; }) => { region: string; service: string; }); } /** a signature computed PER REQUEST over bytes only the pack can canonicalize */ | { in: 'signature'; algorithm: 'hmac-sha256'; encoding: 'hex' | 'base64'; header: string; /** The exact bytes to sign, from the request as the kernel will send it. Pure: it is handed no * secret, which is what keeps the secret out of pack code while the pack still decides what * the vendor signs. * * `null` means THIS REQUEST IS NOT SIGNED — a real thing vendors do (Veriff exempts * `POST /sessions`, the call that has no session to sign for yet). It is the pack stating the * vendor's own rule, not a failure: an unsigned request the vendor expects to be unsigned is * correct, and the alternative would be signing bytes the vendor will reject. */ canonical: (req: { method: string; path: string; body?: string; }) => string | null; } /** A TOKEN EXCHANGE: the sealed secret is traded for a short-lived token the calls then carry (Bluesky's * createSession from an app password, an OAuth refresh-token grant). The kernel sends `request` itself — * its body fields are literals or `{ credential: '' }` read from the sealed credential — keeps * the token it answers to itself, and puts it on every later call; a `retryOn` answer (401 by default) * exchanges once more and retries the call once. The pack never holds the secret or the token: what it * needs to know about the session it asks the vendor for with an ordinary authenticated call. */ | { in: 'exchange'; request: { method: 'POST'; path: string; format: 'json' | 'form'; body: Record; /** HTTP Basic client authentication on the token request (an OAuth confidential client), from sealed fields */ basicAuth?: { user: { credential: string; }; password: { credential: string; }; }; }; /** the token's field in the JSON answer (a dotted path) */ token: string; /** the header it rides in (default `authorization`) and its scheme (default `Bearer`; '' for none) */ header?: string; scheme?: string; retryOn?: number[]; /** the vendor's code for an expired token in the answer's `error` field (Bluesky's `ExpiredToken`): with it, * a retryOn status exchanges again only when the answer names that code, so a bad request never spends * the shared token or the vendor's session allowance */ retryCode?: string; /** A vendor that ROTATES the secret each exchange spends (X issues a new refresh token with every refresh grant, * and the old one no longer works): the answer's field holding the new one (a dotted path) and the sealed field * it replaces. The kernel keeps the new value for its next exchange and hands the updated credential to the * root's custody to seal (`onRotate`), so the root outlives the first refresh token. */ rotate?: { answer: string; credential: string; }; /** The answer's field giving the token's lifetime in seconds (a dotted path; OAuth's `expires_in`): a token held past * it is exchanged again before the next call. For a vendor that answers an expired token with no `retryOn` status * (TikTok's API for Business answers HTTP 200 with a code), and so a long-lived process never sends a dead token. */ expiresIn?: string; }; /** Throws with the precise refusal; returns the parsed URL when the origin is usable. */ export declare function validateRemoteOrigin(origin: string, opts?: { allowInsecureLoopback?: boolean; }): URL; /** `vendorHosts`: the pack's declared vendor host rules (its descriptor's `hosts`). An absolute https URL * on one of them — an exact `host` rule whose `pathPattern` the path matches — is a second host of the * same vendor that takes the credential (LinkedIn's image uploads on www.linkedin.com/dms-uploads, * Instagram's rupload host); it is sent credentialed like an anchored call, as HTTP Basic when a rule naming * it names a `basic` user. Any other absolute URL is refused. */ /** Where a credential is kept (the head's and refresh's root: head.ts rootCustody): `key` names the credential stably * across rotations and anew when another is sealed in its place (the token cache is keyed by it, not by the secret's value), `open` reads it as sealed now, `seal` keeps a rotated one. */ /** `unsealed`: where a rotation the seal failed to keep is recorded durably (the field and the cause, never a value), since * the root cannot renew once the process holding the new secret ends; a later successful seal clears it. */ export type CredentialCustody = { key: string; open: () => Promise; seal: (credential: CredentialPayload) => Promise | void; unsealed?: (field: string, cause: string) => void; }; /** `vendor`: the vendor whose rate budget every call is charged to (D8): the call is priced and reserved before it is * made, refused when the budget is spent, and settled with the vendor's answer (its back-off honoured). */ /** `now`: the instant a signature is made at (the wall clock unless given: an acceptance whose vendor is a fixture World * signs at that World's clock, which its twin checks a signature's date against). */ export type RemoteExecuteOptions = { custody?: CredentialCustody; vendor?: string; now?: () => Date; }; export declare function buildRemoteExecute(origin: URL, credential: CredentialPayload, auth?: TwinAuthStrategy, vendorHosts?: readonly HostRule[], options?: RemoteExecuteOptions): RemoteExecute;