Implement JWT

This commit is contained in:
2024-05-21 16:00:20 +02:00
parent d845ae1391
commit 4b94352d8b
3 changed files with 141 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
import {beforeAll, expect, test} from 'bun:test'
import {JWTcontext, type JWTalgorithm} from '../src/jwt'
const algs: JWTalgorithm[] = ["HS256", "HS512", "ES256", "ES512", "EdDSA"]
const contexts: Map<JWTalgorithm, JWTcontext> = new Map()
beforeAll(async () => {
for (const alg of algs) {
const key = await JWTcontext.gen_key(alg as JWTalgorithm)
expect(key).not.toBeUndefined()
const context = new JWTcontext(key, alg as JWTalgorithm)
contexts.set(alg as JWTalgorithm, context)
}
})
test('Base case', async () => {
let payload = {
yeet: "yaat",
lol: "yes"
}
for (const context of contexts.values()) {
const jwt = await context.sign(payload, true, "2 days", "pascal", "server")
const decoded = jwt.payload
expect(decoded).toEqual(payload)
const verified = (await context.verify(jwt, "pascal", "server")).expect("Should verify the JWT")
expect(verified).toEqual(payload)
}
})
test.todo("Multiple audience can be verified")
test.todo("Multiple issuer can be verified")
test.todo("Wrong audience is rejected")
test.todo("Wrong issuer is rejected")
test.todo("Expired JWT is rejected")
test.todo("Wrong key won't decrypt")
test.todo("tampered JWT are rejected (test 3 parts) (TODO : decode ?)")