Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit ad062ed

Browse files
committed
feat(tokens): Add a modify_meta script.
1 parent 12a5cf1 commit ad062ed

File tree

6 files changed

+89
-69
lines changed

6 files changed

+89
-69
lines changed

modules/tokens/module.json

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
{
2-
"name": "Tokens",
3-
"description": "Create & verify tokens for authorization purposes.",
4-
"icon": "lock",
5-
"tags": [
6-
"core",
7-
"utility"
8-
],
9-
"authors": [
10-
"rivet-gg",
11-
"NathanFlurry"
12-
],
13-
"status": "stable",
14-
"scripts": {
15-
"create": {
16-
"name": "Create Token"
17-
},
18-
"fetch": {
19-
"name": "Fetch Token",
20-
"description": "Get a token by its ID."
21-
},
22-
"fetch_by_token": {
23-
"name": "Fetch by Token",
24-
"description": "Get a token by its secret token."
25-
},
26-
"revoke": {
27-
"name": "Revoke Token",
28-
"description": "Revoke a token, preventing it from being used again."
29-
},
30-
"validate": {
31-
"name": "Validate Token",
32-
"description": "Validate a token. Throws an error if the token is invalid."
33-
},
34-
"extend": {
35-
"name": "Extend Token",
36-
"description": "Extend or remove the expiration date of a token. (Only works on valid tokens.)"
37-
}
38-
},
39-
"errors": {
40-
"token_not_found": {
41-
"name": "Token Not Found"
42-
},
43-
"token_revoked": {
44-
"name": "Token Revoked"
45-
},
46-
"token_expired": {
47-
"name": "Token Expired"
48-
}
49-
}
2+
"name": "Tokens",
3+
"description": "Create & verify tokens for authorization purposes.",
4+
"icon": "lock",
5+
"tags": [
6+
"core",
7+
"utility"
8+
],
9+
"authors": [
10+
"rivet-gg",
11+
"NathanFlurry"
12+
],
13+
"status": "stable",
14+
"scripts": {
15+
"create": {
16+
"name": "Create Token"
17+
},
18+
"fetch": {
19+
"name": "Fetch Token",
20+
"description": "Get a token by its ID."
21+
},
22+
"fetch_by_token": {
23+
"name": "Fetch by Token",
24+
"description": "Get a token by its secret token."
25+
},
26+
"revoke": {
27+
"name": "Revoke Token",
28+
"description": "Revoke a token, preventing it from being used again."
29+
},
30+
"validate": {
31+
"name": "Validate Token",
32+
"description": "Validate a token. Throws an error if the token is invalid."
33+
},
34+
"extend": {
35+
"name": "Extend Token",
36+
"description": "Extend or remove the expiration date of a token. (Only works on valid tokens.)"
37+
}
38+
},
39+
"errors": {
40+
"token_not_found": {
41+
"name": "Token Not Found"
42+
},
43+
"token_revoked": {
44+
"name": "Token Revoked"
45+
},
46+
"token_expired": {
47+
"name": "Token Expired"
48+
}
49+
}
5050
}

modules/tokens/scripts/extend.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ScriptContext } from "../module.gen.ts";
2-
import { TokenWithSecret, tokenFromRow } from "../utils/types.ts";
2+
import { tokenFromRow, TokenWithSecret } from "../utils/types.ts";
33

44
export interface Request {
5-
token: string;
6-
newExpiration: string | null;
5+
token: string;
6+
newExpiration: string | null;
77
}
88

99
export interface Response {
@@ -14,22 +14,22 @@ export async function run(
1414
ctx: ScriptContext,
1515
req: Request,
1616
): Promise<Response> {
17-
// Ensure the token hasn't expired or been revoked yet
18-
const { token } = await ctx.modules.tokens.validate({
19-
token: req.token,
20-
});
17+
// Ensure the token hasn't expired or been revoked yet
18+
const { token } = await ctx.modules.tokens.validate({
19+
token: req.token,
20+
});
2121

22-
// Update the token's expiration date
23-
const newToken = await ctx.db.token.update({
24-
where: {
25-
id: token.id,
26-
},
27-
data: {
28-
expireAt: req.newExpiration,
29-
},
30-
});
22+
// Update the token's expiration date
23+
const newToken = await ctx.db.token.update({
24+
where: {
25+
id: token.id,
26+
},
27+
data: {
28+
expireAt: req.newExpiration,
29+
},
30+
});
3131

32-
// Return the updated token
32+
// Return the updated token
3333
return {
3434
token: tokenFromRow(newToken),
3535
};

modules/tokens/scripts/revoke.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Prisma, ScriptContext } from "../module.gen.ts";
1+
import { ScriptContext } from "../module.gen.ts";
22

33
export interface Request {
44
tokenIds: string[];

modules/tokens/tests/meta.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { test, TestContext } from "../module.gen.ts";
2+
import {
3+
assertEquals,
4+
assertExists,
5+
} from "https://deno.land/std@0.217.0/assert/mod.ts";
6+
7+
const METADATA = { meta: "data", test: "data" };
8+
9+
test("get_and_check_meta", async (ctx: TestContext) => {
10+
const { token } = await ctx.modules.tokens.create({
11+
type: "test",
12+
meta: METADATA,
13+
});
14+
15+
const { tokens: [returnedToken] } = await ctx.modules.tokens.fetch({
16+
tokenIds: [token.id],
17+
});
18+
assertExists(returnedToken);
19+
assertEquals(returnedToken.meta, METADATA);
20+
});

modules/tokens/tests/validate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { RuntimeError, test, TestContext } from "../module.gen.ts";
22
import {
33
assertEquals,
4-
assertRejects,
54
assertGreater,
5+
assertRejects,
66
} from "https://deno.land/std@0.217.0/assert/mod.ts";
77

88
test(
@@ -97,6 +97,6 @@ test(
9797
}, {
9898
...token,
9999
expireAt: null,
100-
})
100+
});
101101
},
102102
);

tests/basic/deno.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)