From c72d1de7f4dc714aa50274d6d87ddab68b3760ce Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Fri, 25 Nov 2022 13:56:39 +0100 Subject: [PATCH] Generic ContextPropertyProbe --- .../oneedtech/inspect/vc/OB20Inspector.java | 5 +- .../vc/probe/ContextPropertyProbe.java | 52 +++++-------------- .../inspect/vc/util/TestOB20Inspector.java | 2 +- 3 files changed, 17 insertions(+), 42 deletions(-) diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/OB20Inspector.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/OB20Inspector.java index 7599944..c2a4f38 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/OB20Inspector.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/OB20Inspector.java @@ -28,6 +28,7 @@ import org.oneedtech.inspect.vc.jsonld.probe.JsonLDCompactionProve; import org.oneedtech.inspect.vc.jsonld.probe.JsonLDValidationProbe; import org.oneedtech.inspect.vc.payload.PngParser; import org.oneedtech.inspect.vc.payload.SvgParser; +import org.oneedtech.inspect.vc.probe.ContextPropertyProbe; import org.oneedtech.inspect.vc.probe.CredentialParseProbe; import org.oneedtech.inspect.vc.probe.TypePropertyProbe; import org.oneedtech.inspect.vc.util.CachingDocumentLoader; @@ -109,7 +110,7 @@ public class OB20Inspector extends Inspector { //context and type properties CredentialEnum type = assertion.getCredentialType(); - for(Probe probe : List.of(/*new ContextPropertyProbe(type), */new TypePropertyProbe(type))) { + for(Probe probe : List.of(new ContextPropertyProbe(type), new TypePropertyProbe(type))) { probeCount++; accumulator.add(probe.run(assertion.getJson(), ctx)); if(broken(accumulator)) return abort(ctx, accumulator, probeCount); @@ -130,7 +131,7 @@ public class OB20Inspector extends Inspector { } protected JsonLDCompactionProve getCompactionProbe(Assertion assertion) { - return new JsonLDCompactionProve(assertion.getContext().get(0)); + return new JsonLDCompactionProve(assertion.getCredentialType().getContextUris().get(0)); } public static class Builder extends Inspector.Builder { diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/ContextPropertyProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/ContextPropertyProbe.java index 67774f2..1580221 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/ContextPropertyProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/ContextPropertyProbe.java @@ -1,24 +1,12 @@ package org.oneedtech.inspect.vc.probe; -import static org.oneedtech.inspect.vc.VerifiableCredential.Type.*; - import static org.oneedtech.inspect.util.code.Defensives.checkNotNull; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.function.BiFunction; -import org.oneedtech.inspect.core.probe.Probe; import org.oneedtech.inspect.core.probe.RunContext; import org.oneedtech.inspect.core.report.ReportItems; -import org.oneedtech.inspect.vc.VerifiableCredential; - -import org.oneedtech.inspect.vc.util.JsonNodeUtil; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.google.common.collect.ImmutableMap; +import org.oneedtech.inspect.vc.Credential.CredentialEnum; /** * A Probe that verifies a credential's context property. @@ -26,9 +14,9 @@ import com.google.common.collect.ImmutableMap; * @author mgylling */ public class ContextPropertyProbe extends PropertyProbe { - private final VerifiableCredential.Type type; + private final CredentialEnum type; - public ContextPropertyProbe(VerifiableCredential.Type type) { + public ContextPropertyProbe(CredentialEnum type) { super(ID, "@context"); this.type = checkNotNull(type); setValidations(this::validate); @@ -40,35 +28,21 @@ public class ContextPropertyProbe extends PropertyProbe { } public ReportItems validate(List nodeValues, RunContext ctx) { - List expected = values.get(values.keySet() - .stream() - .filter(s->s.contains(type)) - .findFirst() - .orElseThrow(()-> new IllegalArgumentException(type.name() + " not recognized"))); - int pos = 0; - for (String uri : expected) { - if ((nodeValues.size() < pos + 1) || !nodeValues.get(pos).equals(uri)) { - return error("missing required @context uri " + uri + " at position " + (pos + 1), ctx); + if (!nodeValues.isEmpty()) { // empty context uri node: inline context + List contextUris = type.getContextUris(); + checkNotNull(contextUris); + + int pos = 0; + for (String uri : contextUris) { + if ((nodeValues.size() < pos + 1) || !nodeValues.get(pos).equals(uri)) { + return error("missing required @context uri " + uri + " at position " + (pos + 1), ctx); + } + pos++; } - pos++; } return success(ctx); } - private final static Map, List> values = new ImmutableMap.Builder, List>() - .put(Set.of(OpenBadgeCredential, AchievementCredential, EndorsementCredential), - List.of("https://www.w3.org/2018/credentials/v1", - //"https://purl.imsglobal.org/spec/ob/v3p0/context.json")) //dev legacy - "https://purl.imsglobal.org/spec/ob/v3p0/context.json")) - .put(Set.of(ClrCredential), - List.of("https://www.w3.org/2018/credentials/v1", -// "https://dc.imsglobal.org/draft/clr/v2p0/context", //dev legacy -// "https://purl.imsglobal.org/spec/ob/v3p0/context.json")) //dev legacy - "https://purl.imsglobal.org/spec/clr/v2p0/context.json", - "https://purl.imsglobal.org/spec/ob/v3p0/context.json")) - - .build(); - public static final String ID = ContextPropertyProbe.class.getSimpleName(); } diff --git a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/TestOB20Inspector.java b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/TestOB20Inspector.java index 1e80e40..323ec88 100644 --- a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/TestOB20Inspector.java +++ b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/TestOB20Inspector.java @@ -29,7 +29,7 @@ public class TestOB20Inspector extends OB20Inspector { @Override protected JsonLDCompactionProve getCompactionProbe(Assertion assertion) { - return new JsonLDCompactionProve(assertion.getContext().get(0), localDomains); + return new JsonLDCompactionProve(assertion.getCredentialType().getContextUris().get(0), localDomains); } public static class TestBuilder extends OB20Inspector.Builder {