Skip to content

Commit

Permalink
added tests for sdjwt utility methods
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 0fa7de4 commit 2d711bc
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/jwt/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class Jwt<
*
*/
public addHeaderClaim(
item: keyof Header,
item: keyof Header | string,
value: Header[typeof item] | unknown
): ReturnJwtWithHeader<Header, Payload, this> {
this.header ??= {} as Header
Expand Down Expand Up @@ -177,7 +177,7 @@ export class Jwt<
*
*/
public addPayloadClaim(
item: keyof Payload,
item: keyof Payload | string,
value: Payload[typeof item] | unknown
): ReturnJwtWithPayload<Header, Payload, this> {
this.payload ??= {} as Payload
Expand Down
10 changes: 5 additions & 5 deletions src/sdJwt/sdJwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class SdJwt<
return this as ReturnSdJwtWithPayload<Header, Payload, this>
}

private addHasherAlgorithmToPayload() {
public addHasherAlgorithmToPayload() {
this.assertHashAndAlgorithm()

this.addPayloadClaim('_sd_alg', this.hasherAndAlgorithm!.algorithm)
Expand Down Expand Up @@ -358,12 +358,12 @@ export class SdJwt<
}

public checkHasher(expectedHasher: HasherAlgorithm | string): boolean {
this.assertPayload()

try {
this.assertClaimInPayload('_sd_alg', expectedHasher)
this.assertPayload()
this.assertClaimInPayload('_sd_alg', expectedHasher.toString())
return true
} catch {
} catch (e) {
console.error(e)
return false
}
}
Expand Down
69 changes: 68 additions & 1 deletion tests/sdJwt.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { before, describe, it } from 'node:test'
import assert, { deepStrictEqual, rejects, strictEqual } from 'node:assert'
import assert, {
deepStrictEqual,
doesNotThrow,
rejects,
strictEqual,
throws
} from 'node:assert'

import {
hasherAndAlgorithm,
Expand All @@ -20,6 +26,67 @@ import {
describe('sd-jwt', async () => {
before(prelude)

describe('utility', () => {
it('Check correct hashing algorithm', () => {
const sdJwt = new SdJwt({
payload: { some: 'payload' }
})
.withHasher(hasherAndAlgorithm)
.addHasherAlgorithmToPayload()

assert(sdJwt.checkHasher(HasherAlgorithm.Sha256))
})

it('Check incorrect hashing algorithm', () => {
const sdJwt = new SdJwt({
payload: { some: 'payload' }
})
.withHasher(hasherAndAlgorithm)
.addHasherAlgorithmToPayload()

assert(!sdJwt.checkHasher(HasherAlgorithm.Sha3_256))
})
})

describe('assert in disclosure frame', () => {
it('assert claim in disclosure frame', () => {
const sdJwt = new SdJwt(
{ header: { a: 'b' }, payload: { c: 'd' } },
{ disclosureFrame: { c: true } }
)

doesNotThrow(() => sdJwt.assertClaimInDisclosureFrame('c'))
})

it('assert claims in disclosure frame', () => {
const sdJwt = new SdJwt(
{ header: { a: 'b' }, payload: { c: 'd', e: 'f', g: 'h' } },
{ disclosureFrame: { c: true, g: true } }
)

doesNotThrow(() => sdJwt.assertClaimInDisclosureFrame('c'))
doesNotThrow(() => sdJwt.assertClaimInDisclosureFrame('g'))
})

it('assert claim not in disclosure frame', () => {
const sdJwt = new SdJwt(
{ header: { a: 'b' }, payload: { c: 'd' } },
{ disclosureFrame: { c: true } }
)

throws(() => sdJwt.assertClaimInDisclosureFrame('z'))
})

it('assert claims not in disclosure frame', () => {
const sdJwt = new SdJwt(
{ header: { a: 'b' }, payload: { c: 'd', e: 'f', g: 'h' } },
{ disclosureFrame: { c: true, g: true } }
)

throws(() => sdJwt.assertClaimInDisclosureFrame('e'))
})
})

describe('JWT without selective disclosure', async () => {
it('should create a simple jwt', async () => {
const sdJwt = await new SdJwt({
Expand Down

0 comments on commit 2d711bc

Please sign in to comment.