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.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<JsonNode> probe : List.of(/*new ContextPropertyProbe(type), */new TypePropertyProbe(type))) {
for(Probe<JsonNode> 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<OB20Inspector.Builder> {

View File

@ -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<String> nodeValues, RunContext ctx) {
List<String> expected = values.get(values.keySet()
.stream()
.filter(s->s.contains(type))
.findFirst()
.orElseThrow(()-> new IllegalArgumentException(type.name() + " not recognized")));
if (!nodeValues.isEmpty()) { // empty context uri node: inline context
List<String> contextUris = type.getContextUris();
checkNotNull(contextUris);
int pos = 0;
for (String uri : expected) {
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++;
}
}
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();
}

View File

@ -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 {