diff --git a/src/jwt/jwt.ts b/src/jwt/jwt.ts index 5506476..9a5ceaf 100644 --- a/src/jwt/jwt.ts +++ b/src/jwt/jwt.ts @@ -319,6 +319,29 @@ export class Jwt< } } + public getClaimInPayload(claimKey: string): T { + this.assertPayload() + return this.getClaimInObject(this.payload!, claimKey) + } + + public getClaimInHeader(claimKey: string): T { + this.assertHeader() + return this.getClaimInObject(this.header!, claimKey) + } + + private getClaimInObject( + object: Record, + claimKey: string + ): T { + const value = getValueByKeyAnyLevel(object, claimKey) + + if (!value) { + throw new JwtError(`Claim key '${claimKey}' not found in any level`) + } + + return value + } + /** * * Returns a string of what needs to be signed.