1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
e6baf7ab75 add algorithm to header and check during verification 2024-02-21 21:04:03 +01:00
cf24b34f63 clean up 2024-02-21 21:03:44 +01:00
2 changed files with 23 additions and 8 deletions

View File

@@ -1,10 +1,10 @@
import { import {
textToArrayBuffer, textToArrayBuffer,
arrayBufferToBase64Url, arrayBufferToBase64Url,
base64UrlToArrayBuffer, base64UrlToArrayBuffer,
textToBase64Url, textToBase64Url,
importKey, importKey,
decodePayload decodePayload
} from "./utils" } from "./utils"
if (typeof crypto === 'undefined' || !crypto.subtle) if (typeof crypto === 'undefined' || !crypto.subtle)
@@ -34,6 +34,13 @@ export type JwtHeader<T = {}> = {
* @default "JWT" * @default "JWT"
*/ */
typ?: string typ?: string
/**
* Algorithm (default: `"HS256"`)
*
* @default "HS256"
*/
alg?: JwtAlgorithm
} & T } & T
/** /**
@@ -196,7 +203,13 @@ export async function verify(token: string, secret: string | JsonWebKey | Crypto
if (!algorithm) if (!algorithm)
throw new Error('algorithm not found') throw new Error('algorithm not found')
const { payload } = decode(token) const { header, payload } = decode(token)
if (header?.alg !== options.algorithm) {
if (options.throwError)
throw new Error('ALG_MISMATCH')
return false
}
try { try {
if (!payload) if (!payload)
@@ -214,7 +227,6 @@ export async function verify(token: string, secret: string | JsonWebKey | Crypto
} catch(err) { } catch(err) {
if (options.throwError) if (options.throwError)
throw err throw err
return false return false
} }
} }

3
src/test.ts Normal file
View File

@@ -0,0 +1,3 @@
import { sign } from './index'
console.log(await sign())