Skip to content

Commit

Permalink
fix: Recover JTI field correctly. Allowing to regenerate the original…
Browse files Browse the repository at this point in the history
… JWT string (#171)

Signed-off-by: Francisco Javier Ribó Labrador <[email protected]>
Signed-off-by: Francisco Javier Ribo Labrador <[email protected]>
  • Loading branch information
elribonazo committed May 2, 2024
1 parent 09cab81 commit 9129387
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/pollux/models/JWTVerifiableCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JWTVerifiableCredentialProperties, any>();
Expand All @@ -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);
Expand Down Expand Up @@ -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),
Expand Down
15 changes: 15 additions & 0 deletions tests/pollux/Pollux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

Expand Down

0 comments on commit 9129387

Please sign in to comment.