eclipse-tractusx / SSI-agent-lib

Apache License 2.0
4 stars 15 forks source link

Cannot use verifiable credential builder to create a VC that has no expiration Date #78

Closed mustafasalfiti closed 7 months ago

mustafasalfiti commented 10 months ago

i will get an error trying to create a verifiable credential that doesn't has a expiration date using credential builder

the problem is identified in this code block

VerifiableCredentialBuilder.java line 100

  public VerifiableCredential build() {
    DateTimeFormatter formatter =
        DateTimeFormatter.ofPattern(VerifiableCredential.TIME_FORMAT).withZone(ZoneOffset.UTC);

    // Map.of does not work, as proof can be null
    Map<String, Object> map = new HashMap<>();
    map.put(VerifiableCredential.CONTEXT, context);
    map.put(VerifiableCredential.ID, id.toString());
    map.put(VerifiableCredential.TYPE, types);
    map.put(VerifiableCredential.ISSUER, issuer.toString());
    map.put(VerifiableCredential.ISSUANCE_DATE, formatter.format(issuanceDate));
    map.put(VerifiableCredential.EXPIRATION_DATE, formatter.format(expirationDate));
    map.put(VerifiableCredential.CREDENTIAL_SUBJECT, credentialSubject);
    if (proof != null) {
      map.put(VerifiableCredential.PROOF, proof);
    }

    return new VerifiableCredential(map);
  }

if expirationDate is null an error will be thrown

solution we can check if there is a expirationDate

  public VerifiableCredential build() {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(VerifiableCredential.TIME_FORMAT)
        .withZone(ZoneOffset.UTC);

    // Map.of does not work, as proof can be null
    Map<String, Object> map = new HashMap<>();
    map.put(VerifiableCredential.CONTEXT, context);
    map.put(VerifiableCredential.ID, id.toString());
    map.put(VerifiableCredential.TYPE, types);
    map.put(VerifiableCredential.ISSUER, issuer.toString());
    map.put(VerifiableCredential.ISSUANCE_DATE, formatter.format(issuanceDate));
    if (expirationDate != null) {
      map.put(VerifiableCredential.EXPIRATION_DATE, formatter.format(expirationDate));
    }
    map.put(VerifiableCredential.CREDENTIAL_SUBJECT, credentialSubject);
    if (proof != null) {
      map.put(VerifiableCredential.PROOF, proof);
    }

    return new VerifiableCredential(map);
  }