Skip to content

Commit 28be467

Browse files
committed
chore: asymmetric jwt sign test
1 parent 4c12c81 commit 28be467

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

bun.lockb

-1.1 KB
Binary file not shown.

test/index.test.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Elysia, t } from 'elysia'
2+
import { importJWK } from 'jose'
23
import { jwt } from '../src'
34

45
import { describe, expect, it } from 'bun:test'
56

6-
const req = (path: string) => new Request(`http://localhost${path}`)
77
const post = (path: string, body = {}) =>
88
new Request(`http://localhost${path}`, {
99
method: 'POST',
@@ -14,8 +14,22 @@ const post = (path: string, body = {}) =>
1414
})
1515

1616
describe('Static Plugin', () => {
17+
async function signTest() {
18+
const name = 'Shirokami'
19+
20+
const _sign = post('/sign', { name })
21+
await _sign.text()
22+
23+
const _verified = post('/verify', { name })
24+
const signed = (await _verified.json()) as {
25+
name: string
26+
}
27+
28+
expect(name).toBe(signed.name)
29+
}
30+
1731
it('sign JWT', async () => {
18-
const app = new Elysia()
32+
new Elysia()
1933
.use(
2034
jwt({
2135
name: 'jwt',
@@ -31,16 +45,31 @@ describe('Static Plugin', () => {
3145
body: t.Object({ name: t.String() })
3246
})
3347

34-
const name = 'Shirokami'
35-
36-
const _sign = post('/sign', { name })
37-
const token = await _sign.text()
48+
await signTest()
49+
})
50+
it('sign JWT (asymmetric)', async () => {
51+
const crv = 'Ed25519'
52+
const d = 'N3cOzsFZwiIbtNiBYQP9bcbcTIdkITC8a4iRslrbW7Q'
53+
const x = 'RjnTe-mqZcVls6SQ5CgW0X__jRaa-Quj5HBDREzVLhc'
54+
const kty = 'OKP'
3855

39-
const _verified = post('/verify', { name })
40-
const signed = (await _verified.json()) as {
41-
name: string
42-
}
56+
new Elysia()
57+
.use(
58+
jwt({
59+
name: 'jwt',
60+
privateKey: await importJWK({ crv, d, x, kty }, 'EdDSA'),
61+
publicKey: await importJWK({ crv, x, kty }, 'EdDSA')
62+
})
63+
)
64+
.post('/validate', ({ jwt, body }) => jwt.sign(body), {
65+
body: t.Object({
66+
name: t.String()
67+
})
68+
})
69+
.post('/validate', ({ jwt, body: { name } }) => jwt.verify(name), {
70+
body: t.Object({ name: t.String() })
71+
})
4372

44-
expect(name).toBe(signed.name)
45-
})
73+
await signTest()
74+
})
4675
})

0 commit comments

Comments
 (0)