Skip to content

Commit

Permalink
get and assert claims in payload and header
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 772f73c commit 0fa7de4
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 9 deletions.
38 changes: 30 additions & 8 deletions src/jwt/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,18 @@ export class Jwt<
* Assert that there is a specific claim, possibly with value, in the Header.
*
*/
public assertClaimInHeader(claimKey: string, claimValue?: unknown) {
public assertClaimInHeader(
claimKey: keyof Header | string,
claimValue?: Header[typeof claimKey] | unknown
) {
this.assertHeader()

try {
this.assertClaimInObject(this.header!, claimKey, claimValue)
this.assertClaimInObject(
this.header!,
claimKey as string,
claimValue
)
} catch (e) {
if (e instanceof JwtError) {
e.message += ' of the header'
Expand All @@ -288,11 +295,18 @@ export class Jwt<
* Assert that there is a specific claim, possibly with value, in the Payload.
*
*/
public assertClaimInPayload(claimKey: string, claimValue?: unknown) {
public assertClaimInPayload(
claimKey: string | keyof Payload,
claimValue?: Payload[typeof claimKey] | unknown
) {
this.assertPayload()

try {
this.assertClaimInObject(this.payload!, claimKey, claimValue)
this.assertClaimInObject(
this.payload!,
claimKey as string,
claimValue
)
} catch (e) {
if (e instanceof JwtError) {
e.message += ' of the payload'
Expand Down Expand Up @@ -327,14 +341,22 @@ export class Jwt<
* @throws when the claim could not be found at any level
*
*/
public getClaimInPayload<T>(claimKey: string): T {
public getClaimInPayload<T>(claimKey: keyof Payload | string): T {
this.assertPayload()
return this.getClaimInObject<T>(this.payload!, claimKey)
return this.getClaimInObject<T>(this.payload!, claimKey as string)
}

public getClaimInHeader<T>(claimKey: string): T {
/**
*
* Get a claim within the payload.
*
* @throws when the payload is not defined
* @throws when the claim could not be found at any level
*
*/
public getClaimInHeader<T>(claimKey: keyof Header | string): T {
this.assertHeader()
return this.getClaimInObject<T>(this.header!, claimKey)
return this.getClaimInObject<T>(this.header!, claimKey as string)
}

private getClaimInObject<T>(
Expand Down
68 changes: 67 additions & 1 deletion tests/jwt.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { before, describe, it } from 'node:test'
import assert, { deepStrictEqual, strictEqual } from 'node:assert'
import assert, { deepStrictEqual, doesNotThrow, strictEqual } from 'node:assert'

import { prelude, signer, verifier } from './utils'

Expand Down Expand Up @@ -371,4 +371,70 @@ describe('JWT', async () => {
assert(!areRequiredClaimsIncluded)
})
})

describe('assert claims', () => {
it('assert top-level claims in payload and header', () => {
const jwt = new Jwt({
header: { alg: 'ES256' },
payload: { iss: 'https://example.org' },
signature: new Uint8Array(32).fill(42)
})

doesNotThrow(() =>
jwt.assertClaimInPayload('iss', 'https://example.org')
)

doesNotThrow(() => jwt.assertClaimInHeader('alg', 'ES256'))
})

it('assert nested-level claims in payload and header', () => {
const jwt = new Jwt({
header: { alg: 'ES256', nested: { nestedHeaderClaim: 'foo' } },
payload: {
iss: 'https://example.org',
nested: { nestedPayloadClaim: [1, 2, 3] }
},
signature: new Uint8Array(32).fill(42)
})

doesNotThrow(() =>
jwt.assertClaimInPayload('nestedPayloadClaim', [1, 2, 3])
)

doesNotThrow(() =>
jwt.assertClaimInHeader('nestedHeaderClaim', 'foo')
)
})
})

describe('get claims', () => {
it('get assert top-level claims in payload and header', () => {
const jwt = new Jwt({
header: { alg: 'ES256' },
payload: { iss: 'https://example.org' },
signature: new Uint8Array(32).fill(42)
})

deepStrictEqual(jwt.getClaimInHeader('alg'), 'ES256')
deepStrictEqual(jwt.getClaimInPayload('iss'), 'https://example.org')
})

it('assert nested-level claims in payload and header', () => {
const jwt = new Jwt({
header: { alg: 'ES256', nested: { nestedHeaderClaim: 'foo' } },
payload: {
iss: 'https://example.org',
nested: { nestedPayloadClaim: [1, 2, 3] }
},
signature: new Uint8Array(32).fill(42)
})

deepStrictEqual(
jwt.getClaimInPayload('nestedPayloadClaim'),
[1, 2, 3]
)

deepStrictEqual(jwt.getClaimInHeader('nestedHeaderClaim'), 'foo')
})
})
})

0 comments on commit 0fa7de4

Please sign in to comment.