From 1d9f762505377a981073f8cd908c912f55966124 Mon Sep 17 00:00:00 2001 From: Paul Latzelsperger <43503240+paullatzelsperger@users.noreply.github.com> Date: Tue, 21 Nov 2023 10:30:41 +0100 Subject: [PATCH] feat!: avoid empty nested arrays when generating VP (#178) --- .../core/PresentationGeneratorImpl.java | 15 +++++++-------- .../verification/AccessTokenVerifierImpl.java | 2 +- .../core/PresentationGeneratorImplTest.java | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/core/identity-hub-core/src/main/java/org/eclipse/edc/identityhub/core/PresentationGeneratorImpl.java b/core/identity-hub-core/src/main/java/org/eclipse/edc/identityhub/core/PresentationGeneratorImpl.java index 8f695dfce..d7bc65fe9 100644 --- a/core/identity-hub-core/src/main/java/org/eclipse/edc/identityhub/core/PresentationGeneratorImpl.java +++ b/core/identity-hub-core/src/main/java/org/eclipse/edc/identityhub/core/PresentationGeneratorImpl.java @@ -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; @@ -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; @@ -68,26 +68,25 @@ public Result createPresentation(List(); + 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); } } diff --git a/core/identity-hub-core/src/main/java/org/eclipse/edc/identityhub/token/verification/AccessTokenVerifierImpl.java b/core/identity-hub-core/src/main/java/org/eclipse/edc/identityhub/token/verification/AccessTokenVerifierImpl.java index 770dd6374..a29cd8b69 100644 --- a/core/identity-hub-core/src/main/java/org/eclipse/edc/identityhub/token/verification/AccessTokenVerifierImpl.java +++ b/core/identity-hub-core/src/main/java/org/eclipse/edc/identityhub/token/verification/AccessTokenVerifierImpl.java @@ -68,7 +68,7 @@ public Result> 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."); } diff --git a/core/identity-hub-core/src/test/java/org/eclipse/edc/identityhub/core/PresentationGeneratorImplTest.java b/core/identity-hub-core/src/test/java/org/eclipse/edc/identityhub/core/PresentationGeneratorImplTest.java index f71ddba6e..57c40f1a6 100644 --- a/core/identity-hub-core/src/test/java/org/eclipse/edc/identityhub/core/PresentationGeneratorImplTest.java +++ b/core/identity-hub-core/src/test/java/org/eclipse/edc/identityhub/core/PresentationGeneratorImplTest.java @@ -63,7 +63,7 @@ void generate_noCredentials() { List 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