@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
115 lines • 5.1 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
/** `'read'`: the viewer may look, never change (no write capability reaches a command). */
export type ConsoleAccess = 'read' | 'write';
/** What an authorizer may answer. */
export type ConsoleAuthorizeResult = boolean | {
allowed: true;
/**
* The apps (tenant partitions) this viewer may see and change. `'*'` (or omitted) is every
* app `console.apps` allows.
*/
apps?: '*' | string[];
/** The tenants this viewer may see, for consoles that scope by tenant. `'*'`: all. */
tenants?: '*' | string[];
/**
* What this viewer may do, in the capability vocabulary (`<lib>:console:read`,
* `<lib>:console:write`, `<lib>:console:*`, or the lib's finer names). Sent with every command
* to another app, which checks it again. Omitted: the lib's full console set. Anything but a
* list of strings allows nothing.
*/
capabilities?: string[];
/** `'read'`: sugar for "no write capabilities", whatever `capabilities` says. */
access?: ConsoleAccess;
} | {
allowed: false;
reason?: 'unauthenticated' | 'forbidden';
} | {
redirectTo: string;
};
/** A class form, for an authorizer in its own file (`@inject()` works). */
export interface ConsoleAuthorizer {
authorize(ctx: HttpContext): ConsoleAuthorizeResult | Promise<ConsoleAuthorizeResult>;
}
export type ConsoleAuthorizerClass = new (...args: any[]) => ConsoleAuthorizer;
/** The `authorize` option: an inline function, the authorizer class, or a lazy import of it. */
export type ConsoleAuthorize = ((ctx: HttpContext) => ConsoleAuthorizeResult | Promise<ConsoleAuthorizeResult>) | ConsoleAuthorizerClass | (() => Promise<{
default: ConsoleAuthorizerClass;
}>);
/** A {@link ConsoleAuthorizeResult} normalized to the answer the console writes. */
export type ConsoleAuthorizeDecision = {
allowed: true;
apps?: '*' | string[];
tenants?: '*' | string[];
capabilities?: string[];
access?: ConsoleAccess;
} | {
allowed: false;
status: 401;
reason: 'unauthenticated';
} | {
allowed: false;
status: 403;
reason: 'forbidden';
error?: unknown;
} | {
allowed: false;
status: 302;
redirectTo: string;
};
/**
* Normalize an authorizer's answer. Fails closed: only `true` or `{ allowed: true }` passes. Its
* `apps` / `tenants`, when not `'*'`, are kept as lists of names (a malformed one allows none);
* `capabilities`, when present, as a list of non-empty strings (a malformed one allows nothing);
* `access` only as `'read'` or `'write'`. `{ redirectTo }` with a non-empty string is a redirect;
* anything else is a denial (`401` for `reason: 'unauthenticated'`, `403` otherwise).
*/
export declare function normalizeConsoleAuthorizeResult(result: unknown): ConsoleAuthorizeDecision;
/**
* Turn an `authorize` option into one guard function.
*
* - A class is built through the request's container (`ctx.containerResolver.make`), so
* `@inject()` works, and its `authorize(ctx)` is called.
* - A function is called with `ctx`. When it resolves to a module whose `default` is an
* authorizer class, it was a lazy import: the class is remembered (the import runs once per
* returned guard) and used as above.
* - A thrown error is a `403` denial, carried on `error` so the caller can log it.
*/
export declare function resolveConsoleAuthorize(authorize: ConsoleAuthorize): (ctx: HttpContext) => Promise<ConsoleAuthorizeDecision>;
/** Options for {@link authorizeByToken}. */
export interface AuthorizeByTokenOptions {
/** The shared secret callers send as `Authorization: Bearer <token>`. */
token: string | undefined;
/** What a caller with the token may see and do: the same fields as an `authorize` answer. */
apps?: '*' | string[];
tenants?: '*' | string[];
capabilities?: string[];
access?: ConsoleAccess;
}
/**
* An `authorize` check for machine access: allows a request whose `Authorization` header is
* `Bearer <token>` (compared in constant time), and answers
* `{ allowed: false, reason: 'unauthenticated' }` (`401`) otherwise. Only the header is read.
*
* ```ts
* authorize: authorizeByToken({ token: env.get('CONSOLE_TOKEN') }),
* // narrowed: authorizeByToken({ token, apps: ['shop'], access: 'read' })
* ```
*
* Throws at config time when `token` is empty or undefined.
*/
export declare function authorizeByToken(options: AuthorizeByTokenOptions): ConsoleAuthorize;
/**
* Allows a request when ANY of the given checks allows it, tried in order. Typical: a token for MCP
* clients and scripts, or a signed-in admin for the browser.
*
* ```ts
* authorize: anyAuthorize(
* authorizeByToken({ token: env.get('CONSOLE_TOKEN') }),
* (ctx) => ctx.auth.user?.isAdmin ?? false,
* ),
* ```
*
* Refused by all: a redirect when one asked for it, else `401` when one wanted a sign-in, else `403`.
*/
export declare function anyAuthorize(...authorizers: ConsoleAuthorize[]): (ctx: HttpContext) => Promise<ConsoleAuthorizeResult>;
//# sourceMappingURL=console_authorize.d.ts.map