Skip to content

Commit

Permalink
chore: allow OBJECT or ARRAY when deserializing VC (#4099)
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger authored Apr 9, 2024
1 parent 07365dc commit d13ea67
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public class StatusList2021RevocationService implements RevocationListService {
private final Cache<String, VerifiableCredential> cache;

public StatusList2021RevocationService(ObjectMapper objectMapper, long cacheValidity) {
this.objectMapper = objectMapper.copy().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // let's make sure this is disabled, because the "@context" would cause problems
this.objectMapper = objectMapper.copy()
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) // technically, credential subjects and credential status can be objects AND Arrays
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // let's make sure this is disabled, because the "@context" would cause problems
cache = new Cache<>(this::updateCredential, cacheValidity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.eclipse.edc.iam.verifiablecredentials.spi.TestFunctions;
import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockserver.integration.ClientAndServer;
Expand All @@ -44,9 +45,28 @@ class StatusList2021RevocationServiceTest {
void setup() {
clientAndServer = ClientAndServer.startClientAndServer("localhost", getFreePort());
clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3"))
.respond(HttpResponse.response().withStatusCode(200).withBody(TestData.STATUS_LIST_CREDENTIAL));
.respond(HttpResponse.response().withStatusCode(200).withBody(TestData.STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT));
}

@AfterEach
void tearDown() {
clientAndServer.stop();
}

@Test
void checkRevocation_shenSubjectIsArray() {
clientAndServer.reset();
clientAndServer.when(request().withMethod("GET").withPath("/credentials/status/3"))
.respond(HttpResponse.response().withStatusCode(200).withBody(TestData.STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY));
var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021",
Map.of(STATUS_LIST_PURPOSE, "revocation",
STATUS_LIST_INDEX, NOT_REVOKED_INDEX,
STATUS_LIST_CREDENTIAL, "http://localhost:%d/credentials/status/3".formatted(clientAndServer.getPort()))))
.build();
assertThat(revocationService.checkValidity(credential)).isSucceeded();
}


@Test
void checkRevocation_whenNotCached_valid() {
var credential = TestFunctions.createCredentialBuilder().credentialStatus(new CredentialStatus("test-id", "StatusList2021",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class TestData {
// test data taken from https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#example-example-statuslist2021credential-0
public static final String STATUS_LIST_CREDENTIAL = """
public static final String STATUS_LIST_CREDENTIAL_SUBJECT_IS_ARRAY = """
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
Expand All @@ -26,12 +26,33 @@ public class TestData {
"type": ["VerifiableCredential", "StatusList2021Credential"],
"issuer": "did:example:12345",
"issued": "2021-04-05T14:27:40Z",
"credentialSubject": [{
"credentialSubject": [
{
"id": "https://example.com/status/3#list",
"type": "StatusList2021",
"https://w3id.org/vc/status-list#statusPurpose": "revocation",
"https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA"
}]
}
]
}
""";

public static final String STATUS_LIST_CREDENTIAL_SINGLE_SUBJECT = """
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://w3id.org/vc/status-list/2021/v1"
],
"id": "https://example.com/credentials/status/3",
"type": ["VerifiableCredential", "StatusList2021Credential"],
"issuer": "did:example:12345",
"issued": "2021-04-05T14:27:40Z",
"credentialSubject": {
"id": "https://example.com/status/3#list",
"type": "StatusList2021",
"https://w3id.org/vc/status-list#statusPurpose": "revocation",
"https://w3id.org/vc/status-list#encodedList": "H4sIAAAAAAAAA-3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA"
}
}
""";
}

0 comments on commit d13ea67

Please sign in to comment.