Skip to content

Commit

Permalink
Feat/generic signer verifier (#529)
Browse files Browse the repository at this point in the history
* chore: fake timer

* feat: generic signer verifier
  • Loading branch information
ilteoood authored Dec 16, 2024
1 parent 4691667 commit 15f0f8d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ type KeyFetcher =

type SignerPayload = Bufferable | Record<string, any>

declare function SignerSync(payload: SignerPayload): string
declare function SignerAsync(payload: SignerPayload): Promise<string>
declare function SignerAsync(payload: SignerPayload, cb: SignerCallback): void
declare function SignerSync<T = SignerPayload>(payload: T): string
declare function SignerAsync<T = SignerPayload>(payload: T): Promise<string>
declare function SignerAsync<T = SignerPayload>(payload: T, cb: SignerCallback): void

declare function VerifierSync(token: Bufferable): any
declare function VerifierAsync(token: Bufferable): Promise<any>
declare function VerifierAsync(token: Bufferable, cb: VerifierCallback): void
declare function VerifierSync<T = Bufferable>(token: T): any
declare function VerifierAsync<T = Bufferable>(token: T): Promise<any>
declare function VerifierAsync<T = Bufferable>(token: T, cb: VerifierCallback): void

export interface JwtHeader extends Record<string, any> {
alg: string | Algorithm
Expand Down Expand Up @@ -147,10 +147,10 @@ export interface PrivateKey {
passphrase: string | undefined
}

export function createSigner(
export function createSigner<T = SignerPayload>(
options?: Partial<SignerOptions & { key: Bufferable | PrivateKey }>
): typeof SignerSync
export function createSigner(options?: Partial<SignerOptions & { key: KeyFetcher }>): typeof SignerAsync
): typeof SignerSync<T>
export function createSigner<T = SignerPayload>(options?: Partial<SignerOptions & { key: KeyFetcher }>): typeof SignerAsync<T>
export function createDecoder(options?: Partial<DecoderOptions>): (token: Bufferable) => any
export function createVerifier(options?: Partial<VerifierOptions & { key: Bufferable }>): typeof VerifierSync
export function createVerifier(options?: Partial<VerifierOptions & { key: KeyFetcher }>): typeof VerifierAsync
export function createVerifier<T = Bufferable>(options?: Partial<VerifierOptions & { key: Bufferable }>): typeof VerifierSync<T>
export function createVerifier<T = Bufferable>(options?: Partial<VerifierOptions & { key: KeyFetcher }>): typeof VerifierAsync<T>
18 changes: 17 additions & 1 deletion test/types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
/* eslint-disable @typescript-eslint/no-unused-vars */

import { expectAssignable, expectNotAssignable, expectType } from 'tsd'
import {
createDecoder,
createSigner,
Expand All @@ -10,7 +11,6 @@ import {
TokenError,
TokenValidationErrorCode
} from '..'
import { expectAssignable, expectNotAssignable, expectType } from 'tsd'

// Signing
// Buffer key, both async/callback styles
Expand Down Expand Up @@ -44,6 +44,14 @@ createSigner({
algorithm: 'RS256'
})

// Generic signer
const signer = createSigner<Record<string, number>>({
expiresIn: '10min',
key: Buffer.from('KEY'),
algorithm: 'RS256'
})
signer({ key: 1 })

// Decoding
const decoder = createDecoder({ checkTyp: 'true' })
decoder('FOO')
Expand Down Expand Up @@ -77,6 +85,14 @@ createVerifier({
}
})('456').then(console.log, console.log)

// Generic verifier
createVerifier<Record<string, number>>({
key: 'KEY',
algorithms: ['RS256'],
requiredClaims: ['aud'],
checkTyp: 'JWT'
})({ key: 1 }).then(console.log, console.log)

// Errors
const wrapped = TokenError.wrap(new Error('ORIGINAL'), 'FAST_JWT_INVALID_TYPE', 'MESSAGE')
wrapped.code === 'FAST_JWT_INVALID_TYPE'
Expand Down

0 comments on commit 15f0f8d

Please sign in to comment.