Skip to content

Commit

Permalink
Extend JwtHeader to allow additional fields (#476)
Browse files Browse the repository at this point in the history
* Extend JwtHeader to allow additional fields

* remove comment

* Change type DecodedJwt.header into a Record
  • Loading branch information
Akallabet authored Sep 5, 2024
1 parent 035d37a commit f2c5b54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ type SignerCallback = (e: Error | TokenError | null, token: string) => void
type VerifierCallback = (e: Error | TokenError | null, payload: any) => void

type DecodedJwt = {
header: { [key: string]: any },
payload: any,
header: Record<string, any>
payload: any
signature: string
}

Expand All @@ -80,7 +80,7 @@ declare function VerifierSync(token: string | Buffer): any
declare function VerifierAsync(token: string | Buffer): Promise<any>
declare function VerifierAsync(token: string | Buffer, cb: object): void

export interface JwtHeader {
export interface JwtHeader extends Record<string, any> {
alg: string | Algorithm
typ?: string | undefined
cty?: string | undefined
Expand Down
28 changes: 27 additions & 1 deletion test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-empty-function */

import { createDecoder, createSigner, createVerifier, DecodedJwt, JwtHeader, TokenError, TokenValidationErrorCode } from '..'
import {
createDecoder,
createSigner,
createVerifier,
DecodedJwt,
JwtHeader,
TokenError,
TokenValidationErrorCode
} from '..'
import { expectAssignable, expectNotAssignable, expectType } from 'tsd'

// Signing
Expand Down Expand Up @@ -94,6 +102,24 @@ const signerOptions = {
}
expectAssignable<JwtHeader>(signerOptions.header)

const signerOptionsCustomHeaders = {
header: {
alg: 'RS256',
typ: '',
cty: '',
crit: [''],
kid: '',
jku: '',
x5u: '',
'x5t#S256': '',
x5t: '',
x5c: '',
customClaim: 'my-custom-claim',
customClaim2: 'my-custom-claim2'
}
}
expectAssignable<JwtHeader>(signerOptionsCustomHeaders.header)

const signerOptionsNoAlg = {
header: {
typ: '',
Expand Down

0 comments on commit f2c5b54

Please sign in to comment.