Generic ContextPropertyProbe

This commit is contained in:
Xavi Aracil 2022-11-25 13:56:39 +01:00
parent 2560281420
commit c72d1de7f4
3 changed files with 17 additions and 42 deletions

View File

@ -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.jsonld.probe.JsonLDValidationProbe;
import org.oneedtech.inspect.vc.payload.PngParser; import org.oneedtech.inspect.vc.payload.PngParser;
import org.oneedtech.inspect.vc.payload.SvgParser; 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.CredentialParseProbe;
import org.oneedtech.inspect.vc.probe.TypePropertyProbe; import org.oneedtech.inspect.vc.probe.TypePropertyProbe;
import org.oneedtech.inspect.vc.util.CachingDocumentLoader; import org.oneedtech.inspect.vc.util.CachingDocumentLoader;
@ -109,7 +110,7 @@ public class OB20Inspector extends Inspector {
//context and type properties //context and type properties
CredentialEnum type = assertion.getCredentialType(); CredentialEnum type = assertion.getCredentialType();
for(Probe<JsonNode> probe : List.of(/*new ContextPropertyProbe(type), */new TypePropertyProbe(type))) { for(Probe<JsonNode> probe : List.of(new ContextPropertyProbe(type), new TypePropertyProbe(type))) {
probeCount++; probeCount++;
accumulator.add(probe.run(assertion.getJson(), ctx)); accumulator.add(probe.run(assertion.getJson(), ctx));
if(broken(accumulator)) return abort(ctx, accumulator, probeCount); if(broken(accumulator)) return abort(ctx, accumulator, probeCount);
@ -130,7 +131,7 @@ public class OB20Inspector extends Inspector {
} }
protected JsonLDCompactionProve getCompactionProbe(Assertion assertion) { 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<OB20Inspector.Builder> { public static class Builder extends Inspector.Builder<OB20Inspector.Builder> {

View File

@ -1,24 +1,12 @@
package org.oneedtech.inspect.vc.probe; package org.oneedtech.inspect.vc.probe;
import static org.oneedtech.inspect.vc.VerifiableCredential.Type.*;
import static org.oneedtech.inspect.util.code.Defensives.checkNotNull; import static org.oneedtech.inspect.util.code.Defensives.checkNotNull;
import java.util.List; 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.probe.RunContext;
import org.oneedtech.inspect.core.report.ReportItems; import org.oneedtech.inspect.core.report.ReportItems;
import org.oneedtech.inspect.vc.VerifiableCredential; import org.oneedtech.inspect.vc.Credential.CredentialEnum;
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;
/** /**
* A Probe that verifies a credential's context property. * A Probe that verifies a credential's context property.
@ -26,9 +14,9 @@ import com.google.common.collect.ImmutableMap;
* @author mgylling * @author mgylling
*/ */
public class ContextPropertyProbe extends PropertyProbe { 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"); super(ID, "@context");
this.type = checkNotNull(type); this.type = checkNotNull(type);
setValidations(this::validate); setValidations(this::validate);
@ -40,35 +28,21 @@ public class ContextPropertyProbe extends PropertyProbe {
} }
public ReportItems validate(List<String> nodeValues, RunContext ctx) { public ReportItems validate(List<String> nodeValues, RunContext ctx) {
List<String> expected = values.get(values.keySet() if (!nodeValues.isEmpty()) { // empty context uri node: inline context
.stream() List<String> contextUris = type.getContextUris();
.filter(s->s.contains(type)) checkNotNull(contextUris);
.findFirst()
.orElseThrow(()-> new IllegalArgumentException(type.name() + " not recognized"))); int pos = 0;
int pos = 0; for (String uri : contextUris) {
for (String uri : expected) { if ((nodeValues.size() < pos + 1) || !nodeValues.get(pos).equals(uri)) {
if ((nodeValues.size() < pos + 1) || !nodeValues.get(pos).equals(uri)) { return error("missing required @context uri " + uri + " at position " + (pos + 1), ctx);
return error("missing required @context uri " + uri + " at position " + (pos + 1), ctx); }
pos++;
} }
pos++;
} }
return success(ctx); return success(ctx);
} }
private final static Map<Set<VerifiableCredential.Type>, List<String>> values = new ImmutableMap.Builder<Set<VerifiableCredential.Type>, List<String>>()
.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(); public static final String ID = ContextPropertyProbe.class.getSimpleName();
} }

View File

@ -29,7 +29,7 @@ public class TestOB20Inspector extends OB20Inspector {
@Override @Override
protected JsonLDCompactionProve getCompactionProbe(Assertion assertion) { 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 { public static class TestBuilder extends OB20Inspector.Builder {