Skip to content

Commit

Permalink
feat: Ability to preserve VCDM fields in VC-JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed May 24, 2024
1 parent b1b23ae commit c62319d
Showing 1 changed file with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ToJwtConverter {
* from JSON-LD to JWT VC
*/

public static JwtVerifiableCredential toJwtVerifiableCredential(VerifiableCredential verifiableCredential, String aud) {
public static JwtVerifiableCredential toJwtVerifiableCredential(VerifiableCredential verifiableCredential, String aud, boolean preserveVerifiableCredentialProperties) {

JWTClaimsSet.Builder jwtPayloadBuilder = new JWTClaimsSet.Builder();

Expand All @@ -33,41 +33,46 @@ public static JwtVerifiableCredential toJwtVerifiableCredential(VerifiableCreden
URI id = verifiableCredential.getId();
if (id != null) {
jwtPayloadBuilder.jwtID(id.toString());
JsonLDUtils.jsonLdRemove(payloadVerifiableCredential, JsonLDKeywords.JSONLD_TERM_ID);
if (!preserveVerifiableCredentialProperties) {
JsonLDUtils.jsonLdRemove(payloadVerifiableCredential, JsonLDKeywords.JSONLD_TERM_ID);
}
}

CredentialSubject credentialSubject = verifiableCredential.getCredentialSubject();
if (credentialSubject != null) {
if (credentialSubject.getId() != null) {
jwtPayloadBuilder.subject(credentialSubject.getId().toString());
if (credentialSubject != null && credentialSubject.getId() != null) {
jwtPayloadBuilder.subject(credentialSubject.getId().toString());
if (!preserveVerifiableCredentialProperties) {
CredentialSubject payloadCredentialSubject = CredentialSubject.builder()
.base(credentialSubject)
.build();
JsonLDUtils.jsonLdRemove(payloadCredentialSubject, JsonLDKeywords.JSONLD_TERM_ID);
CredentialSubject.removeFromJsonLdObject(payloadVerifiableCredential);
payloadCredentialSubject.addToJsonLDObject(payloadVerifiableCredential);
}
CredentialSubject payloadCredentialSubject = CredentialSubject.builder()
.base(credentialSubject)
.build();
JsonLDUtils.jsonLdRemove(payloadCredentialSubject, JsonLDKeywords.JSONLD_TERM_ID);
CredentialSubject.removeFromJsonLdObject(payloadVerifiableCredential);
payloadCredentialSubject.addToJsonLDObject(payloadVerifiableCredential);
}

URI issuer = verifiableCredential.getIssuer();
if (issuer != null) {
jwtPayloadBuilder.issuer(issuer.toString());
if ( verifiableCredential.getJsonObject().containsKey(VerifiableCredentialKeywords.JSONLD_TERM_ISSUER)
&& !( verifiableCredential.getJsonObject().get(VerifiableCredentialKeywords.JSONLD_TERM_ISSUER) instanceof Map)) {
if (!preserveVerifiableCredentialProperties) {
JsonLDUtils.jsonLdRemove(payloadVerifiableCredential, VerifiableCredentialKeywords.JSONLD_TERM_ISSUER);
}
}

Date issuanceDate = verifiableCredential.getIssuanceDate();
if (issuanceDate != null) {
jwtPayloadBuilder.notBeforeTime(issuanceDate);
JsonLDUtils.jsonLdRemove(payloadVerifiableCredential, VerifiableCredentialKeywords.JSONLD_TERM_ISSUANCEDATE);
if (!preserveVerifiableCredentialProperties) {
JsonLDUtils.jsonLdRemove(payloadVerifiableCredential, VerifiableCredentialKeywords.JSONLD_TERM_ISSUANCEDATE);
}
}

Date expirationDate = verifiableCredential.getExpirationDate();
if (expirationDate != null) {
jwtPayloadBuilder.expirationTime(expirationDate);
JsonLDUtils.jsonLdRemove(payloadVerifiableCredential, VerifiableCredentialKeywords.JSONLD_TERM_EXPIRATIONDATE);
if (!preserveVerifiableCredentialProperties) {
JsonLDUtils.jsonLdRemove(payloadVerifiableCredential, VerifiableCredentialKeywords.JSONLD_TERM_EXPIRATIONDATE);
}
}

if (aud != null) {
Expand All @@ -82,9 +87,19 @@ public static JwtVerifiableCredential toJwtVerifiableCredential(VerifiableCreden
return new JwtVerifiableCredential(jwtPayload, payloadVerifiableCredential, null, null);
}

public static JwtVerifiableCredential toJwtVerifiableCredential(VerifiableCredential verifiableCredential, String aud) {

return toJwtVerifiableCredential(verifiableCredential, null, false);
}

public static JwtVerifiableCredential toJwtVerifiableCredential(VerifiableCredential verifiableCredential, boolean preserveVerifiableCredentialProperties) {

return toJwtVerifiableCredential(verifiableCredential, null, preserveVerifiableCredentialProperties);
}

public static JwtVerifiableCredential toJwtVerifiableCredential(VerifiableCredential verifiableCredential) {

return toJwtVerifiableCredential(verifiableCredential, null);
return toJwtVerifiableCredential(verifiableCredential, null, false);
}

/*
Expand Down

0 comments on commit c62319d

Please sign in to comment.