Skip to content

Commit

Permalink
feat!: avoid empty nested arrays when generating VP (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger authored Nov 21, 2023
1 parent 3678a2c commit 1d9f762
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.eclipse.edc.identityhub.core;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.eclipse.edc.identityhub.spi.generator.PresentationCreatorRegistry;
import org.eclipse.edc.identityhub.spi.generator.PresentationGenerator;
Expand All @@ -26,6 +25,7 @@
import org.eclipse.edc.spi.result.Result;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -68,26 +68,25 @@ public Result<PresentationResponse> createPresentation(List<VerifiableCredential
var ldpVcs = ofNullable(groups.get(JSON_LD)).orElseGet(List::of);


String vpToken;
var vpToken = new ArrayList<>();

if (defaultFormatVp == JSON_LD) { // LDP-VPs cannot contain JWT VCs
var arrayBuilder = Json.createArrayBuilder();
if (!ldpVcs.isEmpty()) {
JsonObject ldpVp = registry.createPresentation(ldpVcs, CredentialFormat.JSON_LD);
arrayBuilder.add(ldpVp);
vpToken.add(ldpVp.toString());
}

if (!jwtVcs.isEmpty()) {
monitor.warning("The VP was requested in %s format, but the request yielded %s JWT-VCs, which cannot be transported in a LDP-VP. A second VP will be returned, containing JWT-VCs".formatted(JSON_LD, jwtVcs.size()));
String jwtVp = registry.createPresentation(jwtVcs, CredentialFormat.JWT);
arrayBuilder.add(jwtVp);
vpToken.add(jwtVp);
}

vpToken = arrayBuilder.build().toString();
} else { //defaultFormatVp == JWT
vpToken = registry.createPresentation(credentials, CredentialFormat.JWT);
vpToken.add(registry.createPresentation(credentials, CredentialFormat.JWT));
}

var presentationResponse = new PresentationResponse(new Object[] {vpToken}, null);
var presentationResponse = new PresentationResponse(vpToken.toArray(new Object[0]), null);
return Result.success(presentationResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Result<List<String>> verify(String token) {

// make sure an access_token claim exists
var claimToken = validationResult.getContent();
if (claimToken.getClaim("access_token") == null) {
if (claimToken.getClaim(ACCES_TOKEN_CLAIM) == null) {
return failure("No 'access_token' claim was found on ID Token.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void generate_noCredentials() {
List<VerifiableCredentialContainer> ldpVcs = List.of();

var result = presentationGenerator.createPresentation(ldpVcs, null);
assertThat(result).isSucceeded();
assertThat(result).isSucceeded().matches(pr -> pr.vpToken().length == 0, "VP Tokens should be empty");
}

@Test
Expand Down

0 comments on commit 1d9f762

Please sign in to comment.