This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tokens): Add a
modify_meta
script.
- Loading branch information
1 parent
801efc4
commit d2012c6
Showing
8 changed files
with
158 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,50 +1,54 @@ | ||
{ | ||
"name": "Tokens", | ||
"description": "Create & verify tokens for authorization purposes.", | ||
"icon": "lock", | ||
"tags": [ | ||
"core", | ||
"utility" | ||
], | ||
"authors": [ | ||
"rivet-gg", | ||
"NathanFlurry" | ||
], | ||
"status": "stable", | ||
"scripts": { | ||
"create": { | ||
"name": "Create Token" | ||
}, | ||
"fetch": { | ||
"name": "Fetch Token", | ||
"description": "Get a token by its ID." | ||
}, | ||
"fetch_by_token": { | ||
"name": "Fetch by Token", | ||
"description": "Get a token by its secret token." | ||
}, | ||
"revoke": { | ||
"name": "Revoke Token", | ||
"description": "Revoke a token, preventing it from being used again." | ||
}, | ||
"validate": { | ||
"name": "Validate Token", | ||
"description": "Validate a token. Throws an error if the token is invalid." | ||
}, | ||
"extend": { | ||
"name": "Extend Token", | ||
"description": "Extend or remove the expiration date of a token. (Only works on valid tokens.)" | ||
} | ||
}, | ||
"errors": { | ||
"token_not_found": { | ||
"name": "Token Not Found" | ||
}, | ||
"token_revoked": { | ||
"name": "Token Revoked" | ||
}, | ||
"token_expired": { | ||
"name": "Token Expired" | ||
} | ||
} | ||
"name": "Tokens", | ||
"description": "Create & verify tokens for authorization purposes.", | ||
"icon": "lock", | ||
"tags": [ | ||
"core", | ||
"utility" | ||
], | ||
"authors": [ | ||
"rivet-gg", | ||
"NathanFlurry" | ||
], | ||
"status": "stable", | ||
"scripts": { | ||
"create": { | ||
"name": "Create Token" | ||
}, | ||
"fetch": { | ||
"name": "Fetch Token", | ||
"description": "Get a token by its ID." | ||
}, | ||
"fetch_by_token": { | ||
"name": "Fetch by Token", | ||
"description": "Get a token by its secret token." | ||
}, | ||
"revoke": { | ||
"name": "Revoke Token", | ||
"description": "Revoke a token, preventing it from being used again." | ||
}, | ||
"validate": { | ||
"name": "Validate Token", | ||
"description": "Validate a token. Throws an error if the token is invalid." | ||
}, | ||
"extend": { | ||
"name": "Extend Token", | ||
"description": "Extend or remove the expiration date of a token. (Only works on valid tokens.)" | ||
}, | ||
"modify_meta": { | ||
"name": "Modify Token Metadata", | ||
"description": "Modify the token's associated metadata, additionally returning the old metadata. (Only works on valid tokens.)" | ||
} | ||
}, | ||
"errors": { | ||
"token_not_found": { | ||
"name": "Token Not Found" | ||
}, | ||
"token_revoked": { | ||
"name": "Token Revoked" | ||
}, | ||
"token_expired": { | ||
"name": "Token Expired" | ||
} | ||
} | ||
} |
This file contains 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 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,38 @@ | ||
import { ScriptContext } from "../module.gen.ts"; | ||
import { Token, tokenFromRow } from "../utils/types.ts"; | ||
|
||
export interface Request { | ||
token: string; | ||
newMeta: { [key: string]: any }; | ||
} | ||
|
||
export interface Response { | ||
token: Token; | ||
oldMeta: { [key: string]: any }; | ||
} | ||
|
||
export async function run( | ||
ctx: ScriptContext, | ||
req: Request, | ||
): Promise<Response> { | ||
// Ensure the token hasn't expired or been revoked yet | ||
const { token } = await ctx.modules.tokens.validate({ | ||
token: req.token, | ||
}); | ||
|
||
// Update the token's expiration date | ||
const newToken = await ctx.db.token.update({ | ||
where: { | ||
id: token.id, | ||
}, | ||
data: { | ||
meta: req.newMeta, | ||
}, | ||
}); | ||
|
||
// Return the updated token | ||
return { | ||
token: tokenFromRow(newToken), | ||
oldMeta: token.meta, | ||
}; | ||
} |
This file contains 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 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,42 @@ | ||
import { test, TestContext } from "../module.gen.ts"; | ||
import { | ||
assertEquals, | ||
assertExists, | ||
assertRejects, | ||
} from "https://deno.land/[email protected]/assert/mod.ts"; | ||
import { RuntimeError } from "../module.gen.ts"; | ||
|
||
const METADATA_PRE = { state: "beforeModify" }; | ||
const METADATA_POST = { state: "afterModify" }; | ||
|
||
test("modify_meta", async (ctx: TestContext) => { | ||
const { token } = await ctx.modules.tokens.create({ | ||
type: "test", | ||
meta: METADATA_PRE, | ||
}); | ||
|
||
const { tokens: [tokenPre] } = await ctx.modules.tokens.fetch({ | ||
tokenIds: [token.id], | ||
}); | ||
assertExists(tokenPre); | ||
// assertEquals(typeof tokenPre.meta, "string"); | ||
assertEquals(tokenPre.meta, METADATA_PRE); | ||
|
||
const { oldMeta: fetchedOldMeta, token: tokenPost } = await ctx.modules.tokens | ||
.modifyMeta({ | ||
token: token.token, | ||
newMeta: METADATA_POST, | ||
}); | ||
assertExists(tokenPost); | ||
assertEquals(tokenPost.meta, METADATA_POST); | ||
assertEquals(fetchedOldMeta, METADATA_PRE); | ||
|
||
await ctx.modules.tokens.revoke({ tokenIds: [token.id] }); | ||
|
||
const err = await assertRejects(() => | ||
ctx.modules.tokens.modifyMeta({ | ||
token: token.token, | ||
newMeta: METADATA_POST, | ||
}), RuntimeError); | ||
assertEquals(err.code, "token_revoked"); | ||
}); |
This file contains 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { RuntimeError, test, TestContext } from "../module.gen.ts"; | ||
import { | ||
assertEquals, | ||
assertRejects, | ||
assertGreater, | ||
assertRejects, | ||
} from "https://deno.land/[email protected]/assert/mod.ts"; | ||
|
||
test( | ||
|
@@ -97,6 +97,6 @@ test( | |
}, { | ||
...token, | ||
expireAt: null, | ||
}) | ||
}); | ||
}, | ||
); |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.