This repository has been archived by the owner on Jun 27, 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.
refactor(jwt): accept secret as first arg
- Loading branch information
1 parent
ac6e82c
commit 3a64b86
Showing
3 changed files
with
62 additions
and
16 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 @@ | ||
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license. | ||
import { cheetah } from '../cheetah.ts' | ||
import { createKey, importKey } from '../jwt.ts' | ||
import { jwt } from '../mod.ts' | ||
import { assertEquals, assertInstanceOf } from '../test/deps.ts' | ||
|
||
Deno.test('jwt', async () => { | ||
const key = await createKey() | ||
const cryptoKey = await importKey(key) | ||
|
||
assertInstanceOf(cryptoKey, CryptoKey) | ||
|
||
const token = await jwt.sign(key, { example: 'object' }) | ||
|
||
assertEquals(await jwt.verify(await createKey(), token) === undefined, true) | ||
assertEquals(await jwt.verify(key, token) !== undefined, true) | ||
assertEquals(await jwt.verify(cryptoKey, token) !== undefined, true) | ||
|
||
Deno.env.set('jwt_secret', key) | ||
|
||
const app = new cheetah() | ||
|
||
app.get('/one', async (c) => { | ||
assertEquals(await jwt.verify(c, token) !== undefined, true) | ||
}) | ||
|
||
await app.fetch(new Request('http://localhost/one')) | ||
|
||
Deno.env.delete('jwt_secret') | ||
Deno.env.set('JWT_SECRET', key) | ||
|
||
app.get('/two', async (c) => { | ||
assertEquals(await jwt.verify(c, token) !== undefined, true) | ||
}) | ||
|
||
await app.fetch(new Request('http://localhost/two')) | ||
}) |
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