From 1677a0b359d93054fdc721814c9edf46746cf1a2 Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Thu, 15 Dec 2022 15:41:24 +0100 Subject: [PATCH] Added a UriFactory for allowing local resources in testing --- .../inspect/vc/OB20EndorsementInspector.java | 12 +++++++ .../oneedtech/inspect/vc/OB20Inspector.java | 30 +++++----------- .../org/oneedtech/inspect/vc/VCInspector.java | 6 ++++ .../vc/jsonld/probe/GraphFetcherProbe.java | 20 +++-------- .../jsonld/probe/JsonLDCompactionProve.java | 1 - .../probe/AssertionRevocationListProbe.java | 23 +++--------- .../probe/VerificationDependenciesProbe.java | 26 +++----------- .../vc/probe/VerificationJWTProbe.java | 23 +++--------- .../ValidationImagePropertyProbe.java | 4 ++- .../validation/ValidationPropertyProbe.java | 18 ++-------- .../resource/DefaultUriResourceFactory.java | 19 ++++++++++ .../vc/resource/UriResourceFactory.java | 13 +++++++ .../vc/resource/TestUriResourceFactory.java | 35 +++++++++++++++++++ .../inspect/vc/util/TestOB20Inspector.java | 7 ++++ 14 files changed, 124 insertions(+), 113 deletions(-) create mode 100644 inspector-vc/src/main/java/org/oneedtech/inspect/vc/resource/DefaultUriResourceFactory.java create mode 100644 inspector-vc/src/main/java/org/oneedtech/inspect/vc/resource/UriResourceFactory.java create mode 100644 inspector-vc/src/test/java/org/oneedtech/inspect/vc/resource/TestUriResourceFactory.java diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/OB20EndorsementInspector.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/OB20EndorsementInspector.java index 7bde48b..5e7d303 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/OB20EndorsementInspector.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/OB20EndorsementInspector.java @@ -7,6 +7,7 @@ import static org.oneedtech.inspect.core.probe.RunContext.Key.JSON_DOCUMENT_LOAD import static org.oneedtech.inspect.core.probe.RunContext.Key.JWT_CREDENTIAL_NODE_NAME; import static org.oneedtech.inspect.core.probe.RunContext.Key.PNG_CREDENTIAL_KEY; import static org.oneedtech.inspect.core.probe.RunContext.Key.SVG_CREDENTIAL_QNAME; +import static org.oneedtech.inspect.core.probe.RunContext.Key.URI_RESOURCE_FACTORY; import static org.oneedtech.inspect.core.report.ReportUtil.onProbeException; import static org.oneedtech.inspect.util.code.Defensives.checkNotNull; import static org.oneedtech.inspect.util.json.ObjectMapperCache.Config.DEFAULT; @@ -33,6 +34,7 @@ import org.oneedtech.inspect.vc.probe.AssertionRevocationListProbe; import org.oneedtech.inspect.vc.probe.ExpirationProbe; import org.oneedtech.inspect.vc.probe.IssuanceProbe; import org.oneedtech.inspect.vc.probe.VerificationDependenciesProbe; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import com.apicatalog.jsonld.loader.DocumentLoader; import com.fasterxml.jackson.databind.JsonNode; @@ -45,10 +47,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class OB20EndorsementInspector extends VCInspector implements SubInspector { private DocumentLoader documentLoader; + private UriResourceFactory uriResourceFactory; protected OB20EndorsementInspector(OB20EndorsementInspector.Builder builder) { super(builder); this.documentLoader = builder.documentLoader; + this.uriResourceFactory = builder.uriResourceFactory; } @Override @@ -78,6 +82,7 @@ public class OB20EndorsementInspector extends VCInspector implements SubInspecto .put(SVG_CREDENTIAL_QNAME, SvgParser.QNames.OB20) .put(JSON_DOCUMENT_LOADER, documentLoader) .put(JWT_CREDENTIAL_NODE_NAME, Assertion.JWT_NODE_NAME) + .put(URI_RESOURCE_FACTORY, uriResourceFactory) .build(); parentObjects.entrySet().stream().forEach(entry -> { @@ -122,6 +127,7 @@ public class OB20EndorsementInspector extends VCInspector implements SubInspecto public static class Builder extends VCInspector.Builder { private DocumentLoader documentLoader; + private UriResourceFactory uriResourceFactory; @SuppressWarnings("unchecked") @Override @@ -133,6 +139,12 @@ public class OB20EndorsementInspector extends VCInspector implements SubInspecto this.documentLoader = documentLoader; return this; } + + public Builder uriResourceFactory(UriResourceFactory uriResourceFactory) { + this.uriResourceFactory = uriResourceFactory; + return this; + } + } } 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 d812cb8..57f939a 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 @@ -8,8 +8,6 @@ import static org.oneedtech.inspect.util.json.ObjectMapperCache.Config.DEFAULT; import static org.oneedtech.inspect.vc.Credential.CREDENTIAL_KEY; import static org.oneedtech.inspect.vc.util.JsonNodeUtil.asNodeList; -import java.net.URI; -import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -48,6 +46,7 @@ import org.oneedtech.inspect.vc.probe.TypePropertyProbe; import org.oneedtech.inspect.vc.probe.VerificationDependenciesProbe; import org.oneedtech.inspect.vc.probe.VerificationJWTProbe; import org.oneedtech.inspect.vc.probe.validation.ValidationPropertyProbeFactory; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import org.oneedtech.inspect.vc.util.CachingDocumentLoader; import com.apicatalog.jsonld.loader.DocumentLoader; @@ -55,8 +54,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import foundation.identity.jsonld.ConfigurableDocumentLoader; - /** * A verifier for Open Badges 2.0. * @author xaracil @@ -82,6 +79,7 @@ public class OB20Inspector extends VCInspector { ObjectMapper mapper = ObjectMapperCache.get(DEFAULT); JsonPathEvaluator jsonPath = new JsonPathEvaluator(mapper); DocumentLoader documentLoader = getDocumentLoader(); + UriResourceFactory uriResourceFactory = getUriResourceFactory(documentLoader); RunContext ctx = new RunContext.Builder() .put(this) @@ -93,6 +91,7 @@ public class OB20Inspector extends VCInspector { .put(Key.SVG_CREDENTIAL_QNAME, SvgParser.QNames.OB20) .put(Key.JSON_DOCUMENT_LOADER, documentLoader) .put(Key.JWT_CREDENTIAL_NODE_NAME, Assertion.JWT_NODE_NAME) + .put(Key.URI_RESOURCE_FACTORY, uriResourceFactory) .build(); List accumulator = new ArrayList<>(); @@ -189,7 +188,10 @@ public class OB20Inspector extends VCInspector { } // Embedded endorsements. Pass document loader because it has already cached documents, and it has localdomains for testing - OB20EndorsementInspector endorsementInspector = new OB20EndorsementInspector.Builder().documentLoader(documentLoader).build(); + OB20EndorsementInspector endorsementInspector = new OB20EndorsementInspector.Builder() + .documentLoader(documentLoader) + .uriResourceFactory(uriResourceFactory) + .build(); // get endorsements for all JSON_LD objects in the graph List endorsements = jsonLdGeneratedObjects.stream().flatMap(node -> { @@ -201,7 +203,7 @@ public class OB20Inspector extends VCInspector { for(JsonNode node : endorsements) { probeCount++; // get endorsement json from context - UriResource uriResource = resolveUriResource(ctx, node.asText()); + UriResource uriResource = uriResourceFactory.of(node.asText()); JsonLdGeneratedObject resolved = (JsonLdGeneratedObject) ctx.getGeneratedObject(JsonLDCompactionProve.getId(uriResource)); if (resolved == null) { throw new IllegalArgumentException("endorsement " + node.toString() + " not found in graph"); @@ -244,20 +246,4 @@ public class OB20Inspector extends VCInspector { */ public static final String ALLOW_LOCAL_REDIRECTION = "ALLOW_LOCAL_REDIRECTION"; } - - protected UriResource resolveUriResource(RunContext ctx, String url) throws URISyntaxException { - URI uri = new URI(url); - UriResource initialUriResource = new UriResource(uri); - UriResource uriResource = initialUriResource; - - // check if uri points to a local resource - if (ctx.get(Key.JSON_DOCUMENT_LOADER) instanceof ConfigurableDocumentLoader) { - if (ConfigurableDocumentLoader.getDefaultHttpLoader() instanceof CachingDocumentLoader.HttpLoader) { - URI resolvedUri = ((CachingDocumentLoader.HttpLoader) ConfigurableDocumentLoader.getDefaultHttpLoader()).resolve(uri); - uriResource = new UriResource(resolvedUri); - } - } - return uriResource; - } - } diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/VCInspector.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/VCInspector.java index 3e29fd7..60dd4ac 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/VCInspector.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/VCInspector.java @@ -20,6 +20,8 @@ import org.oneedtech.inspect.core.report.Report; import org.oneedtech.inspect.core.report.ReportItems; import org.oneedtech.inspect.util.code.Tuple; import org.oneedtech.inspect.vc.jsonld.probe.ExtensionProbe; +import org.oneedtech.inspect.vc.resource.DefaultUriResourceFactory; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import org.oneedtech.inspect.vc.util.CachingDocumentLoader; import org.oneedtech.inspect.vc.util.JsonNodeUtil; @@ -82,6 +84,10 @@ public abstract class VCInspector extends Inspector { return new CachingDocumentLoader(); } + protected UriResourceFactory getUriResourceFactory(DocumentLoader documentLoader) { + return new DefaultUriResourceFactory(); + } + protected List> getExtensionProbes(JsonNode node, String entryPath) { List> probes = new ArrayList<>(); if (!node.isObject()) { diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/probe/GraphFetcherProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/probe/GraphFetcherProbe.java index 988d3fb..b5958e1 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/probe/GraphFetcherProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/probe/GraphFetcherProbe.java @@ -26,6 +26,7 @@ import org.oneedtech.inspect.vc.Assertion.ValueType; import org.oneedtech.inspect.vc.Validation; import org.oneedtech.inspect.vc.jsonld.JsonLdGeneratedObject; import org.oneedtech.inspect.vc.probe.CredentialParseProbe; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import org.oneedtech.inspect.vc.util.CachingDocumentLoader; import org.oneedtech.inspect.vc.util.JsonNodeUtil; import org.oneedtech.inspect.vc.util.PrimitiveValueValidator; @@ -126,9 +127,11 @@ public class GraphFetcherProbe extends Probe { private ReportItems fetchNode(RunContext ctx, ReportItems result, JsonNode idNode) throws URISyntaxException, Exception, JsonProcessingException, JsonMappingException { - UriResource uriResource = resolveUriResource(ctx, idNode.asText().strip()); + System.out.println("fetchNode " + idNode.asText().strip()); + UriResource uriResource = ((UriResourceFactory) ctx.get(Key.URI_RESOURCE_FACTORY)).of(idNode.asText().strip()); JsonLdGeneratedObject resolved = (JsonLdGeneratedObject) ctx.getGeneratedObject(JsonLDCompactionProve.getId(uriResource)); if (resolved == null) { + System.out.println("parsing and loading " + idNode.asText().strip()); result = new ReportItems(List.of(result, new CredentialParseProbe().run(uriResource, ctx))); if (!result.contains(Outcome.FATAL, Outcome.EXCEPTION)) { Assertion fetchedAssertion = (Assertion) ctx.getGeneratedObject(uriResource.getID()); @@ -200,21 +203,6 @@ public class GraphFetcherProbe extends Probe { return matcher.matches(); } - protected UriResource resolveUriResource(RunContext ctx, String url) throws URISyntaxException { - URI uri = new URI(url); - UriResource initialUriResource = new UriResource(uri); - UriResource uriResource = initialUriResource; - - // check if uri points to a local resource - if (ctx.get(Key.JSON_DOCUMENT_LOADER) instanceof ConfigurableDocumentLoader) { - if (ConfigurableDocumentLoader.getDefaultHttpLoader() instanceof CachingDocumentLoader.HttpLoader) { - URI resolvedUri = ((CachingDocumentLoader.HttpLoader) ConfigurableDocumentLoader.getDefaultHttpLoader()).resolve(uri); - uriResource = new UriResource(resolvedUri); - } - } - return uriResource; - } - public static final String ID = GraphFetcherProbe.class.getSimpleName(); public static final String URN_REGEX = "^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'"; protected final static Logger logger = LogManager.getLogger(GraphFetcherProbe.class); diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/probe/JsonLDCompactionProve.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/probe/JsonLDCompactionProve.java index b1b8946..1aeb5f5 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/probe/JsonLDCompactionProve.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/probe/JsonLDCompactionProve.java @@ -12,7 +12,6 @@ import org.oneedtech.inspect.vc.jsonld.JsonLdGeneratedObject; import com.apicatalog.jsonld.JsonLd; import com.apicatalog.jsonld.JsonLdOptions; -import com.apicatalog.jsonld.api.CompactionApi; import com.apicatalog.jsonld.document.JsonDocument; import com.apicatalog.jsonld.loader.DocumentLoader; diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/AssertionRevocationListProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/AssertionRevocationListProbe.java index b951cb0..d91abe3 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/AssertionRevocationListProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/AssertionRevocationListProbe.java @@ -15,6 +15,7 @@ import org.oneedtech.inspect.core.report.ReportItems; import org.oneedtech.inspect.util.resource.UriResource; import org.oneedtech.inspect.vc.jsonld.JsonLdGeneratedObject; import org.oneedtech.inspect.vc.jsonld.probe.JsonLDCompactionProve; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import org.oneedtech.inspect.vc.util.CachingDocumentLoader; import org.oneedtech.inspect.vc.util.JsonNodeUtil; @@ -41,9 +42,10 @@ public class AssertionRevocationListProbe extends Probe { public ReportItems run(JsonLdGeneratedObject jsonLdGeneratedObject, RunContext ctx) throws Exception { ObjectMapper mapper = (ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER); JsonNode jsonNode = (mapper).readTree(jsonLdGeneratedObject.getJson()); + UriResourceFactory uriResourceFactory = (UriResourceFactory) ctx.get(Key.URI_RESOURCE_FACTORY); // get badge - UriResource badgeUriResource = resolveUriResource(ctx, getBadgeClaimId(jsonNode)); + UriResource badgeUriResource = uriResourceFactory.of(getBadgeClaimId(jsonNode)); JsonLdGeneratedObject badgeObject = (JsonLdGeneratedObject) ctx.getGeneratedObject( JsonLDCompactionProve.getId(badgeUriResource)); @@ -51,7 +53,7 @@ public class AssertionRevocationListProbe extends Probe { JsonNode badgeNode = ((ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER)) .readTree(badgeObject.getJson()); - UriResource issuerUriResource = resolveUriResource(ctx, badgeNode.get("issuer").asText().strip()); + UriResource issuerUriResource = uriResourceFactory.of(badgeNode.get("issuer").asText().strip()); JsonLdGeneratedObject issuerObject = (JsonLdGeneratedObject) ctx.getGeneratedObject( JsonLDCompactionProve.getId(issuerUriResource)); JsonNode issuerNode = ((ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER)) @@ -63,7 +65,7 @@ public class AssertionRevocationListProbe extends Probe { return success(ctx); } - UriResource revocationListUriResource = resolveUriResource(ctx, revocationListIdNode.asText().strip()); + UriResource revocationListUriResource = uriResourceFactory.of(revocationListIdNode.asText().strip()); JsonLdGeneratedObject revocationListObject = (JsonLdGeneratedObject) ctx.getGeneratedObject( JsonLDCompactionProve.getId(revocationListUriResource)); JsonNode revocationListNode = ((ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER)) @@ -88,21 +90,6 @@ public class AssertionRevocationListProbe extends Probe { return success(ctx); } - protected UriResource resolveUriResource(RunContext ctx, String url) throws URISyntaxException { - URI uri = new URI(url); - UriResource initialUriResource = new UriResource(uri); - UriResource uriResource = initialUriResource; - - // check if uri points to a local resource - if (ctx.get(Key.JSON_DOCUMENT_LOADER) instanceof ConfigurableDocumentLoader) { - if (ConfigurableDocumentLoader.getDefaultHttpLoader() instanceof CachingDocumentLoader.HttpLoader) { - URI resolvedUri = ((CachingDocumentLoader.HttpLoader) ConfigurableDocumentLoader.getDefaultHttpLoader()).resolve(uri); - uriResource = new UriResource(resolvedUri); - } - } - return uriResource; - } - /** * Return the ID of the node with name propertyName * @param jsonNode node diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/VerificationDependenciesProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/VerificationDependenciesProbe.java index 38f34be..8011742 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/VerificationDependenciesProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/VerificationDependenciesProbe.java @@ -13,14 +13,12 @@ import org.oneedtech.inspect.core.report.ReportItems; import org.oneedtech.inspect.util.resource.UriResource; import org.oneedtech.inspect.vc.jsonld.JsonLdGeneratedObject; import org.oneedtech.inspect.vc.jsonld.probe.JsonLDCompactionProve; -import org.oneedtech.inspect.vc.util.CachingDocumentLoader; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import org.oneedtech.inspect.vc.util.JsonNodeUtil; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import foundation.identity.jsonld.ConfigurableDocumentLoader; - /** * Verification probe for Open Badges 2.0 * Maps to "ASSERTION_VERIFICATION_DEPENDENCIES" task in python implementation @@ -45,13 +43,14 @@ public class VerificationDependenciesProbe extends Probe public ReportItems run(JsonLdGeneratedObject jsonLdGeneratedObject, RunContext ctx) throws Exception { ObjectMapper mapper = (ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER); JsonNode jsonNode = (mapper).readTree(jsonLdGeneratedObject.getJson()); + UriResourceFactory uriResourceFactory = (UriResourceFactory) ctx.get(Key.URI_RESOURCE_FACTORY); JsonNode verificationNode = jsonNode.get("verification"); checkNotNull(verificationNode); String type = null; if (verificationNode.isTextual()) { // get verification from graph - UriResource verificationUriResource = resolveUriResource(ctx, verificationNode.asText().strip()); + UriResource verificationUriResource = uriResourceFactory.of(verificationNode.asText().strip()); JsonLdGeneratedObject verificationObject = (JsonLdGeneratedObject) ctx.getGeneratedObject( JsonLDCompactionProve.getId(verificationUriResource)); JsonNode verificationRootNode = ((ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER)) @@ -63,14 +62,14 @@ public class VerificationDependenciesProbe extends Probe if ("HostedBadge".equals(type)) { // get badge - UriResource badgeUriResource = resolveUriResource(ctx, getBadgeClaimId(jsonNode)); + UriResource badgeUriResource = uriResourceFactory.of(getBadgeClaimId(jsonNode)); JsonLdGeneratedObject badgeObject = (JsonLdGeneratedObject) ctx.getGeneratedObject( JsonLDCompactionProve.getId(badgeUriResource)); JsonNode badgeNode = ((ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER)) .readTree(badgeObject.getJson()); // get issuer from badge - UriResource issuerUriResource = resolveUriResource(ctx, badgeNode.get("issuer").asText().strip()); + UriResource issuerUriResource = uriResourceFactory.of(badgeNode.get("issuer").asText().strip()); JsonLdGeneratedObject issuerObject = (JsonLdGeneratedObject) ctx.getGeneratedObject( JsonLDCompactionProve.getId(issuerUriResource)); @@ -139,21 +138,6 @@ public class VerificationDependenciesProbe extends Probe return issuerUri.getAuthority(); } - protected UriResource resolveUriResource(RunContext ctx, String url) throws URISyntaxException { - URI uri = new URI(url); - UriResource initialUriResource = new UriResource(uri); - UriResource uriResource = initialUriResource; - - // check if uri points to a local resource - if (ctx.get(Key.JSON_DOCUMENT_LOADER) instanceof ConfigurableDocumentLoader) { - if (ConfigurableDocumentLoader.getDefaultHttpLoader() instanceof CachingDocumentLoader.HttpLoader) { - URI resolvedUri = ((CachingDocumentLoader.HttpLoader) ConfigurableDocumentLoader.getDefaultHttpLoader()).resolve(uri); - uriResource = new UriResource(resolvedUri); - } - } - return uriResource; - } - /** * Return the ID of the node with name propertyName * @param jsonNode node diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/VerificationJWTProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/VerificationJWTProbe.java index c75fbb5..d342f31 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/VerificationJWTProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/VerificationJWTProbe.java @@ -15,6 +15,7 @@ import org.oneedtech.inspect.core.report.ReportItems; import org.oneedtech.inspect.util.resource.UriResource; import org.oneedtech.inspect.vc.jsonld.JsonLdGeneratedObject; import org.oneedtech.inspect.vc.jsonld.probe.JsonLDCompactionProve; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import org.oneedtech.inspect.vc.util.CachingDocumentLoader; import org.oneedtech.inspect.vc.util.JsonNodeUtil; @@ -44,16 +45,17 @@ public class VerificationJWTProbe extends Probe { public ReportItems run(JsonLdGeneratedObject assertion, RunContext ctx) throws Exception { ObjectMapper mapper = (ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER); JsonNode assertionNode = (mapper).readTree(assertion.getJson()); + UriResourceFactory uriResourceFactory = (UriResourceFactory) ctx.get(Key.URI_RESOURCE_FACTORY); // get badge from assertion - UriResource badgeUriResource = resolveUriResource(ctx, assertionNode.get("badge").asText().strip()); + UriResource badgeUriResource = uriResourceFactory.of(assertionNode.get("badge").asText().strip()); JsonLdGeneratedObject badgeObject = (JsonLdGeneratedObject) ctx.getGeneratedObject( JsonLDCompactionProve.getId(badgeUriResource)); JsonNode badgeNode = ((ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER)) .readTree(badgeObject.getJson()); // get issuer from badge - UriResource issuerUriResource = resolveUriResource(ctx, badgeNode.get("issuer").asText().strip()); + UriResource issuerUriResource = uriResourceFactory.of(badgeNode.get("issuer").asText().strip()); JsonLdGeneratedObject issuerObject = (JsonLdGeneratedObject) ctx.getGeneratedObject( JsonLDCompactionProve.getId(issuerUriResource)); @@ -71,7 +73,7 @@ public class VerificationJWTProbe extends Probe { } // get creator from id - UriResource creatorUriResource = resolveUriResource(ctx, creatorId); + UriResource creatorUriResource = uriResourceFactory.of(creatorId); JsonLdGeneratedObject creatorObject = (JsonLdGeneratedObject) ctx.getGeneratedObject( JsonLDCompactionProve.getId(creatorUriResource)); JsonNode creatorNode = ((ObjectMapper) ctx.get(Key.JACKSON_OBJECTMAPPER)) @@ -106,21 +108,6 @@ public class VerificationJWTProbe extends Probe { return success(ctx); } - protected UriResource resolveUriResource(RunContext ctx, String url) throws URISyntaxException { - URI uri = new URI(url); - UriResource initialUriResource = new UriResource(uri); - UriResource uriResource = initialUriResource; - - // check if uri points to a local resource - if (ctx.get(Key.JSON_DOCUMENT_LOADER) instanceof ConfigurableDocumentLoader) { - if (ConfigurableDocumentLoader.getDefaultHttpLoader() instanceof CachingDocumentLoader.HttpLoader) { - URI resolvedUri = ((CachingDocumentLoader.HttpLoader) ConfigurableDocumentLoader.getDefaultHttpLoader()).resolve(uri); - uriResource = new UriResource(resolvedUri); - } - } - return uriResource; - } - private static final List allowedTypes = List.of("id", "email", "url", "telephone"); public static final String ID = VerificationJWTProbe.class.getSimpleName(); diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/validation/ValidationImagePropertyProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/validation/ValidationImagePropertyProbe.java index e4123b4..30d3432 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/validation/ValidationImagePropertyProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/validation/ValidationImagePropertyProbe.java @@ -5,10 +5,12 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.oneedtech.inspect.core.probe.RunContext; +import org.oneedtech.inspect.core.probe.RunContext.Key; import org.oneedtech.inspect.core.report.ReportItems; import org.oneedtech.inspect.util.resource.MimeType; import org.oneedtech.inspect.util.resource.UriResource; import org.oneedtech.inspect.vc.Validation; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import org.oneedtech.inspect.vc.util.PrimitiveValueValidator; import com.fasterxml.jackson.databind.JsonNode; @@ -56,7 +58,7 @@ public class ValidationImagePropertyProbe extends ValidationPropertyProbe { } } else if (!url.isEmpty()) { try { - UriResource uriResource = resolveUriResource(ctx, url); + UriResource uriResource = ((UriResourceFactory) ctx.get(Key.URI_RESOURCE_FACTORY)).of(url); // TODO: load resource from cache // TODO: check accept type -> 'Accept': 'application/ld+json, application/json, image/png, image/svg+xml' uriResource.asByteSource(); diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/validation/ValidationPropertyProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/validation/ValidationPropertyProbe.java index b7fdc56..d3590f2 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/validation/ValidationPropertyProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/validation/ValidationPropertyProbe.java @@ -22,6 +22,7 @@ import org.oneedtech.inspect.vc.Validation; import org.oneedtech.inspect.vc.jsonld.JsonLdGeneratedObject; import org.oneedtech.inspect.vc.jsonld.probe.JsonLDCompactionProve; import org.oneedtech.inspect.vc.probe.PropertyProbe; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import org.oneedtech.inspect.vc.util.CachingDocumentLoader; import org.oneedtech.inspect.vc.util.JsonNodeUtil; @@ -126,7 +127,7 @@ public class ValidationPropertyProbe extends PropertyProbe { } // get node from context - UriResource uriResource = resolveUriResource(ctx, childNode.asText()); + UriResource uriResource = ((UriResourceFactory) ctx.get(Key.URI_RESOURCE_FACTORY)).of(childNode.asText().strip()); JsonLdGeneratedObject resolved = (JsonLdGeneratedObject) ctx.getGeneratedObject(JsonLDCompactionProve.getId(uriResource)); if (resolved == null) { if (validation.isAllowRemoteUrl() && URL.getValidationFunction().apply(childNode)) { @@ -153,21 +154,6 @@ public class ValidationPropertyProbe extends PropertyProbe { return result.size() > 0 ? result : success(ctx); } - protected UriResource resolveUriResource(RunContext ctx, String url) throws URISyntaxException { - URI uri = new URI(url); - UriResource initialUriResource = new UriResource(uri); - UriResource uriResource = initialUriResource; - - // check if uri points to a local resource - if (ctx.get(Key.JSON_DOCUMENT_LOADER) instanceof ConfigurableDocumentLoader) { - if (ConfigurableDocumentLoader.getDefaultHttpLoader() instanceof CachingDocumentLoader.HttpLoader) { - URI resolvedUri = ((CachingDocumentLoader.HttpLoader) ConfigurableDocumentLoader.getDefaultHttpLoader()).resolve(uri); - uriResource = new UriResource(resolvedUri); - } - } - return uriResource; - } - private ReportItems validatePrerequisites(JsonNode node, RunContext ctx) { List results = validation.getPrerequisites().stream() .map(v -> ValidationPropertyProbeFactory.of(validation.getName(), v, validation.isFullValidate())) diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/resource/DefaultUriResourceFactory.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/resource/DefaultUriResourceFactory.java new file mode 100644 index 0000000..2c117d1 --- /dev/null +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/resource/DefaultUriResourceFactory.java @@ -0,0 +1,19 @@ +package org.oneedtech.inspect.vc.resource; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.oneedtech.inspect.util.resource.UriResource; + +/** + * Default factory for URIResources + * @author xaracil + */ +public class DefaultUriResourceFactory implements UriResourceFactory { + + @Override + public UriResource of(String uri) throws URISyntaxException { + return new UriResource(new URI(uri)); + } + +} diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/resource/UriResourceFactory.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/resource/UriResourceFactory.java new file mode 100644 index 0000000..b5861f4 --- /dev/null +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/resource/UriResourceFactory.java @@ -0,0 +1,13 @@ +package org.oneedtech.inspect.vc.resource; + +import java.net.URISyntaxException; + +import org.oneedtech.inspect.util.resource.UriResource; + +/** + * Factory interface for URI resources + * @author xaracil + */ +public interface UriResourceFactory { + public UriResource of(String uri) throws URISyntaxException; +} diff --git a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/resource/TestUriResourceFactory.java b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/resource/TestUriResourceFactory.java new file mode 100644 index 0000000..b663bf5 --- /dev/null +++ b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/resource/TestUriResourceFactory.java @@ -0,0 +1,35 @@ +package org.oneedtech.inspect.vc.resource; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.oneedtech.inspect.util.resource.UriResource; +import org.oneedtech.inspect.vc.util.CachingDocumentLoader; + +import com.apicatalog.jsonld.loader.DocumentLoader; + +import foundation.identity.jsonld.ConfigurableDocumentLoader; + +/** + * UriResource factory for test, resolving local references + * @author xaracil + */ +public class TestUriResourceFactory implements UriResourceFactory { + + final DocumentLoader documentLoader; + + public TestUriResourceFactory(DocumentLoader documentLoader) { + this.documentLoader = documentLoader; + } + + @Override + public UriResource of(String uriString) throws URISyntaxException { + URI uri = new URI(uriString); + if (documentLoader instanceof CachingDocumentLoader) { + URI resolvedUri = ((CachingDocumentLoader.HttpLoader) ConfigurableDocumentLoader.getDefaultHttpLoader()).resolve(uri); + uri = resolvedUri; + } + return new UriResource(uri); + } + +} 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 2728550..c98b273 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 @@ -8,6 +8,8 @@ import java.util.Map; import org.oneedtech.inspect.util.resource.ResourceType; import org.oneedtech.inspect.util.spec.Specification; import org.oneedtech.inspect.vc.OB20Inspector; +import org.oneedtech.inspect.vc.resource.TestUriResourceFactory; +import org.oneedtech.inspect.vc.resource.UriResourceFactory; import com.apicatalog.jsonld.loader.DocumentLoader; @@ -32,6 +34,11 @@ public class TestOB20Inspector extends OB20Inspector { return new CachingDocumentLoader(localDomains); } + @Override + protected UriResourceFactory getUriResourceFactory(DocumentLoader documentLoader) { + return new TestUriResourceFactory(documentLoader); + } + public static class TestBuilder extends OB20Inspector.Builder { final Map localDomains;