import type { Context, Next } from 'hono'; import type { JWTManager } from '../jwt.js'; import { WorkOSApiError } from './error-handler.js'; export interface WorkOSAuthContext { environment: string; apiKey: string; } /** * The verified widget session behind a `/_widgets/*` request. No `/_widgets` path carries an * organization id, so the token's `org_id` is the only source of the organization context. */ export interface WidgetAuthContext { organizationId: string; userId: string; /** The `permissions` claim — the widget scopes the token was minted with. */ permissions: string[]; } export type WorkOSAppEnv = { Variables: { auth?: WorkOSAuthContext; widgetAuth?: WidgetAuthContext; requestId?: string; }; }; export interface ApiKeyEntry { environment: string; /** Expiry timestamp (ISO 8601). Omitted/null means the key never expires. */ expiresAt?: string | null; } export type ApiKeyMap = Record; /** * A key is expired when it has an expiry timestamp in the past. A malformed timestamp * (NaN) is treated as expired — fail closed, so a bad value can't authenticate forever. */ export declare function isApiKeyEntryExpired(entry: ApiKeyEntry): boolean; export declare function authMiddleware(apiKeys: ApiKeyMap): (c: Context, next: Next) => Promise; /** The `aud` claim `POST /widgets/token` mints, and the only audience `/_widgets/*` accepts. */ export declare const WIDGET_TOKEN_AUDIENCE = "widgets"; /** * Every widget-token failure is a 403, never a 401: the shipped `@workos-inc/widgets` client * treats 403 as "the token is the problem" — it refetches the token once, retries, and then * renders its expired-session or incorrect-permissions state — and treats every other error * status as a generic API failure. The private widgets spec documents 403 and no 401 for the * same reason. Business-logic errors on these routes must therefore never use 403. */ export declare function widgetForbidden(message: string): WorkOSApiError; /** * Authenticate `/_widgets/*` with the JWT `POST /widgets/token` mints, instead of an API key. * The token is verified against the emulator's own signing key, must carry the widgets * audience, and must resolve an organization — the org scope every widget route works in. */ export declare function widgetAuthMiddleware(jwt: JWTManager): (c: Context, next: Next) => Promise;