diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidation.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidation.java index 7c1199e6..657d584b 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidation.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidation.java @@ -21,10 +21,12 @@ package org.eclipse.tractusx.ssi.lib.proof; +import java.util.logging.Logger; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver; +import org.eclipse.tractusx.ssi.lib.exception.InvalidJsonLdException; import org.eclipse.tractusx.ssi.lib.exception.UnsupportedSignatureTypeException; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable.VerifiableType; @@ -36,12 +38,13 @@ import org.eclipse.tractusx.ssi.lib.proof.transform.TransformedLinkedData; import org.eclipse.tractusx.ssi.lib.proof.types.ed25519.Ed25519ProofVerifier; import org.eclipse.tractusx.ssi.lib.proof.types.jws.JWSProofVerifier; -import org.eclipse.tractusx.ssi.lib.serialization.jsonLd.JsonLdValidator; -import org.eclipse.tractusx.ssi.lib.serialization.jsonLd.JsonLdValidatorImpl; +import org.eclipse.tractusx.ssi.lib.validation.JsonLdValidator; +import org.eclipse.tractusx.ssi.lib.validation.JsonLdValidatorImpl; /** The type Linked data proof validation. */ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class LinkedDataProofValidation { + static final Logger LOG = Logger.getLogger(LinkedDataProofValidation.class.getName()); /** * New instance linked data proof validation. @@ -99,9 +102,14 @@ public boolean verify(Verifiable verifiable) { final TransformedLinkedData transformedData = transformer.transform(verifiable); final HashedLinkedData hashedData = hasher.hash(transformedData); - return jsonLdValidator.validate(verifiable) - && verifier.verify(hashedData, verifiable) - && validateVerificationMethodOfVC(verifiable); + try { + jsonLdValidator.validate(verifiable); + return verifier.verify(hashedData, verifiable) && validateVerificationMethodOfVC(verifiable); + } catch (InvalidJsonLdException e) { + LOG.severe("Could not valiate " + verifiable.getId()); + LOG.throwing(this.getClass().getName(), "verify", e); + return false; + } } /** diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdValidator.java b/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdValidator.java deleted file mode 100644 index d2f7a258..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdValidator.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.serialization.jsonLd; - -import org.eclipse.tractusx.ssi.lib.model.JsonLdObject; - -/** The interface Json ld validator. */ -public interface JsonLdValidator { - - /** - * Validate json ld object.. - * - * @param jsonLdObject the json ld object - * @return the boolean - */ - public boolean validate(JsonLdObject jsonLdObject); -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdValidatorImpl.java b/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdValidatorImpl.java deleted file mode 100644 index 6fae9d38..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdValidatorImpl.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.serialization.jsonLd; - -import com.apicatalog.jsonld.JsonLdError; -import com.apicatalog.jsonld.JsonLdOptions; -import com.apicatalog.jsonld.document.JsonDocument; -import com.apicatalog.jsonld.http.media.MediaType; -import com.apicatalog.jsonld.processor.ExpansionProcessor; -import jakarta.json.Json; -import jakarta.json.JsonArray; -import jakarta.json.JsonObject; -import jakarta.json.JsonValue; -import java.util.Map; -import org.eclipse.tractusx.ssi.lib.model.JsonLdObject; -import org.eclipse.tractusx.ssi.lib.model.RemoteDocumentLoader; - -/** The type Json ld validator. */ -public class JsonLdValidatorImpl implements JsonLdValidator { - - private static final String UNDEFINED_TERM_URI = "urn:UNDEFINEDTERM"; - - private static void findUndefinedTerms(JsonArray jsonArray) { - - for (JsonValue entry : jsonArray) { - - if (entry instanceof JsonObject) { - findUndefinedTerms((JsonObject) entry); - } - } - } - - private static void findUndefinedTerms(JsonObject jsonObject) { - - for (Map.Entry entry : jsonObject.entrySet()) { - - if (entry.getKey().startsWith(UNDEFINED_TERM_URI)) { - - throw new RuntimeException( - "Undefined JSON-LD term: " + entry.getKey().substring(UNDEFINED_TERM_URI.length())); - } - - if (entry.getValue() instanceof JsonArray) { - findUndefinedTerms((JsonArray) entry.getValue()); - } - if (entry.getValue() instanceof JsonObject) { - findUndefinedTerms((JsonObject) entry.getValue()); - } - } - } - - private void validateJsonLd(JsonLdObject jsonLdObject) { - - try { - - JsonObject expandContext = - Json.createObjectBuilder().add("@vocab", Json.createValue(UNDEFINED_TERM_URI)).build(); - - JsonDocument jsonDocument = JsonDocument.of(MediaType.JSON_LD, jsonLdObject.toJsonObject()); - - var documentLoader = RemoteDocumentLoader.getInstance(); - documentLoader.setEnableHttps(true); - documentLoader.setHttpsContexts(jsonLdObject.getContext()); - - JsonLdOptions jsonLdOptions = new JsonLdOptions(); - jsonLdOptions.setDocumentLoader(documentLoader); - jsonLdOptions.setExpandContext(expandContext); - - JsonArray jsonArray = ExpansionProcessor.expand(jsonDocument, jsonLdOptions, true); - JsonObject jsonObject = jsonArray.getJsonObject(0); - - findUndefinedTerms(jsonObject); - } catch (JsonLdError ex) { - throw new RuntimeException(ex.getMessage(), ex); - } - } - - public boolean validate(JsonLdObject jsonLdObject) throws IllegalStateException { - validateJsonLd(jsonLdObject); - return true; // Todo better handling - } -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidator.java b/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidator.java index a329c7b1..3a9df3c7 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidator.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidator.java @@ -22,25 +22,9 @@ package org.eclipse.tractusx.ssi.lib.validation; import org.eclipse.tractusx.ssi.lib.exception.InvalidJsonLdException; -import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; -import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentation; +import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; -/** The interface Json ld validator. */ public interface JsonLdValidator { - /** - * Validate. - * - * @param verifiablePresentation the verifiable presentation - * @throws InvalidJsonLdException the invalid json ld exception - */ - void validate(VerifiablePresentation verifiablePresentation) throws InvalidJsonLdException; - - /** - * Validate. - * - * @param verifiableCredential the verifiable credential - * @throws InvalidJsonLdException the invalid json ld exception - */ - void validate(VerifiableCredential verifiableCredential) throws InvalidJsonLdException; + void validate(Verifiable verifiable) throws InvalidJsonLdException; } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidatorImpl.java b/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidatorImpl.java index 81957647..9e3ff27b 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidatorImpl.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidatorImpl.java @@ -31,29 +31,35 @@ import jakarta.json.JsonObject; import jakarta.json.JsonValue; import java.util.Map; +import java.util.logging.Logger; import org.eclipse.tractusx.ssi.lib.exception.InvalidJsonLdException; import org.eclipse.tractusx.ssi.lib.model.JsonLdObject; import org.eclipse.tractusx.ssi.lib.model.RemoteDocumentLoader; +import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentation; /** The type Json ld validator. */ public class JsonLdValidatorImpl implements JsonLdValidator { + static final Logger LOG = Logger.getLogger(JsonLdValidatorImpl.class.getName()); private static final String UNDEFINED_TERM_URI = "urn:UNDEFINEDTERM"; - public void validate(VerifiablePresentation verifiablePresentation) - throws InvalidJsonLdException { - for (VerifiableCredential verifiableCredential : - verifiablePresentation.getVerifiableCredentials()) { - validate(verifiableCredential); + public void validate(Verifiable verifiable) throws InvalidJsonLdException { + if (verifiable instanceof VerifiableCredential) { + validateJsonLd(verifiable); + } else if (verifiable instanceof VerifiablePresentation) { + VerifiablePresentation verifiablePresentation = (VerifiablePresentation) verifiable; + for (VerifiableCredential verifiableCredential : + verifiablePresentation.getVerifiableCredentials()) { + validate(verifiableCredential); + } + } else { + LOG.warning("Unsupported Verifiable type: " + verifiable.getClass().getName()); + throw new InvalidJsonLdException( + String.format("Unsupported Verifiable type: %s", verifiable.getClass().getName())); } } - @Override - public void validate(VerifiableCredential verifiableCredential) throws InvalidJsonLdException { - validateJsonLd(verifiableCredential); - } - private void validateJsonLd(JsonLdObject jsonLdObject) throws InvalidJsonLdException { try {