Skip to content

Commit

Permalink
fix: don't run JSON.stringify on canonicalized data (#172)
Browse files Browse the repository at this point in the history
fixes #171
  • Loading branch information
yhuard authored May 19, 2021
1 parent 915b578 commit 5480bfc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/JWT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const DID_JSON = 'application/did+json'

function encodeSection(data: any, shouldCanonicalize: boolean = false): string {
if (shouldCanonicalize) {
return encodeBase64url(JSON.stringify(canonicalizeData(data)))
return encodeBase64url(canonicalizeData(data))
} else {
return encodeBase64url(JSON.stringify(data))
}
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/JWT.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,13 @@ describe('JWS', () => {
})

it('createJWS can canonicalize a JSON payload', async () => {
expect.assertions(2)
expect.assertions(3)
const payload = { z: 'z', a: 'a' }
const jws = await createJWS(payload, signer, {}, { canonicalize: true })
expect(jws).toMatchSnapshot()
expect(JSON.parse(decodeBase64url(jws.split('.')[1]))).toEqual(JSON.stringify({ a: 'a', z: 'z' }))
const parsedPayload = JSON.parse(decodeBase64url(jws.split('.')[1]))
expect(parsedPayload).toEqual(payload)
expect(JSON.stringify(parsedPayload)).toEqual(JSON.stringify({ a: 'a', z: 'z' }))
})

it('createJWS works with base64url payload', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/JWT.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`JWS createJWS can canonicalize a JSON payload 1`] = `"IntcImFsZ1wiOlwiRVMyNTZLXCJ9Ig.IntcImFcIjpcImFcIixcInpcIjpcInpcIn0i.aKyOmGo5Dx2VsvDZAAHMxNtgqBeI0-qij4OB2AsISFdGsnNDJNEptZkQjY497mzX1LdMPS84pzykqalPtMwqMQ"`;
exports[`JWS createJWS can canonicalize a JSON payload 1`] = `"eyJhbGciOiJFUzI1NksifQ.eyJhIjoiYSIsInoiOiJ6In0.FiH0l1yEU2PmMQnD17WhsqwaV9oyFsOkm-U_natKFqeo4OTA7OYheYmlRy1OpNYbtYhDyZj0w1NfCpgsgDMGjw"`;

exports[`JWS createJWS works with JSON payload 1`] = `"eyJhbGciOiJFUzI1NksifQ.eyJzb21lIjoiZGF0YSJ9.dblNz-7BVLknOFIBPmt5VTG0MDls_Q69WI8OfQuqNdUp4y50-b8Ubn0xujm1ijfmfqRunpks5TyWqgMsQkR_GQ"`;

Expand Down

0 comments on commit 5480bfc

Please sign in to comment.