-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(cli,webapp): mint short-lived delegated tokens that act as a user #3997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "trigger.dev": patch | ||
| --- | ||
|
|
||
| Adds `trigger.dev mint-token`, which mints a short-lived delegated token from your stored personal access token. The token authenticates against the API as you, can be narrowed with `--cap` and given a lifetime with `--ttl`, and prints to stdout so it can be captured. | ||
|
|
||
| ```bash | ||
| UAT=$(trigger.dev mint-token --ttl 3600 --cap read:runs) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { type ActionFunctionArgs, json } from "@remix-run/server-runtime"; | ||
| import { signUserActorToken } from "@trigger.dev/rbac"; | ||
| import { z } from "zod"; | ||
| import { env } from "~/env.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
| import { rbac } from "~/services/rbac.server"; | ||
|
|
||
| // Callers pick the TTL (default 1h) up to a hard ceiling; renewal = mint again | ||
| // with the PAT. The default is short, but the ceiling allows long-lived tokens | ||
| // for callers that need them (e.g. a long-running integration). | ||
| const DEFAULT_UAT_TTL_SECONDS = 60 * 60; // 1 hour | ||
| const MAX_UAT_TTL_SECONDS = 365 * 24 * 60 * 60; // 365 days | ||
|
|
||
| // Mint a short-lived delegated user-actor token (`tr_uat_`) from a personal | ||
| // access token. A UAT is a strict downgrade of the PAT: same user identity, | ||
| // short-lived, optionally narrowed by `cap`. It lets a holder (an agent, the | ||
| // MCP server, an IDE) act as the user without carrying a long-lived PAT. | ||
| const RequestBodySchema = z | ||
| .object({ | ||
| // Optional scope cap (e.g. ["read:runs"]) — ceilings the UAT below the | ||
| // user's role. Absent → identity-only, floored by the user's role at | ||
| // use-time. | ||
| cap: z.array(z.string()).optional(), | ||
| // Attribution label recorded in the token's `act.client` (e.g. the agent | ||
| // or tool that requested it). | ||
| client: z.string().min(1).max(255).optional(), | ||
| // Lifetime in seconds. Omitted → 1h. Over the ceiling → 400 (we don't | ||
| // silently clamp, so a caller never thinks it got longer than it did). | ||
| ttlSeconds: z.number().int().positive().max(MAX_UAT_TTL_SECONDS).optional(), | ||
| }) | ||
| .optional(); | ||
|
|
||
| export async function action({ request }: ActionFunctionArgs) { | ||
| try { | ||
| // Mint only from a real PAT. authenticatePat requires the `tr_pat_` | ||
| // prefix, so a UAT can't mint another UAT (no indefinite renewal) and an | ||
| // env API key / OAT can't mint one either. | ||
| const patAuth = await rbac.authenticatePat(request, {}); | ||
| if (!patAuth.ok) { | ||
| return json({ error: patAuth.error }, { status: patAuth.status }); | ||
| } | ||
|
|
||
| // A role-restricted PAT (one with a TokenRole cap) can't mint a UAT: the | ||
| // UAT is floored by the user's role at use-time and wouldn't carry the | ||
| // PAT's narrower ceiling, so minting would widen the grant. Reject rather | ||
| // than silently escalate. (The OSS fallback has no TokenRoles, so this | ||
| // only takes effect with the cloud RBAC plugin installed.) | ||
| const tokenRole = await rbac.getTokenRole(patAuth.tokenId); | ||
| if (tokenRole) { | ||
| return json( | ||
| { | ||
| error: | ||
| "Cannot mint a user-actor token from a role-restricted personal access token", | ||
| }, | ||
| { status: 403 } | ||
| ); | ||
| } | ||
|
|
||
| const parsedBody = RequestBodySchema.safeParse(await request.json().catch(() => ({}))); | ||
| if (!parsedBody.success) { | ||
| return json( | ||
| { error: "Invalid request body", issues: parsedBody.error.issues }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
| const body = parsedBody.data ?? {}; | ||
| const ttlSeconds = body.ttlSeconds ?? DEFAULT_UAT_TTL_SECONDS; | ||
|
|
||
| const token = await signUserActorToken(env.SESSION_SECRET, { | ||
| userId: patAuth.userId, | ||
| client: body.client ?? "personal-access-token", | ||
| cap: body.cap, | ||
| // Absolute exp (seconds since epoch). jose treats a number as absolute. | ||
| expirationTime: Math.floor(Date.now() / 1000) + ttlSeconds, | ||
| }); | ||
|
|
||
| return json({ token, expiresInSeconds: ttlSeconds }); | ||
| } catch (error) { | ||
| if (error instanceof Response) throw error; | ||
| logger.error("Failed to mint user-actor token", { error }); | ||
| return json({ error: "Internal Server Error" }, { status: 500 }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.