UNPKG

@adonis-agora/agent

Version:

Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.

95 lines • 3.74 kB
import { SignJWT } from 'jose'; import { bareEcPublicJwk, importSigningKey } from './keys.js'; import { nowSeconds, randomId, requireSecureUrl } from './util.js'; /** Lifetime of a client assertion (the guides suggest at most five minutes). */ export const ASSERTION_TTL_SECONDS = 60; export const DELEGATION_TYP = 'pap-delegation+jwt'; /** How long a delegation lasts, within the 24 hours 5.2 allows. */ export const DELEGATION_TTL_SECONDS = 7 * 3600; /** * The Personal Agent's identity (4.1): its `client_id` URL, its metadata document, and the * keys from `jwks_uri` that sign client assertions and the delegations of Session JWT keys. */ export class AgentIdentity { baseUrl; clientName; logoUri; keys; extensions; constructor(cfg, sec = {}) { const base = requireSecureUrl(cfg.baseUrl, 'identity base URL', sec); if (base.search || base.hash) throw new TypeError('identity base URL must not have a query'); this.baseUrl = base.toString().replace(/\/+$/, ''); this.clientName = cfg.clientName; this.logoUri = cfg.logoUri ?? `${this.baseUrl}/logo.svg`; if (cfg.keys.length === 0) throw new TypeError('identity needs at least one signing key'); this.keys = cfg.keys; this.extensions = cfg.extensions; } /** `client_id` is the URL the metadata document is fetched from (4.1). */ get clientId() { return `${this.baseUrl}/agent.json`; } get jwksUri() { return `${this.baseUrl}/jwks.json`; } /** Same origin as `client_id`, as 4.1 requires for `redirect_uris`. */ get redirectUri() { return `${this.baseUrl}/oauth/callback`; } metadata() { const doc = { client_id: this.clientId, client_name: this.clientName, logo_uri: this.logoUri, jwks_uri: this.jwksUri, redirect_uris: [this.redirectUri], token_endpoint_auth_method: 'private_key_jwt', }; if (this.extensions && Object.keys(this.extensions).length > 0) { doc.extensions = this.extensions; } return doc; } /** The public key set. Never contains private members. */ jwks() { return { keys: this.keys.map((k) => ({ ...k.publicJwk })) }; } async sign(payload, typ) { const key = this.keys[0]; if (!key) throw new TypeError('identity has no signing key'); const header = { alg: 'ES256', kid: key.kid }; if (typ) header.typ = typ; return new SignJWT(payload).setProtectedHeader(header).sign(await importSigningKey(key)); } /** * `private_key_jwt` client assertion (4.1, RFC 7523): `iss` = `sub` = `client_id`, `aud` = the * Company's issuer as a single string, short-lived, with `iat` and a fresh `jti`. */ clientAssertion(issuer) { const iat = nowSeconds(); return this.sign({ iss: this.clientId, sub: this.clientId, aud: issuer, iat, exp: iat + ASSERTION_TTL_SECONDS, jti: randomId(), }); } /** * A delegation (5.2): vouches for one delegated public key. It names no User or Company. Its * times are rounded to the hour, as the guides suggest, so they say less about when it was made. */ async delegate(publicJwk) { const iat = Math.floor(nowSeconds() / 3600) * 3600; const exp = iat + DELEGATION_TTL_SECONDS; const jwt = await this.sign({ iss: this.clientId, cnf: { jwk: bareEcPublicJwk(publicJwk) }, iat, exp, jti: randomId() }, DELEGATION_TYP); return { jwt, exp }; } } //# sourceMappingURL=identity.js.map