Skip to content

Commit

Permalink
get claim from jwt
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht committed Oct 31, 2023
1 parent 839b119 commit 178ce01
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/jwt/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,29 @@ export class Jwt<
}
}

public getClaimInPayload<T>(claimKey: string): T {
this.assertPayload()
return this.getClaimInObject<T>(this.payload!, claimKey)
}

public getClaimInHeader<T>(claimKey: string): T {
this.assertHeader()
return this.getClaimInObject<T>(this.header!, claimKey)
}

private getClaimInObject<T>(
object: Record<string, unknown>,
claimKey: string
): T {
const value = getValueByKeyAnyLevel<T>(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.
Expand Down

0 comments on commit 178ce01

Please sign in to comment.