import { Effect } from "effect"; import { HttpClient } from "effect/http"; import type { JWK } from "jose"; import { ConfigurationError, InsufficientScope, ProviderUnavailable, Unauthorized } from "./errors.ts"; import type { AuthenticationError } from "./errors.ts"; /** * What a verified credential proves: an OAuth client's access token or an API key. A request's * caller, `CurrentPrincipal`, is this or, on a trusted local surface, the process itself, which * no verifier produces: `Caller` of the `/session` entry. */ export interface Principal { readonly subject: string; readonly scopes: readonly string[]; readonly actor: { readonly kind: "client"; readonly clientId: string; } | { readonly kind: "key"; readonly keyId: string; }; /** * When this credential stops being valid, in epoch milliseconds: an access token's * verified `exp`, so a host never decodes the token again, or an API key's expiry. * `undefined` for a key that does not expire. A key can also be revoked before then; * `Resource.watch` ends a connection at whichever comes first. */ readonly expiresAt: number | undefined; } export interface Options { readonly issuer: string; readonly resource: string; /** Where this verifier keeps what it reads and finds what was read before: see `Store`. */ readonly store: Store; readonly requiredScopes?: readonly string[]; /** `false` refuses API keys without reading the issuer's key list; only OAuth access tokens are accepted. */ readonly apiKeys?: boolean | undefined; } export interface Verifier { readonly verifyToken: (token: string) => Effect.Effect; } /** One verified key list: the entries it holds and how long they may decide. */ export interface Keys { /** Epoch milliseconds, by this host's clock: when the list was read. */ readonly readAt: number; /** Epoch milliseconds: the end of the issuer's outage window for this list. */ readonly expiresAt: number; readonly entries: ReadonlyMap; } /** * The last JWKS document read from an issuer: its keys, which signing keys are resolved * from, or the operation that failed. Either answers until its lifetime or cooldown ends. */ export type SettledJwks = { readonly readAt: number; readonly keys: readonly JWK[]; } | { readonly readAt: number; readonly failure: string; }; /** The last key list read for one resource of an issuer, and the last one that verified. */ export interface SettledKeyList { /** Epoch milliseconds: when the last read ended, which holds off the next for the cooldown. */ readonly readAt: number; /** The operation the last read failed at; `undefined` when it succeeded. */ readonly failure: string | undefined; /** The last list that verified, which decides through an outage until it expires. */ readonly held: Keys | undefined; } /** * What verifiers have read and decoded, for every verifier given it: JWKS documents by * issuer, key lists by issuer and resource. It holds values only, never an Effect, fiber, * Promise, Response or timer, so it may live as long as the process, at module scope on a * runtime whose I/O belongs to one request, such as Cloudflare Workers. Every read runs in * the verification that misses, and writes its result back; concurrent misses each read. */ export interface Store { readonly jwks: Map; readonly keyLists: Map; } /** An empty store: `const store = Verifier.store()`, once, where the process starts. */ export declare const store: () => Store; /** * A verifier over `options.store`; transport is selected by the application, not the SDK. * It holds nothing but its options, the store and the HTTP client, so acquiring one per * request costs nothing: every read it makes runs in the verification that needs it. * Verification runs to completion: a deadline is the request edge's policy, which * `Resource` applies, so an in-process caller can await a provider call it cannot cancel. * The one exception is a key list read, which runs its fetches together and gives up on each * after three seconds. */ export declare const make: (options: Options) => Effect.Effect<{ verifyToken: (token: string) => Effect.Effect<{ subject: string; scopes: string[]; actor: { kind: "client"; clientId: string; }; expiresAt: number; } | { subject: string; scopes: readonly string[]; actor: { kind: "key"; keyId: string; }; expiresAt: number | undefined; }, InsufficientScope | ProviderUnavailable | Unauthorized, never>; }, ConfigurationError, HttpClient.HttpClient>;