1
0
silverbullet/server/crypto.test.ts

28 lines
949 B
TypeScript
Raw Permalink Normal View History

2023-12-11 11:11:56 +00:00
import { sleep } from "$sb/lib/async.ts";
import { MemoryKvPrimitives } from "../plugos/lib/memory_kv_primitives.ts";
import { assertEquals } from "../test_deps.ts";
import { JWTIssuer } from "./crypto.ts";
Deno.test("Test JWT crypto", async () => {
const db = new MemoryKvPrimitives();
const jwt = new JWTIssuer(db);
await jwt.init("test");
// Timeout value is 0 seconds, which means it should expire immediately with a 1 second grace period
const token = await jwt.createJWT({ user: "pete", role: "admin" }, 0);
const verified = await jwt.verifyAndDecodeJWT(token);
assertEquals(verified.user, "pete");
try {
await jwt.verifyAndDecodeJWT(token + "bla");
assertEquals(true, false, "Should have thrown invalid signature");
} catch {
// expected
}
await sleep(1500);
try {
await jwt.verifyAndDecodeJWT(token);
assertEquals(true, false, "Should have thrown a timeout");
} catch {
// expected
}
});