import * as Action from "@gjermundgaraba/effect-actions/Action"; import * as Authentication from "@gjermundgaraba/effect-actions/Authentication"; import { Context, Duration, Effect, Layer, Redacted } from "effect"; import { HttpRouter } from "effect/http"; import type { HttpClient } from "effect/http"; import type { HttpApiSecurity } from "effect/http-api"; import type { ConfigurationError } from "./errors.ts"; import { ProviderUnavailable } from "./errors.ts"; import { CurrentPrincipal, Whoami } from "./session.ts"; import type { Caller } from "./session.ts"; import * as Verifier from "./verify.ts"; /** * Verify a request's bearer token with `verifier`, failing as an effect-actions surface * refuses: what a verifier of your own, given to `Authentication.layer`, yields before its * own checks. A declared resource's `provider` does this; reach for it only with a verifier * of your own, whose descriptor declares `ProviderUnavailable` too, or another error the * verifier maps it to. */ export declare const authenticate: (verifier: Verifier.Verifier, token: Redacted.Redacted) => Effect.Effect; export interface Options { readonly issuer: string; /** * The origin browsers and agents reach this application at: the listener that publishes * discovery. The resource is its origin root, so any path here is discarded: one identifier * covers `/api`, `/mcp` and sockets. A request reaching that listener at another host is * logged once as a warning, since an OAuth client discovering the resource there refuses it. */ readonly publicUrl: URL; /** * What the resource's verifier keeps of what it reads, `Verifier.store()`: made once where * the process starts, so it outlives a layer built per request. */ readonly store: Verifier.Store; /** `false` refuses API keys without reading the issuer's key list; only OAuth access tokens are accepted. */ readonly apiKeys?: boolean; /** * `false` publishes no RFC 9728 discovery and names none in a challenge: for a surface * whose callers hold API keys and never run an OAuth flow, such as a second listener * beside the one that publishes the resource. That listener still names the same resource, * so it takes the publishing listener's `publicUrl`, not its own. */ readonly discovery?: boolean; } /** * A refusal a caller outside the router can send as-is: the status, headers and JSON body * `authentication` answers the same refusal with on a route. */ export interface Refusal { readonly status: number; readonly headers: Readonly>; readonly body: string; } export declare const verificationDeadline: Duration.Duration; /** What one Authorization header value amounts to for this resource. */ export type Admission = { readonly ok: true; readonly principal: Verifier.Principal; } | { readonly ok: false; readonly refusal: Refusal; }; /** * What a declared resource's `layer` builds: its verifier and its refusals. An * interface, as `Declared` is, so a consumer's emitted declarations name it. `Scope` is the * resource's scopes, as `make` infers them: a scope `admit` and `watch` check is one of them. */ export interface Built { /** The resource identifier: the public origin root, which a token's audience names. */ readonly resource: string; readonly issuer: string; /** What `authentication` publishes and names in every challenge; none with `discovery: false`. */ readonly protectedResource: Authentication.ProtectedResource | undefined; readonly verifier: Verifier.Verifier; /** * What an Authorization header value amounts to, for a caller outside the router, such as a * Node `upgrade` handler, a socket or a per-request endpoint: any verified caller, or with * `scope`, one that also holds it, so a socket that writes demands its scope before it is * established. */ readonly admit: (authorization: string | null | undefined, scope?: Scope) => Effect.Effect; /** * Fails, with the refusal `admit` would now answer, once the credential it admitted stops * being valid: a token expires, or a key is revoked, expires or loses the scope. Never * succeeds; race it against the connection. */ readonly watch: (authorization: string | null | undefined, scope?: Scope) => Effect.Effect; } /** * The service identity of the resource whose descriptor is named `Name`, so two resources are * two services: each surface reads its own, whatever layer graph builds them. */ export interface Resource { readonly "~@clankerauth/Resource": Name; } type Unavailable = readonly [typeof ProviderUnavailable]; /** * A descriptor of `CurrentPrincipal` by bearer token, declaring `ProviderUnavailable`, as a * binding and an MCP endpoint name it, beside the contracts: * `Authentication.make("notes.Login", CurrentPrincipal, { error: ProviderUnavailable })`. * `E` is what it declares, that one error unless it declares more. */ export type Login = Authentication.Descriptor; type DeclaresUnavailable = [ E extends unknown ? ProviderUnavailable extends E[number]["Type"] ? never : E : never ] extends [never] ? unknown : { readonly "Resource.make takes a descriptor declaring error: ProviderUnavailable": never; }; /** * What a resource declares beside its descriptor, the same in every deployment: the * application's scopes, and the one every credential must carry. */ export interface Declaration { /** * Every scope the resource has: published in discovery, and held by `local`'s principal. * effect-actions refuses one that is no OAuth scope token when the provider builds that * discovery; with `discovery: false`, nothing checks them. */ readonly scopes: readonly Scope[]; /** * The scope every credential must carry, one of `scopes`: verification requires it, every * 401 names it, and a first login requests it. Any other scope is the application's to * require, with `requires`. */ readonly required: NoInfer; } /** * A declared resource, as `make` gives it. `E` is what its descriptor declares, * `ProviderUnavailable` alone unless it declares more. `Scope` is its scopes, as `make` * infers them: `requires`, `admitted`, `admit` and `watch` take only one of them, so a scope * it does not declare is a type error. Where an exported declaration must be written out, * annotate with all three: * `Resource.Declared<"notes.Login", readonly [typeof ProviderUnavailable], "notes:read" | "notes:write">`. */ export interface Declared { /** The descriptor the resource verifies, as given to `make`. */ readonly authentication: Login; /** The built resource: `admit` and `watch` for a caller outside the router, `verifier`. */ readonly service: Context.Service, Built>; /** The resource, built once per layer graph: provide it beside the surfaces it guards. */ readonly layer: (options: Options) => Layer.Layer, ConfigurationError, HttpClient.HttpClient>; /** * The descriptor's provider: its verifier, reading the resource, which also publishes the * resource's discovery. Provide it to every layer serving the resource's protected actions. */ readonly provider: Layer.Layer, never, Resource | HttpRouter.HttpRouter>; /** * Refuses a caller without `scope`, for an authorizer the application writes: * `Action.implement(actions, handlers, { authorize: (action) => action.readOnly ? Effect.void : Notes.requires("notes:write") })`. * The `Forbidden` names the scope for an OAuth client, which steps up to it; an API key or * the process cannot, so theirs names none. */ readonly requires: (scope: Scope) => Effect.Effect; /** * The host's own routes, admitted as the resource's actions are, holding `scope` if given: * `HttpRouter.add("GET", "/frame", frame).pipe(Layer.provide(Notes.admitted().layer))`. * effect-actions' `Authentication.protect` authenticates them with the descriptor's provider * and answers them as it answers an action's route; `scope` is checked inside it with * `requires`. An admitted route reads its caller from `CurrentPrincipal`. */ readonly admitted: (scope?: Scope) => Authentication.Protection; /** * The process itself as a principal, for a trusted local surface such as stdio MCP or a * local command, where no credential exists to verify: every scope the resource declares, * no expiry. The application's authorizer runs against it as against a verified caller: * `Effect.provideService(CurrentPrincipal, Notes.local("owner"))`. */ readonly local: (subject: string) => Caller; /** * `Whoami`, answered from the verified credential. A process surface has no credential, so * serve it over HTTP alone, `ActionHttp.layer(Http, [app, Notes.session])`, and leave it out * of MCP. */ readonly session: Action.Implementation>; } /** * Declare an application's resource, at module level, by the descriptor its bindings and * MCP endpoints name, its scopes and the one every credential carries: * `const Notes = Resource.make(Login, { scopes: ["notes:read", "notes:write"], required: "notes:read" })`. * The descriptor's name names its service, so two resources of one process are two services. * What varies by deployment comes later, where the application starts, as * `Notes.layer(options)`, provided once beside the surfaces. Which scope an action needs * beyond `required` is the application's rule, written with `requires`. The descriptor * declares `ProviderUnavailable`, which its verifier fails with when the issuer cannot be * reached; one that does not is a type error here. */ export declare const make: (authentication: Login & DeclaresUnavailable, declaration: Declaration) => Declared; export {};