@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
120 lines • 5.48 kB
JavaScript
import { consoleClosedByDefault, isPageNavigation } from '../console-authorize.js';
import { evaluateGovernanceGate } from '../governance-gate.js';
import { resolveAccessDeniedPage } from './access_denied_page.js';
/** The request's actor, or `undefined` when there is no resolver or it rejects the request. */
async function resolveActorQuietly(ctx, actorResolver) {
if (actorResolver === undefined)
return undefined;
try {
return (await actorResolver.resolve(ctx)) ?? undefined;
}
catch {
return undefined;
}
}
/**
* Decide whether a request may use the agent console — the SPA shell AND the `/agent/governance/*`
* routes it reads go through this same check. Touches nothing on the response, so it is unit
* testable free of the router:
*
* - `authorize` omitted → allowed outside production, `403 forbidden` in production.
* - otherwise the actor is resolved (best effort: `undefined` when there is none) and
* `authorize(ctx, actor)` decides: `true`/`{ allowed: true }` allow; `false`/`{ allowed: false }`
* → `403 forbidden`; `reason: 'unauthenticated'` → `401 unauthenticated`; `{ redirectTo }` → `401`
* carrying the path; a throw → `403` (its message as `detail` outside production only).
*/
export async function evaluateDashboardGate(ctx, options) {
const inProduction = options.inProduction === true;
if (consoleClosedByDefault(options.authorize, inProduction)) {
return { ok: false, status: 403, error: 'forbidden' };
}
if (options.authorize === undefined)
return { ok: true };
const actor = await resolveActorQuietly(ctx, options.actorResolver);
const verdict = await evaluateGovernanceGate(actor, ctx, options.authorize, !inProduction);
if (verdict.ok)
return actor === undefined ? { ok: true } : { ok: true, actor };
return {
ok: false,
status: verdict.status === 401 ? 401 : 403,
error: verdict.error ?? 'forbidden',
...(verdict.detail !== undefined ? { detail: verdict.detail } : {}),
...(verdict.redirectTo !== undefined ? { redirectTo: verdict.redirectTo } : {}),
};
}
/**
* Write a refused {@link DashboardGateVerdict} as JSON — what the `/agent/governance/*` routes (and
* any non-page request to the shell) answer: `{ error }`, plus `redirectTo` when `authorize` asked
* for one. A page navigation with `redirectTo` is redirected instead. Stands down when the hook
* already answered the request itself.
*/
export function answerConsoleDenialJson(ctx, verdict) {
if (responseAnswered(ctx))
return;
if (verdict.redirectTo !== undefined && isPageNavigation(ctx)) {
ctx.response.redirect(verdict.redirectTo);
return;
}
ctx.response.status(verdict.status).json({
error: verdict.error,
...(verdict.redirectTo !== undefined ? { redirectTo: verdict.redirectTo } : {}),
...(verdict.detail !== undefined ? { detail: verdict.detail } : {}),
});
}
/**
* Write a refused {@link DashboardGateVerdict} for the SPA shell — shared by BOTH dashboard providers
* (embedded and standalone) so they refuse identically. A page navigation gets the built-in
* access-denied page (or the host's `accessDenied` customisation of it), or a redirect when
* `authorize` returned `{ redirectTo }`; anything else gets the JSON {@link answerConsoleDenialJson}
* writes. Stands down when `authorize` (or the renderer) already answered the request.
*/
export async function answerDashboardDenial(ctx, verdict, options) {
const answered = () => responseAnswered(ctx);
if (answered())
return;
if (!isPageNavigation(ctx)) {
answerConsoleDenialJson(ctx, verdict);
return;
}
if (verdict.redirectTo !== undefined) {
ctx.response.redirect(verdict.redirectTo);
return;
}
const nonce = cspNonce(ctx);
const info = {
status: verdict.status,
reason: verdict.status === 401 ? 'unauthenticated' : 'forbidden',
basePath: options.basePath,
...(verdict.detail !== undefined ? { detail: verdict.detail } : {}),
...(nonce !== undefined ? { nonce } : {}),
};
const html = await resolveAccessDeniedPage(info, options.accessDenied, ctx, answered);
if (html === null)
return;
ctx.response
.status(info.status)
.header('content-type', 'text/html; charset=utf-8')
.header('cache-control', 'no-store, must-revalidate')
.send(html);
}
/**
* Whether something already answered this request: a redirect (`location` header) or a body queued
* on the response. The body check reads AdonisJS's `response.hasLazyBody` structurally so a
* plain-object `ctx` double in a unit test (which has neither) still works.
*/
function responseAnswered(ctx) {
if (ctx.response.getHeader('location'))
return true;
const response = ctx.response;
return response.hasLazyBody === true || response.headersSent === true;
}
/**
* The request's CSP nonce when the host runs `@adonisjs/shield` with `@nonce` in its policy (shield
* exposes it as `response.nonce`). Read structurally: this package neither depends on shield nor
* cares which middleware minted the nonce — only that the page's inline `<style>` carries it.
*/
function cspNonce(ctx) {
const nonce = ctx.response.nonce;
return typeof nonce === 'string' && nonce !== '' ? nonce : undefined;
}
//# sourceMappingURL=gate.js.map