Skip to content

Commit

Permalink
feat: add payload generics to jose.decodeJwt
Browse files Browse the repository at this point in the history
fixes #604
  • Loading branch information
panva committed Nov 3, 2023
1 parent b4f3b61 commit 9de49e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
10 changes: 8 additions & 2 deletions docs/functions/util_decode_jwt.decodeJwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ Support from the community to continue maintaining and improving this module is

---

**decodeJwt**(`jwt`): [`JWTPayload`](../interfaces/types.JWTPayload.md)
**decodeJwt**<`PayloadType`\>(`jwt`): `PayloadType` & [`JWTPayload`](../interfaces/types.JWTPayload.md)

Decodes a signed JSON Web Token payload. This does not validate the JWT Claims Set types or
values. This does not validate the JWS Signature. For a proper Signed JWT Claims Set validation
and JWS signature verification use `jose.jwtVerify()`. For an encrypted JWT Claims Set validation
and JWE decryption use `jose.jwtDecrypt()`.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `PayloadType` | [`JWTPayload`](../interfaces/types.JWTPayload.md) |

#### Parameters

| Name | Type | Description |
Expand All @@ -21,7 +27,7 @@ and JWE decryption use `jose.jwtDecrypt()`.

#### Returns

[`JWTPayload`](../interfaces/types.JWTPayload.md)
`PayloadType` & [`JWTPayload`](../interfaces/types.JWTPayload.md)

**`Example`**

Expand Down
4 changes: 2 additions & 2 deletions src/util/decode_jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { JWTInvalid } from './errors.js'
*
* @param jwt JWT token in compact JWS serialization.
*/
export function decodeJwt(jwt: string) {
export function decodeJwt<PayloadType = JWTPayload>(jwt: string): PayloadType & JWTPayload {
if (typeof jwt !== 'string')
throw new JWTInvalid('JWTs must use Compact JWS serialization, JWT must be a string')

Expand All @@ -43,7 +43,7 @@ export function decodeJwt(jwt: string) {
throw new JWTInvalid('Failed to parse the decoded payload as JSON')
}

if (!isObject<JWTPayload>(result)) throw new JWTInvalid('Invalid JWT Claims Set')
if (!isObject<PayloadType & JWTPayload>(result)) throw new JWTInvalid('Invalid JWT Claims Set')

return result
}
13 changes: 13 additions & 0 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ expectType<KeyObject>(await lib.EmbeddedJWK())
expectType<'string'>(result.payload.foo)
}

{
const result = lib.decodeJwt<{ foo: 'string' }>('')
expectType<string | undefined>(result.iss)
expectType<unknown>(result.unknown)
expectType<'string'>(result.foo)
}

{
const result = await lib.jwtVerify('', new Uint8Array())
expectType<string | undefined>(result.payload.iss)
Expand All @@ -247,3 +254,9 @@ expectType<KeyObject>(await lib.EmbeddedJWK())
expectType<string | undefined>(result.payload.iss)
expectType<unknown>(result.payload.unknown)
}

{
const result = lib.decodeJwt('')
expectType<string | undefined>(result.iss)
expectType<unknown>(result.unknown)
}

0 comments on commit 9de49e2

Please sign in to comment.