jwt.ts
This file
Path
src/utils/jwt/jwt.ts
292 lines 路 8.3 KiB 路 TypeScript
Last changed by
fix(jwt): throw JwtTokenInvalid when the signature is not valid base64url (#5379)Neil Daquioagbefore adoption52febbc
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293/** * @module * JSON Web Token (JWT) * https://datatracker.ietf.org/doc/html/rfc7519 */
import { decodeBase64Url, encodeBase64Url } from '../../utils/encode'import { AlgorithmTypes } from './jwa'import type { AsymmetricAlgorithm, SignatureAlgorithm, SymmetricAlgorithm } from './jwa'import { signing, verifying } from './jws'import type { HonoJsonWebKey, SignatureKey } from './jws'import { JwtAlgorithmMismatch, JwtAlgorithmNotAllowed, JwtAlgorithmRequired, JwtHeaderInvalid, JwtHeaderRequiresKid, JwtPayloadRequiresAud, JwtSymmetricAlgorithmNotAllowed, JwtTokenAudience, JwtTokenExpired, JwtTokenInvalid, JwtTokenIssuedAt, JwtTokenIssuer, JwtTokenNotBefore, JwtTokenSignatureMismatched,} from './types'import type { JWTPayload } from './types'import { utf8Decoder, utf8Encoder } from './utf8'
const encodeJwtPart = (part: unknown): string => encodeBase64Url(utf8Encoder.encode(JSON.stringify(part)).buffer).replace(/=/g, '')const encodeSignaturePart = (buf: ArrayBufferLike): string => encodeBase64Url(buf).replace(/=/g, '')
const decodeJwtPart = (part: string): TokenHeader | JWTPayload | undefined => JSON.parse(utf8Decoder.decode(decodeBase64Url(part)))
export interface TokenHeader { alg: SignatureAlgorithm typ?: 'JWT' kid?: string}
export function isTokenHeader(obj: unknown): obj is TokenHeader { if (typeof obj === 'object' && obj !== null) { const objWithAlg = obj as { [key: string]: unknown } return ( 'alg' in objWithAlg && Object.values(AlgorithmTypes).includes(objWithAlg.alg as AlgorithmTypes) && (!('typ' in objWithAlg) || objWithAlg.typ === 'JWT') ) } return false}
export const sign = async ( payload: JWTPayload, privateKey: SignatureKey, alg: SignatureAlgorithm = 'HS256'): Promise<string> => { const encodedPayload = encodeJwtPart(payload) let encodedHeader if (typeof privateKey === 'object' && 'alg' in privateKey) { alg = privateKey.alg as SignatureAlgorithm encodedHeader = encodeJwtPart({ alg, typ: 'JWT', kid: privateKey.kid }) } else { encodedHeader = encodeJwtPart({ alg, typ: 'JWT' }) }
const partialToken = `${encodedHeader}.${encodedPayload}`
const signaturePart = await signing(privateKey, alg, utf8Encoder.encode(partialToken)) const signature = encodeSignaturePart(signaturePart)
return `${partialToken}.${signature}`}
export type VerifyOptions = { /** The expected issuer used for verifying the token */ iss?: string | RegExp /** Verify the `nbf` claim (default: `true`) */ nbf?: boolean /** Verify the `exp` claim (default: `true`) */ exp?: boolean /** Verify the `iat` claim (default: `true`) */ iat?: boolean /** Acceptable audience(s) for the token */ aud?: string | string[] | RegExp}
export type VerifyOptionsWithAlg = { /** The algorithm used for decoding the token */ alg: SignatureAlgorithm} & VerifyOptions
export const verify = async ( token: string, publicKey: SignatureKey, algOrOptions: SignatureAlgorithm | VerifyOptionsWithAlg): Promise<JWTPayload> => { if (!algOrOptions) { throw new JwtAlgorithmRequired() }
const { alg, iss, nbf = true, exp = true, iat = true, aud, } = typeof algOrOptions === 'string' ? { alg: algOrOptions } : algOrOptions
if (!alg) { throw new JwtAlgorithmRequired() }
const tokenParts = token.split('.') if (tokenParts.length !== 3) { throw new JwtTokenInvalid(token) }
const { header, payload } = decode(token) if (!isTokenHeader(header)) { throw new JwtHeaderInvalid(header) } if (header.alg !== alg) { throw new JwtAlgorithmMismatch(alg, header.alg) } const now = Math.floor(Date.now() / 1000) if (nbf && payload.nbf !== undefined) { if (typeof payload.nbf !== 'number' || !Number.isFinite(payload.nbf) || payload.nbf > now) { throw new JwtTokenNotBefore(token) } } if (exp && payload.exp !== undefined) { if (typeof payload.exp !== 'number' || !Number.isFinite(payload.exp) || payload.exp <= now) { throw new JwtTokenExpired(token) } } if (iat && payload.iat !== undefined) { if (typeof payload.iat !== 'number' || !Number.isFinite(payload.iat) || now < payload.iat) { throw new JwtTokenIssuedAt(now, payload.iat) } } if (iss) { if (!payload.iss) { throw new JwtTokenIssuer(iss, null) } if (typeof iss === 'string' && payload.iss !== iss) { throw new JwtTokenIssuer(iss, payload.iss) } if (iss instanceof RegExp && !iss.test(payload.iss)) { throw new JwtTokenIssuer(iss, payload.iss) } }
if (aud) { if (!payload.aud) { throw new JwtPayloadRequiresAud(payload) }
const audiences = Array.isArray(payload.aud) ? payload.aud : [payload.aud] const matched = audiences.some((payloadAud): boolean => aud instanceof RegExp ? aud.test(payloadAud) : typeof aud === 'string' ? payloadAud === aud : Array.isArray(aud) && aud.includes(payloadAud) ) if (!matched) { throw new JwtTokenAudience(aud, payload.aud) } }
const headerPayload = token.substring(0, token.lastIndexOf('.')) let signature: Uint8Array<ArrayBuffer> try { signature = decodeBase64Url(tokenParts[2]) } catch { throw new JwtTokenInvalid(token) } const verified = await verifying(publicKey, alg, signature, utf8Encoder.encode(headerPayload)) if (!verified) { throw new JwtTokenSignatureMismatched(token) }
return payload}
// Symmetric algorithms that are not allowed for JWK verificationconst symmetricAlgorithms: SymmetricAlgorithm[] = [ AlgorithmTypes.HS256, AlgorithmTypes.HS384, AlgorithmTypes.HS512,]
export const verifyWithJwks = async ( token: string, options: { keys?: HonoJsonWebKey[] jwks_uri?: string verification?: VerifyOptions allowedAlgorithms: readonly AsymmetricAlgorithm[] }, init?: RequestInit): Promise<JWTPayload> => { const verifyOpts = options.verification || {}
const header = decodeHeader(token)
if (!isTokenHeader(header)) { throw new JwtHeaderInvalid(header) } if (!header.kid) { throw new JwtHeaderRequiresKid(header) }
// Reject symmetric algorithms (HS256, HS384, HS512) to prevent algorithm confusion attacks if (symmetricAlgorithms.includes(header.alg as SymmetricAlgorithm)) { throw new JwtSymmetricAlgorithmNotAllowed(header.alg) }
// Validate against allowed algorithms if (!options.allowedAlgorithms.includes(header.alg as AsymmetricAlgorithm)) { throw new JwtAlgorithmNotAllowed(header.alg, options.allowedAlgorithms) }
let verifyKeys = options.keys ? [...options.keys] : undefined
if (options.jwks_uri) { const response = await fetch(options.jwks_uri, init) if (!response.ok) { throw new Error(`failed to fetch JWKS from ${options.jwks_uri}`) } const data = (await response.json()) as { keys?: JsonWebKey[] } if (!data.keys) { throw new Error('invalid JWKS response. "keys" field is missing') } if (!Array.isArray(data.keys)) { throw new Error('invalid JWKS response. "keys" field is not an array') } verifyKeys ??= [] verifyKeys.push(...(data.keys as HonoJsonWebKey[])) } else if (!verifyKeys) { throw new Error('verifyWithJwks requires options for either "keys" or "jwks_uri" or both') }
const matchingKey = verifyKeys.find((key) => key.kid === header.kid) if (!matchingKey) { throw new JwtTokenInvalid(token) }
// Verify that JWK's alg matches JWT header's alg when JWK has alg field if (matchingKey.alg && matchingKey.alg !== header.alg) { throw new JwtAlgorithmMismatch(matchingKey.alg, header.alg) }
return await verify(token, matchingKey, { alg: header.alg, ...verifyOpts, })}
export const decode = (token: string): { header: TokenHeader; payload: JWTPayload } => { const parts = token.split('.') if (parts.length !== 3) { throw new JwtTokenInvalid(token) } try { const header = decodeJwtPart(parts[0]) as TokenHeader const payload = decodeJwtPart(parts[1]) as JWTPayload return { header, payload, } } catch { throw new JwtTokenInvalid(token) }}
export const decodeHeader = (token: string): TokenHeader => { const parts = token.split('.') if (parts.length !== 3) { throw new JwtTokenInvalid(token) } try { return decodeJwtPart(parts[0]) as TokenHeader } catch { throw new JwtTokenInvalid(token) }}