Skip to content

Commit bd509cb

Browse files
committed
Added task scopes to work like tags and batches
Also removed scopes for tags when auto-generating a public access token as that could be dangerous.
1 parent 4145ddf commit bd509cb

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

apps/webapp/app/routes/api.v3.runs.$runId.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const loader = createLoaderApiRoute(
2121
runs: run.friendlyId,
2222
tags: run.runTags,
2323
batch: run.batch?.friendlyId,
24+
tasks: run.taskIdentifier,
2425
}),
2526
superScopes: ["read:runs", "read:all", "admin"],
2627
},

apps/webapp/app/routes/realtime.v1.runs.$runId.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const loader = createLoaderApiRoute(
3434
runs: run.friendlyId,
3535
tags: run.runTags,
3636
batch: run.batch?.friendlyId,
37+
tasks: run.taskIdentifier,
3738
}),
3839
superScopes: ["read:runs", "read:all", "admin"],
3940
},

apps/webapp/app/routes/realtime.v1.streams.$runId.$streamId.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const loader = createLoaderApiRoute(
4545
runs: run.friendlyId,
4646
tags: run.runTags,
4747
batch: run.batch?.friendlyId,
48+
tasks: run.taskIdentifier,
4849
}),
4950
superScopes: ["read:runs", "read:all", "admin"],
5051
},

docs/frontend/overview.mdx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ You can use our [React hooks](/frontend/react-hooks) in your frontend applicatio
1111
To create a Public Access Token, you can use the `auth.createPublicToken` function in your **backend** code:
1212

1313
```tsx
14-
const publicToken = await auth.createPublicToken();
14+
const publicToken = await auth.createPublicToken(); // 👈 this public access token has no permissions, so is pretty useless!
1515
```
1616

1717
### Scopes
1818

19-
By default a Public Access Token has limited permissions. You can specify the scopes you need when creating a Public Access Token:
19+
By default a Public Access Token has no permissions. You must specify the scopes you need when creating a Public Access Token:
2020

2121
```ts
2222
const publicToken = await auth.createPublicToken({
2323
scopes: {
2424
read: {
25-
runs: true,
25+
runs: true, // ❌ this token can read all runs, possibly useful for debugging/testing
2626
},
2727
},
2828
});
@@ -34,7 +34,7 @@ This will allow the token to read all runs, which is probably not what you want.
3434
const publicToken = await auth.createPublicToken({
3535
scopes: {
3636
read: {
37-
runs: ["run_1234", "run_5678"],
37+
runs: ["run_1234", "run_5678"], // ✅ this token can read only these runs
3838
},
3939
},
4040
});
@@ -46,7 +46,7 @@ You can scope the token to only read certain tasks:
4646
const publicToken = await auth.createPublicToken({
4747
scopes: {
4848
read: {
49-
tasks: ["my-task-1", "my-task-2"],
49+
tasks: ["my-task-1", "my-task-2"], // 👈 this token can read all runs of these tasks
5050
},
5151
},
5252
});
@@ -58,7 +58,7 @@ Or tags:
5858
const publicToken = await auth.createPublicToken({
5959
scopes: {
6060
read: {
61-
tags: ["my-tag-1", "my-tag-2"],
61+
tags: ["my-tag-1", "my-tag-2"], // 👈 this token can read all runs with these tags
6262
},
6363
},
6464
});
@@ -70,13 +70,13 @@ Or a specific batch of runs:
7070
const publicToken = await auth.createPublicToken({
7171
scopes: {
7272
read: {
73-
batch: "batch_1234",
73+
batch: "batch_1234", // 👈 this token can read all runs in this batch
7474
},
7575
},
7676
});
7777
```
7878

79-
You can also combine scopes. For example, to read only certain tasks and tags:
79+
You can also combine scopes. For example, to read runs with specific tags and for specific tasks:
8080

8181
```ts
8282
const publicToken = await auth.createPublicToken({
@@ -105,6 +105,19 @@ const publicToken = await auth.createPublicToken({
105105

106106
This will allow the token to trigger the specified tasks. `tasks` is the only write scope available at the moment.
107107

108+
We **strongly** recommend creating short-lived tokens for write scopes, as they can be used to trigger tasks from your frontend application:
109+
110+
```ts
111+
const publicToken = await auth.createPublicToken({
112+
scopes: {
113+
write: {
114+
tasks: ["my-task-1"], // ✅ this token can trigger this task
115+
},
116+
},
117+
expirationTime: "1m", // ✅ this token will expire after 1 minute
118+
});
119+
```
120+
108121
### Expiration
109122

110123
By default, Public Access Token's expire after 15 minutes. You can specify a different expiration time when creating a Public Access Token:
@@ -133,7 +146,7 @@ const handle = await tasks.trigger("my-task", { some: "data" });
133146
console.log(handle.publicAccessToken);
134147
```
135148

136-
By default, tokens returned from the `trigger` function expire after 15 minutes and have a read scope for that specific run, and any tags associated with it. You can customize the expiration of the auto-generated tokens by passing a `publicTokenOptions` object to the `trigger` function:
149+
By default, tokens returned from the `trigger` function expire after 15 minutes and have a read scope for that specific run. You can customize the expiration of the auto-generated tokens by passing a `publicTokenOptions` object to the `trigger` function:
137150

138151
```ts
139152
const handle = await tasks.trigger(

docs/frontend/react-hooks.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export async function startRun() {
144144
const handle = await tasks.trigger<typeof exampleTask>("example", { foo: "bar" });
145145

146146
// Set the auto-generated publicAccessToken in a cookie
147-
cookies().set("publicAccessToken", handle.publicAccessToken);
147+
cookies().set("publicAccessToken", handle.publicAccessToken); // ✅ this token only has access to read this run
148148

149149
redirect(`/runs/${handle.id}`);
150150
}

packages/core/src/v3/apiClient/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,7 @@ export class ApiClient {
214214
secretKey: this.accessToken,
215215
payload: {
216216
...claims,
217-
scopes: [`read:runs:${data.id}`].concat(
218-
body.options?.tags ? Array.from(body.options?.tags).map((t) => `read:tags:${t}`) : []
219-
),
217+
scopes: [`read:runs:${data.id}`],
220218
},
221219
expirationTime: requestOptions?.publicAccessToken?.expirationTime ?? "1h",
222220
});

0 commit comments

Comments
 (0)