@nylorun/agents 0.12.0-beta → 0.14.0-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +0,0 @@
1
- /**
2
- * A route for app servers that mints a subject token for the signed-in person (Host feature
3
- * `subject-tokens`). The browser client calls it whenever it needs a token. Mount it on your
4
- * app's own origin behind your sign-in: `POST` only, no CORS, never cached.
5
- *
6
- * export const POST = createTokenEndpoint({
7
- * client, role: "user", subject: (request) => userFromCookie(request)?.id,
8
- * });
9
- */
10
- import type { TokenScope } from "@nylorun/core/contracts";
11
- import type { AgentsClient } from "./session-client.js";
12
- export interface TokenEndpointOptions {
13
- /** The application client (the Tenant key stays on the server). */
14
- client: AgentsClient | Promise<AgentsClient>;
15
- /** The signed-in person the token is for; `undefined` answers 401. */
16
- subject(request: Request): string | undefined | Promise<string | undefined>;
17
- /** The access policy role to mint for, or a function of the request and subject. */
18
- role: string | ((request: Request, subject: string) => string | Promise<string>);
19
- scopes?: readonly TokenScope[];
20
- agents?: readonly string[];
21
- ttlSeconds?: number;
22
- }
23
- export declare function createTokenEndpoint(options: TokenEndpointOptions): (request: Request) => Promise<Response>;
@@ -1,30 +0,0 @@
1
- import { RuntimeError } from "./http.js";
2
- const NO_STORE = { "cache-control": "no-store" };
3
- export function createTokenEndpoint(options) {
4
- return async (request) => {
5
- if (request.method !== "POST")
6
- return new Response(null, { status: 405, headers: { allow: "POST", ...NO_STORE } });
7
- const subject = await options.subject(request);
8
- if (!subject)
9
- return Response.json({ error: "Sign in required" }, { status: 401, headers: NO_STORE });
10
- const role = typeof options.role === "string"
11
- ? options.role
12
- : await options.role(request, subject);
13
- try {
14
- const client = await options.client;
15
- const minted = await client.tokens.create({
16
- subject,
17
- role,
18
- ...(options.scopes ? { scopes: options.scopes } : {}),
19
- ...(options.agents ? { agents: options.agents } : {}),
20
- ...(options.ttlSeconds === undefined ? {} : { ttlSeconds: options.ttlSeconds }),
21
- });
22
- return Response.json({ token: minted.token, expiresAt: minted.expiresAt }, { headers: NO_STORE });
23
- }
24
- catch (error) {
25
- // The person sees that a token could not be issued, never the Runtime's details.
26
- const status = error instanceof RuntimeError && error.status === 400 ? 403 : 502;
27
- return Response.json({ error: "Could not issue a token" }, { status, headers: NO_STORE });
28
- }
29
- };
30
- }