@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
153 lines • 7.75 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
import type { ApplicationService } from '@adonisjs/core/types';
export { actorFromClaims, discoverJwksUri, issuerMetadataUrls } from '../mcp/jwt-verifier.js';
/**
* `devTools.auth`: OAuth for the dev tools hub (`/mcp/agora`) with any authorization server.
*
* The hub publishes `authorizationServers` in its RFC 9728 protected-resource metadata, answers a
* refused request with `WWW-Authenticate: Bearer resource_metadata=…` (so an MCP client opens the
* login), and hands every bearer token to `verify`. The principal `verify` returns is what the
* consoles' `authorize` callbacks see: through {@link devToolsPrincipal}, and as `ctx.auth.user` when
* it carries a `user`.
*/
export interface DevToolsAuth {
/** Issuer URLs advertised as `authorization_servers` (RFC 9728). */
authorizationServers: string[];
/** Advertised as `scopes_supported`. */
scopes?: string[];
/** Advertised as `resource_name`. Default `'Agora dev tools'`. */
resourceName?: string;
/**
* Validate a bearer token. Return the principal, or `null` when the token is not one of yours
* (it is then left on the request for an `authorizeByToken` console). Throw `McpAuthError` to
* refuse a token you recognized (bad audience, expired…): `401` with `error="invalid_token"`, or
* `403` with `error="insufficient_scope"`. Any other throw is a `401` too, and is logged.
*/
verify(token: string, context: DevToolsAuthContext): Promise<DevToolsPrincipal | null>;
}
/** What `verify` knows about the request. */
export interface DevToolsAuthContext {
ctx: HttpContext;
app: ApplicationService;
/** The hub's resource URL (`https://app.example.com/mcp/agora`), the audience a token must name. */
resource: string;
}
/** Who a verified token belongs to. */
export interface DevToolsPrincipal {
/** The token's subject (`sub`). Audit lines show it as `user:<subject>`. */
subject: string;
/** The token's claims (a JWT payload, or an introspection response). */
claims: Record<string, unknown>;
/** Roles of the caller, when the token carries them. Default: a string array under `claims.roles`. */
roles?: string[];
/** Scopes the token was granted. */
scopes?: string[];
/** The app's user for this subject. Set → `ctx.auth.user` is this user during the request. */
user?: unknown;
/**
* Who is really acting, when the token was minted for an impersonation (RFC 8693 `act` claim).
* Audit lines use `actor.sub` when set; authorization stays on `subject`.
*/
actor?: {
sub: string;
email?: string;
};
}
/**
* The principal `devTools.auth` verified for this request, or `undefined` (no token, a token the
* hub's auth did not recognize, or not a dev tools request). Read it in an `authorize`:
*
* ```ts
* authorize: (ctx) => devToolsPrincipal(ctx)?.roles?.includes('admin') === true
* ```
*/
export declare function devToolsPrincipal(ctx: object): DevToolsPrincipal | undefined;
/**
* Put the principal on the request: {@link devToolsPrincipal}, and with a `user`, `ctx.auth` answers
* for it (`user`, `check()`, `authenticate()`, `checkUsing()`…) the way a guard that signed it in
* would, so `authorizeByRoles(…)` and `(ctx) => ctx.auth.user?.isAdmin` work unchanged.
*/
export declare function bindDevToolsPrincipal(ctx: HttpContext, principal: DevToolsPrincipal): void;
/** Resolve the app's user for a verified token. `null` → the account is gone (`401`). */
export type DevToolsUserResolver = (claims: Record<string, unknown>, context: DevToolsAuthContext) => unknown | Promise<unknown>;
export interface JwtAuthOptions {
/** The authorization server's issuer: the exact `iss` of its tokens, and what the hub advertises. */
issuer: string;
/**
* Accepted `aud` values. Default: the hub's own resource URL (`https://app.example.com/mcp/agora`),
* as RFC 8707 resource indicators bind it.
*/
audience?: string | string[];
/** The JWKS URL. Default: `jwks_uri` from the issuer's OpenID / OAuth server metadata. */
jwksUri?: string;
/** Accepted signing algorithms. Default: the asymmetric ones `jose` supports. */
algorithms?: string[];
/** Scopes the hub advertises in its metadata (`scopes_supported`). */
scopes?: string[];
/** Scopes every token must carry, else `403 insufficient_scope`. */
requiredScopes?: string[];
/** The claim holding the caller's roles. Default `'roles'`. */
rolesClaim?: string;
/** Clock skew allowed on `exp`/`nbf`, in seconds. Default `30`. */
clockTolerance?: number;
/** Find the app's user for the token (e.g. `(claims) => User.find(claims.sub)`). */
resolveUser?: DevToolsUserResolver;
/** Advertised as `resource_name`. */
resourceName?: string;
/**
* Put a verified user token on the request as an authkit identity (`ctx.authkit`, global roles
* from `rolesClaim`), so `getAuthkit(ctx)` and `authkitDashboardAuthorize` see the caller.
* Default: when `@adonis-agora/authkit-client` is installed. Set `false` for an issuer that is not
* authkit.
*/
authkit?: boolean;
}
/**
* Verify JWT access tokens of an OAuth / OIDC authorization server (authkit with JWT access tokens,
* Sésame with `accessTokenFormat: 'jwt'`, Auth0, Keycloak, Entra ID…) against its JWKS: signature,
* `iss`, `aud` (the hub's URL by default) and `exp`/`nbf`. The JWKS URL is discovered from the
* issuer's metadata on first use and its keys are cached (refetched when a token names an unknown key).
*
* A token with another `iss`, or that is not a JWT, is not this auth's: it is left on the request
* for an `authorizeByToken` console.
*
* With `@adonis-agora/authkit-client` installed (or `authkit: true`), a verified user token is also
* put on the request as an authkit identity (`ctx.authkit`, and `ctx.auth` with `authkitClientGuard`),
* so a console gated by `authkitDashboardAuthorize` accepts the caller.
*/
export declare function jwtAuth(options: JwtAuthOptions): DevToolsAuth;
export interface IntrospectionAuthOptions {
/** The authorization server's issuer, advertised in the metadata (and checked against `iss` when the response has one). */
issuer: string;
/** The RFC 7662 introspection endpoint. */
endpoint: string;
/** The hub's client credentials at the authorization server (HTTP Basic). */
clientId: string;
clientSecret: string;
/** Accepted `aud` values. Default: the hub's resource URL. */
audience?: string | string[];
/**
* Refuse a token whose introspection response has no `aud` (one not bound to a resource, which
* any resource of the server could have been issued). Default `true`.
*/
requireAudience?: boolean;
scopes?: string[];
requiredScopes?: string[];
rolesClaim?: string;
resolveUser?: DevToolsUserResolver;
resourceName?: string;
}
/**
* Verify opaque access tokens with the authorization server's introspection endpoint (RFC 7662):
* one `POST` per request. `active: false` → `401`; an `aud` that does not name the hub, or none
* (unless `requireAudience: false`) → `401`. An unreachable endpoint → `503`.
*/
export declare function introspectionAuth(options: IntrospectionAuthOptions): DevToolsAuth;
/** What is wrong with a `devTools.auth` value, read structurally. Empty → usable. */
export declare function devToolsAuthProblems(auth: unknown): {
errors: string[];
warnings: string[];
};
/** The issuers of a `devTools.auth`, for messages. */
export declare function devToolsAuthIssuers(auth: unknown): string[];
//# sourceMappingURL=auth.d.ts.map