Skip to content

Commit

Permalink
add algorithm to header and check during verification
Browse files Browse the repository at this point in the history
  • Loading branch information
tsndr committed Feb 21, 2024
1 parent cf24b34 commit e6baf7a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export type JwtHeader<T = {}> = {
* @default "JWT"
*/
typ?: string

/**
* Algorithm (default: `"HS256"`)
*
* @default "HS256"
*/
alg?: JwtAlgorithm
} & T

/**
Expand Down Expand Up @@ -196,7 +203,13 @@ export async function verify(token: string, secret: string | JsonWebKey | Crypto
if (!algorithm)
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 {
if (!payload)
Expand Down
3 changes: 3 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { sign } from './index'

console.log(await sign())

0 comments on commit e6baf7a

Please sign in to comment.