Skip to content

Commit

Permalink
chore(types): Add missing input type for DecodedJwt and Updated input…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
msgadi authored Dec 13, 2024
1 parent e76ddee commit 08a24c5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ type VerifierCallback = (e: Error | TokenError | null, payload: any) => void
type DecodedJwt = {
header: Record<string, any>
payload: any
signature: string
signature: string,
input: string
}

type KeyFetcher =
Expand Down
4 changes: 2 additions & 2 deletions src/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function verify(
throw e
}

const { header, payload, signature } = decoded
const { header, payload, signature, input } = decoded
const cacheContext = {
cache,
token,
Expand All @@ -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)
}
Expand Down
13 changes: 13 additions & 0 deletions test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,16 @@ expectNotAssignable<JwtHeader>(signerOptionsNoAlg.header)

// Check all errors are typed correctly
expectType<TokenValidationErrorCode[]>(Object.values(TokenError.codes))

const decodedJwt: DecodedJwt = {
header: { alg: 'RS256', typ: 'JWT' },
payload: { sub: '12345', iss: 'iss' },
signature: 'abc123',
input: 'input'
}

expectType<DecodedJwt>(decodedJwt)
expectType<Record<string, any>>(decodedJwt.header)
expectType<any>(decodedJwt.payload)
expectType<string>(decodedJwt.signature)
expectType<string>(decodedJwt.input)
2 changes: 1 addition & 1 deletion test/verifier.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
)

Expand Down

0 comments on commit 08a24c5

Please sign in to comment.