From 08a24c575f58fee858204f63e26276c29a1da0a2 Mon Sep 17 00:00:00 2001 From: Mohammed Gadi <9037086+msgadi@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:41:01 +0530 Subject: [PATCH] chore(types): Add missing input type for DecodedJwt and Updated input field to return decoded 'input' (#524) * chore(types): add missing input type for DecodedJwt * adding test case for type DecodedJwt * fixed input as input instead of whole token. * fix: test typo fix. --- src/index.d.ts | 3 ++- src/verifier.js | 4 ++-- test/types.spec.ts | 13 +++++++++++++ test/verifier.spec.js | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 82c15f6..20d278d 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -65,7 +65,8 @@ type VerifierCallback = (e: Error | TokenError | null, payload: any) => void type DecodedJwt = { header: Record payload: any - signature: string + signature: string, + input: string } type KeyFetcher = diff --git a/src/verifier.js b/src/verifier.js index 92e92a2..1d9a213 100644 --- a/src/verifier.js +++ b/src/verifier.js @@ -288,7 +288,7 @@ function verify( throw e } - const { header, payload, signature } = decoded + const { header, payload, signature, input } = decoded const cacheContext = { cache, token, @@ -309,7 +309,7 @@ function verify( try { verifyToken(key, decoded, validationContext) - return cacheSet(cacheContext, complete ? { header, payload, signature, input: token } : payload) + return cacheSet(cacheContext, complete ? { header, payload, signature, input } : payload) } catch (e) { throw cacheSet(cacheContext, e) } diff --git a/test/types.spec.ts b/test/types.spec.ts index a7d7d6f..a7c33fa 100644 --- a/test/types.spec.ts +++ b/test/types.spec.ts @@ -137,3 +137,16 @@ expectNotAssignable(signerOptionsNoAlg.header) // Check all errors are typed correctly expectType(Object.values(TokenError.codes)) + +const decodedJwt: DecodedJwt = { + header: { alg: 'RS256', typ: 'JWT' }, + payload: { sub: '12345', iss: 'iss' }, + signature: 'abc123', + input: 'input' +} + +expectType(decodedJwt) +expectType>(decodedJwt.header) +expectType(decodedJwt.payload) +expectType(decodedJwt.signature) +expectType(decodedJwt.input) diff --git a/test/verifier.spec.js b/test/verifier.spec.js index 51892a7..c898999 100644 --- a/test/verifier.spec.js +++ b/test/verifier.spec.js @@ -117,7 +117,7 @@ test('it correctly verifies a token - sync', t => { header: { typ: 'JWT', alg: 'HS256' }, payload: { a: 1 }, signature: '57TF7smP9XDhIexBqPC-F1toZReYZLWb_YRU5tv0sxM', - input: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoxfQ.57TF7smP9XDhIexBqPC-F1toZReYZLWb_YRU5tv0sxM' + input: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoxfQ' } )