/** * Endpoint-key authentication — the claude-code harness's ONLY auth mode. * There is no OAuth anywhere in this package: a delegation round reaches its * endpoint with `ANTHROPIC_BASE_URL` plus `ANTHROPIC_API_KEY` (x-api-key) or * `ANTHROPIC_AUTH_TOKEN` (Authorization: Bearer), exactly the environment a * shell would export for a proxy relay, and `claude -p` needs nothing else * (verified against claude 2.1.296: a fresh scoped home with only these * variables runs a round and scaffolds `.claude.json` itself — no onboarding * or key-approval gate in headless mode). * * Two layers, merged per field, file over config: * * - `/endpoint-auth.json` (mode 0600) — the per-scope credential. * Its name deliberately matches the eval contract hash's credential denylist * (`auth`), so the key material never enters an evaluation's condition * fingerprint; the file's mtime serves as the credential stamp, which makes * "edit the file" the recovery gesture for a `rejected` grade. * - The plugin config's `baseUrl` / `apiKey` / `authToken` — the deployment * default every scope without a file shares. Read PER ROUND from the * apply-time snapshot, so a profile edit applies to the next round (and a * host restart clears the in-memory `rejected` mark). * * The env layer this module emits rides the family's existing plumbing: * `delegationEnv` on the host, and `containerExecSpawn`'s name-only `-e` * forwarding into a container (the value travels in the docker client's * environment, never on an argv). A containerized round therefore * authenticates with the same credential as the host — nothing is authorized * inside the unit, which is the point. * @module @khorsheed/dsh-local-agent-claude-code/endpoint-auth */ /** * The scoped-home credential file's name. Chosen to match the eval contract * hash's credential-shaped denylist (`/token|key|credential|secret|password|auth|\.env/i`), * so the file is excluded from condition fingerprints by name. */ export declare const ENDPOINT_AUTH_FILE = "endpoint-auth.json"; /** * One scope's endpoint credential. `baseUrl` is optional (absent means the * CLI's own default endpoint — an official console key needs no relay); at * least one of `apiKey` / `authToken` must be non-empty for the scope to * count as authenticated. When both are set the CLI's own precedence rules * apply — the package passes both through untouched. */ export interface ClaudeEndpointAuth { readonly baseUrl?: string; /** Sent as `ANTHROPIC_API_KEY` (the `x-api-key` header). */ readonly apiKey?: string; /** Sent as `ANTHROPIC_AUTH_TOKEN` (the `Authorization: Bearer` header). */ readonly authToken?: string; } /** Whether a resolved endpoint carries a usable credential. */ export declare function hasEndpointCredential(auth: ClaudeEndpointAuth | undefined): auth is (ClaudeEndpointAuth & { apiKey: string; }) | (ClaudeEndpointAuth & { authToken: string; }); /** * Read the scope's endpoint-auth.json. Absent or unreadable yields undefined * (the caller falls back to the plugin config); malformed JSON or a non-object * payload also yields undefined — a torn file must not fail a status read. * Non-string fields are dropped, never carried through. * @param homeDir - the `claude-code` harness's scoped home. * @returns the file's endpoint credential, or undefined. */ export declare function readEndpointAuthFile(homeDir: string): Promise; /** * Resolve one scope's effective endpoint credential: the scope's * endpoint-auth.json merged FIELD BY FIELD over the plugin config's * `baseUrl` / `apiKey` / `authToken` (a file that names only a key rotates * the key while keeping the deployment's relay URL). The result may carry no * credential at all — {@link hasEndpointCredential} is the judgment, kept * separate so the caller can still route/log the resolved baseUrl. * @param homeDir - the scope's scoped home. * @param config - the plugin config's deployment-level endpoint triple. * @returns the merged endpoint auth. */ export declare function resolveEndpointAuth(homeDir: string, config: ClaudeEndpointAuth): Promise; /** * The spawn-env layer for a resolved endpoint: one entry per defined field, * nothing else. Undefined yields an empty layer — the spread then adds no * keys at all, byte-identical to the pre-endpoint env. * @param auth - the resolved endpoint auth, or undefined. * @returns the explicit env entries for the delegation spawn. */ export declare function endpointAuthEnv(auth: ClaudeEndpointAuth | undefined): NodeJS.ProcessEnv; /** * The credential marker's modification stamp (epoch ms): the endpoint file's * mtime, or undefined when the scope has no file (a config-sourced credential * has no marker — rotating it pairs with the profile edit and instance * restart that clear the in-memory grade marks). A fresh file write is a * fresh stamp, which is how an edited-in key recovers a `rejected` grade. * @param homeDir - the scope's scoped home. * @returns the stamp, or undefined. */ export declare function endpointAuthStamp(homeDir: string): Promise; /** Whether the scope carries its own endpoint file at all. */ export declare function endpointAuthFileExists(homeDir: string): Promise; /** * Write the scope's endpoint-auth.json (mode 0600, mkdir -p first), the * per-scope provisioning path an evaluation's `conditions provision` (or a * person) uses to give one scope its own credential. Whole-file write: the * payload is exactly what {@link readEndpointAuthFile} parses back. * @param homeDir - the scope's scoped home. * @param auth - the endpoint credential to persist. */ export declare function writeEndpointAuth(homeDir: string, auth: ClaudeEndpointAuth): Promise; /** * Clear the scope's OWN endpoint credential (the endpoint-auth.json file). * A deployment-level credential from the plugin config is untouched — the * core's logout reply then names the profile row as the place to edit. * @param homeDir - the scope's scoped home. */ export declare function clearEndpointAuth(homeDir: string): Promise; /** The official endpoint used when no baseUrl is configured anywhere. */ export declare const OFFICIAL_BASE_URL = "https://api.anthropic.com"; /** The subcommand's fetch shape (injected in tests). */ export type EndpointCheckFetch = (url: string, init: { headers: Record; signal: AbortSignal; }) => Promise<{ ok: boolean; status: number; text(): Promise; }>; /** One model the endpoint serves, from `GET /v1/models`. */ export interface EndpointServedModel { readonly id: string; readonly displayName?: string; /** Reasoning effort levels the endpoint advertises for this model, in ascending order. */ readonly efforts?: readonly string[]; } /** * The outcome of one `GET /v1/models`: the parsed model list, the * HTTP status when the endpoint answered, or the transport error's message. * Callers fold it their own way (the check's verdict text, the catalog's * directory); no case throws. */ export type EndpointModelsResult = { readonly kind: 'models'; readonly models: readonly EndpointServedModel[]; } | { readonly kind: 'http'; readonly status: number; readonly body: string; } | { readonly kind: 'unreachable'; readonly message: string; } | { readonly kind: 'malformed'; }; /** * `GET /v1/models` with the resolved credential's auth header(s) — * the token-free endpoint probe shared by the `/claude-code check` verdict * and the model catalog. Anthropic's own API serves this route with * `x-api-key`; relays (openproxy verified) proxy it and answer the same * shape. Never throws: every failure mode folds into the result. * @param endpoint - the resolved endpoint auth (caller checks the credential). * @param fetchImpl - the HTTP fetch; default the host's global fetch. * @param signal - abort for the whole probe (timeout bundled by the caller). * @returns the folded result. */ export declare function fetchEndpointModels(endpoint: ClaudeEndpointAuth & { readonly baseUrl: string; }, fetchImpl?: EndpointCheckFetch, signal?: AbortSignal): Promise; /** * Probe the resolved endpoint with a cheap, token-free round-trip: `GET * /v1/models` with the resolved credential's auth header(s). A 200 * proves both reachability and credential validity — the exact evidence a * "verify" button needs — and the served model list rides the reply so the * user sees what the endpoint offers (any id can be typed into the model * field by hand). One transport-level retry rides along: a local proxy * restart (a Surge/clash reload) stalls the first attempt into the timeout * and answers the retry immediately. Never throws: every failure mode folds * into the reply text, since the reply IS the diagnostic. * @param endpoint - the resolved endpoint auth (scope file over settings). * @param fetchImpl - the HTTP fetch; default the host's global fetch. * @param timeoutMs - one attempt's deadline (two attempts at most). * @returns the human-readable verdict for the `/claude-code check` reply. */ export declare function checkClaudeEndpoint(endpoint: ClaudeEndpointAuth, fetchImpl?: EndpointCheckFetch, timeoutMs?: number): Promise; //# sourceMappingURL=endpoint-auth.d.ts.map