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 script to extend token life
- Loading branch information
1 parent
b5c0823
commit 01a9205
Showing
3 changed files
with
88 additions
and
3 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
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,37 @@ | ||
import { ScriptContext } from "../_gen/scripts/extend.ts"; | ||
import { TokenWithSecret } from "../types/common.ts"; | ||
import { tokenFromRow } from "../types/common.ts"; | ||
|
||
export interface Request { | ||
token: string; | ||
newExpiration: string | null; | ||
} | ||
|
||
export interface Response { | ||
token: TokenWithSecret; | ||
} | ||
|
||
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: { | ||
expireAt: req.newExpiration, | ||
}, | ||
}); | ||
|
||
// Return the updated token | ||
return { | ||
token: tokenFromRow(newToken), | ||
}; | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import { RuntimeError, test, TestContext } from "../_gen/test.ts"; | |
import { | ||
assertEquals, | ||
assertRejects, | ||
assertGreater, | ||
} from "https://deno.land/[email protected]/assert/mod.ts"; | ||
|
||
test( | ||
|
@@ -10,7 +11,7 @@ test( | |
const error = await assertRejects(async () => { | ||
await ctx.modules.tokens.validate({ token: "invalid token" }); | ||
}, RuntimeError); | ||
assertEquals(error.code, "TOKEN_NOT_FOUND"); | ||
assertEquals(error.code, "token_not_found"); | ||
}, | ||
); | ||
|
||
|
@@ -27,7 +28,7 @@ test( | |
const error = await assertRejects(async () => { | ||
await ctx.modules.tokens.validate({ token: token.token }); | ||
}, RuntimeError); | ||
assertEquals(error.code, "TOKEN_REVOKED"); | ||
assertEquals(error.code, "token_revoked"); | ||
}, | ||
); | ||
|
||
|
@@ -52,6 +53,50 @@ test( | |
const error = await assertRejects(async () => { | ||
await ctx.modules.tokens.validate({ token: token.token }); | ||
}, RuntimeError); | ||
assertEquals(error.code, "TOKEN_EXPIRED"); | ||
assertEquals(error.code, "token_expired"); | ||
}, | ||
); | ||
|
||
test( | ||
"validate token extended not expired", | ||
async (ctx: TestContext) => { | ||
const { token } = await ctx.modules.tokens.create({ | ||
type: "test", | ||
meta: { foo: "bar" }, | ||
// Set initial expiration to 200ms in the future | ||
expireAt: new Date(Date.now() + 200).toISOString(), | ||
}); | ||
|
||
// Token should be valid | ||
const validateRes = await ctx.modules.tokens.validate({ | ||
token: token.token, | ||
}); | ||
assertEquals(token.id, validateRes.token.id); | ||
|
||
// Extend token expiration by 10 seconds | ||
await ctx.modules.tokens.extend({ | ||
token: token.token, | ||
newExpiration: new Date(Date.now() + 10000).toISOString(), | ||
}); | ||
|
||
// Wait for 0.5 seconds to ensure token WOULD HAVE expired without | ||
// extension. | ||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
|
||
// Token should STILL be valid, and have a different `expireAt` time | ||
const validateResAfterWait = await ctx.modules.tokens.validate({ | ||
token: token.token, | ||
}); | ||
|
||
// Assert that everything except `expireAt` is the same and `expireAt` | ||
// is greater. | ||
assertGreater(validateResAfterWait.token.expireAt, token.expireAt); | ||
assertEquals({ | ||
...validateResAfterWait.token, | ||
expireAt: null, | ||
}, { | ||
...token, | ||
expireAt: null, | ||
}) | ||
}, | ||
); |