From 686318e2326e2716cd536b1a696c5e8c6ccc49e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Rib=C3=B3?= Date: Thu, 8 Feb 2024 12:35:26 +0100 Subject: [PATCH] fix: Recover JTI field correctly. Allowing to regenerate the original JWT string (#171) --- src/pollux/models/JWTVerifiableCredential.ts | 7 +++---- tests/pollux/Pollux.test.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/pollux/models/JWTVerifiableCredential.ts b/src/pollux/models/JWTVerifiableCredential.ts index 95a17818b..83ef16215 100644 --- a/src/pollux/models/JWTVerifiableCredential.ts +++ b/src/pollux/models/JWTVerifiableCredential.ts @@ -20,8 +20,7 @@ export const JWTVerifiableCredentialRecoveryId = "jwt+credential"; export class JWTCredential extends Credential - implements ProvableCredential, StorableCredential -{ + implements ProvableCredential, StorableCredential { public credentialType = CredentialType.JWT; public recoveryId = JWTVerifiableCredentialRecoveryId; public properties = new Map(); @@ -41,7 +40,7 @@ export class JWTCredential public readonly originalJWTString?: string ) { super(); - + this.properties.set(JWTVerifiableCredentialProperties.jti, jti); this.properties.set(JWTVerifiableCredentialProperties.iss, iss); this.properties.set(JWTVerifiableCredentialProperties.sub, sub); this.properties.set(JWTVerifiableCredentialProperties.nbf, nbf); @@ -143,7 +142,7 @@ export class JWTCredential const credentialData = JSON.stringify(Object.fromEntries(this.properties)); return { - id: this.getProperty(JWTVerifiableCredentialProperties.jti), + id: this.jti || this.getProperty(JWTVerifiableCredentialProperties.jti), recoveryId: this.recoveryId, credentialData: credentialData, issuer: this.getProperty(JWTVerifiableCredentialProperties.iss), diff --git a/tests/pollux/Pollux.test.ts b/tests/pollux/Pollux.test.ts index 1791cf607..f3e461563 100644 --- a/tests/pollux/Pollux.test.ts +++ b/tests/pollux/Pollux.test.ts @@ -356,6 +356,21 @@ describe("Pollux", () => { expect(result.issuanceDate).to.equal(issuanceDate); expect(result.expirationDate).to.equal(expirationDate); }); + + it("should be able to recover JTI from a toStorable", async () => { + const result = await pollux.parseCredential( + Buffer.from(Fixtures.Credentials.JWT.credentialPayloadEncoded), + { + type: CredentialType.JWT, + } + ) as JWTCredential; + + const storable = result.toStorable(); + + expect(storable).to.be.an("object"); + expect(storable).to.have.property("id"); + expect(storable.id).to.equal(Fixtures.Credentials.JWT.credentialPayloadEncoded); + }); }); });