From 15f0f8d0f36c05659b5add9fc97d6d95e52bcf55 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Mon, 16 Dec 2024 18:53:01 +0100 Subject: [PATCH] Feat/generic signer verifier (#529) * chore: fake timer * feat: generic signer verifier --- src/index.d.ts | 22 +++++++++++----------- test/types.spec.ts | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 573ad58..7df6318 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -77,13 +77,13 @@ type KeyFetcher = type SignerPayload = Bufferable | Record -declare function SignerSync(payload: SignerPayload): string -declare function SignerAsync(payload: SignerPayload): Promise -declare function SignerAsync(payload: SignerPayload, cb: SignerCallback): void +declare function SignerSync(payload: T): string +declare function SignerAsync(payload: T): Promise +declare function SignerAsync(payload: T, cb: SignerCallback): void -declare function VerifierSync(token: Bufferable): any -declare function VerifierAsync(token: Bufferable): Promise -declare function VerifierAsync(token: Bufferable, cb: VerifierCallback): void +declare function VerifierSync(token: T): any +declare function VerifierAsync(token: T): Promise +declare function VerifierAsync(token: T, cb: VerifierCallback): void export interface JwtHeader extends Record { alg: string | Algorithm @@ -147,10 +147,10 @@ export interface PrivateKey { passphrase: string | undefined } -export function createSigner( +export function createSigner( options?: Partial -): typeof SignerSync -export function createSigner(options?: Partial): typeof SignerAsync +): typeof SignerSync +export function createSigner(options?: Partial): typeof SignerAsync export function createDecoder(options?: Partial): (token: Bufferable) => any -export function createVerifier(options?: Partial): typeof VerifierSync -export function createVerifier(options?: Partial): typeof VerifierAsync +export function createVerifier(options?: Partial): typeof VerifierSync +export function createVerifier(options?: Partial): typeof VerifierAsync diff --git a/test/types.spec.ts b/test/types.spec.ts index f164bab..6bf5178 100644 --- a/test/types.spec.ts +++ b/test/types.spec.ts @@ -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, @@ -10,7 +11,6 @@ import { TokenError, TokenValidationErrorCode } from '..' -import { expectAssignable, expectNotAssignable, expectType } from 'tsd' // Signing // Buffer key, both async/callback styles @@ -44,6 +44,14 @@ createSigner({ algorithm: 'RS256' }) +// Generic signer +const signer = createSigner>({ + expiresIn: '10min', + key: Buffer.from('KEY'), + algorithm: 'RS256' +}) +signer({ key: 1 }) + // Decoding const decoder = createDecoder({ checkTyp: 'true' }) decoder('FOO') @@ -77,6 +85,14 @@ createVerifier({ } })('456').then(console.log, console.log) +// Generic verifier +createVerifier>({ + 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'