|
| 1 | +import { type ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { signUserActorToken } from "@trigger.dev/rbac"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { env } from "~/env.server"; |
| 5 | +import { logger } from "~/services/logger.server"; |
| 6 | +import { rbac } from "~/services/rbac.server"; |
| 7 | + |
| 8 | +// Callers pick the TTL (default 1h) up to a hard ceiling; renewal = mint again |
| 9 | +// with the PAT. The default is short, but the ceiling allows long-lived tokens |
| 10 | +// for callers that need them (e.g. a long-running integration). |
| 11 | +const DEFAULT_UAT_TTL_SECONDS = 60 * 60; // 1 hour |
| 12 | +const MAX_UAT_TTL_SECONDS = 365 * 24 * 60 * 60; // 365 days |
| 13 | + |
| 14 | +// Mint a short-lived delegated user-actor token (`tr_uat_`) from a personal |
| 15 | +// access token. A UAT is a strict downgrade of the PAT: same user identity, |
| 16 | +// short-lived, optionally narrowed by `cap`. It lets a holder (an agent, the |
| 17 | +// MCP server, an IDE) act as the user without carrying a long-lived PAT. |
| 18 | +const RequestBodySchema = z |
| 19 | + .object({ |
| 20 | + // Optional scope cap (e.g. ["read:runs"]) — ceilings the UAT below the |
| 21 | + // user's role. Absent → identity-only, floored by the user's role at |
| 22 | + // use-time. |
| 23 | + cap: z.array(z.string()).optional(), |
| 24 | + // Attribution label recorded in the token's `act.client` (e.g. the agent |
| 25 | + // or tool that requested it). |
| 26 | + client: z.string().min(1).max(255).optional(), |
| 27 | + // Lifetime in seconds. Omitted → 1h. Over the ceiling → 400 (we don't |
| 28 | + // silently clamp, so a caller never thinks it got longer than it did). |
| 29 | + ttlSeconds: z.number().int().positive().max(MAX_UAT_TTL_SECONDS).optional(), |
| 30 | + }) |
| 31 | + .optional(); |
| 32 | + |
| 33 | +export async function action({ request }: ActionFunctionArgs) { |
| 34 | + try { |
| 35 | + // Mint only from a real PAT. authenticatePat requires the `tr_pat_` |
| 36 | + // prefix, so a UAT can't mint another UAT (no indefinite renewal) and an |
| 37 | + // env API key / OAT can't mint one either. |
| 38 | + const patAuth = await rbac.authenticatePat(request, {}); |
| 39 | + if (!patAuth.ok) { |
| 40 | + return json({ error: patAuth.error }, { status: patAuth.status }); |
| 41 | + } |
| 42 | + |
| 43 | + // A role-restricted PAT (one with a TokenRole cap) can't mint a UAT: the |
| 44 | + // UAT is floored by the user's role at use-time and wouldn't carry the |
| 45 | + // PAT's narrower ceiling, so minting would widen the grant. Reject rather |
| 46 | + // than silently escalate. (The OSS fallback has no TokenRoles, so this |
| 47 | + // only takes effect with the cloud RBAC plugin installed.) |
| 48 | + const tokenRole = await rbac.getTokenRole(patAuth.tokenId); |
| 49 | + if (tokenRole) { |
| 50 | + return json( |
| 51 | + { |
| 52 | + error: |
| 53 | + "Cannot mint a user-actor token from a role-restricted personal access token", |
| 54 | + }, |
| 55 | + { status: 403 } |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + const parsedBody = RequestBodySchema.safeParse(await request.json().catch(() => ({}))); |
| 60 | + if (!parsedBody.success) { |
| 61 | + return json( |
| 62 | + { error: "Invalid request body", issues: parsedBody.error.issues }, |
| 63 | + { status: 400 } |
| 64 | + ); |
| 65 | + } |
| 66 | + const body = parsedBody.data ?? {}; |
| 67 | + const ttlSeconds = body.ttlSeconds ?? DEFAULT_UAT_TTL_SECONDS; |
| 68 | + |
| 69 | + const token = await signUserActorToken(env.SESSION_SECRET, { |
| 70 | + userId: patAuth.userId, |
| 71 | + client: body.client ?? "personal-access-token", |
| 72 | + cap: body.cap, |
| 73 | + // Absolute exp (seconds since epoch). jose treats a number as absolute. |
| 74 | + expirationTime: Math.floor(Date.now() / 1000) + ttlSeconds, |
| 75 | + }); |
| 76 | + |
| 77 | + return json({ token, expiresInSeconds: ttlSeconds }); |
| 78 | + } catch (error) { |
| 79 | + if (error instanceof Response) throw error; |
| 80 | + logger.error("Failed to mint user-actor token", { error }); |
| 81 | + return json({ error: "Internal Server Error" }, { status: 500 }); |
| 82 | + } |
| 83 | +} |
0 commit comments