Added issuer property validations

This commit is contained in:
Xavi Aracil 2022-11-30 18:49:31 +01:00
parent 92ad87c6bc
commit d6e0d85d2f
4 changed files with 64 additions and 5 deletions

View File

@ -9,6 +9,7 @@ import java.util.function.Function;
import org.oneedtech.inspect.schema.Catalog;
import org.oneedtech.inspect.schema.SchemaKey;
import org.oneedtech.inspect.util.resource.Resource;
import org.oneedtech.inspect.vc.Validation.MessageLevel;
import org.oneedtech.inspect.vc.util.JsonNodeUtil;
import org.oneedtech.inspect.vc.util.PrimitiveValueValidator;
@ -148,7 +149,8 @@ public class Assertion extends Credential {
URL(PrimitiveValueValidator::validateUrl),
URL_AUTHORITY(PrimitiveValueValidator::validateUrlAuthority),
IMAGE(null);
IMAGE(null),
ISSUER(null);
private final Function<JsonNode, Boolean> validationFunction;
@ -264,7 +266,8 @@ public class Assertion extends Credential {
new Validation.Builder().name("email").type(ValueType.EMAIL).required(true).build(),
new Validation.Builder().name("telephone").type(ValueType.TELEPHONE).required(false).build(),
new Validation.Builder().name("publicKey").type(ValueType.ID).expectedType(Type.CryptographicKey).fetch(true).required(false).build(),
new Validation.Builder().name("verification").type(ValueType.ID).expectedType(Type.VerificationObjectIssuer).fetch(false).required(false).build()
new Validation.Builder().name("verification").type(ValueType.ID).expectedType(Type.VerificationObjectIssuer).fetch(false).required(false).build(),
new Validation.Builder().name("id").type(ValueType.ISSUER).required(false).messageLevel(MessageLevel.Warning).build()
))
.put(Type.Profile, List.of(
new Validation.Builder().name("id").type(ValueType.IRI).required(true).build(),
@ -276,7 +279,8 @@ public class Assertion extends Credential {
new Validation.Builder().name("email").type(ValueType.EMAIL).required(true).build(),
new Validation.Builder().name("telephone").type(ValueType.TELEPHONE).required(false).build(),
new Validation.Builder().name("publicKey").type(ValueType.ID).expectedType(Type.CryptographicKey).fetch(true).required(false).build(),
new Validation.Builder().name("verification").type(ValueType.ID).expectedType(Type.VerificationObjectIssuer).fetch(false).required(false).build()
new Validation.Builder().name("verification").type(ValueType.ID).expectedType(Type.VerificationObjectIssuer).fetch(false).required(false).build(),
new Validation.Builder().name("id").type(ValueType.ISSUER).required(false).messageLevel(MessageLevel.Warning).build()
))
.put(Type.RevocationList, List.of(
new Validation.Builder().name("type").type(ValueType.RDF_TYPE).required(true).many(true).mustContainOneType(List.of(Type.RevocationList)).build(),

View File

@ -20,6 +20,7 @@ public class Validation {
private final boolean fetch;
private final String defaultType;
private final boolean fullValidate;
private MessageLevel messageLevel;
public Validation(Builder builder) {
this.name = builder.name;
@ -34,9 +35,9 @@ public class Validation {
this.fetch = builder.fetch;
this.defaultType = builder.defaultType;
this.fullValidate = builder.fullValidate;
this.messageLevel = builder.messageLevel;
}
public String getName() {
return name;
}
@ -85,6 +86,15 @@ public class Validation {
return fullValidate;
}
public MessageLevel getMessageLevel() {
return messageLevel;
}
public enum MessageLevel {
Warning,
Error
}
public static class Builder {
private String name;
private Assertion.ValueType type;
@ -98,11 +108,13 @@ public class Validation {
private boolean fetch;
private String defaultType;
private boolean fullValidate;
private MessageLevel messageLevel;
public Builder() {
this.mustContainOne = new ArrayList<>();
this.prerequisites = new ArrayList<>();
this.expectedTypes = new ArrayList<>();
this.messageLevel = MessageLevel.Error;
}
public Builder name(String name) {
@ -184,6 +196,11 @@ public class Validation {
return this;
}
public Builder messageLevel(MessageLevel messageLevel) {
this.messageLevel = messageLevel;
return this;
}
public Validation build() {
return new Validation(this);
}

View File

@ -0,0 +1,35 @@
package org.oneedtech.inspect.vc.probe.validation;
import org.oneedtech.inspect.core.probe.RunContext;
import org.oneedtech.inspect.core.report.ReportItems;
import org.oneedtech.inspect.vc.Validation;
import org.oneedtech.inspect.vc.Validation.MessageLevel;
import com.fasterxml.jackson.databind.JsonNode;
public class ValidationIssuerPropertyProbe extends ValidationPropertyProbe {
public ValidationIssuerPropertyProbe(Validation validation) {
super(validation);
}
public ValidationIssuerPropertyProbe(Validation validation, boolean fullValidate) {
super(validation, fullValidate);
}
@Override
protected ReportItems validate(JsonNode node, RunContext ctx) {
if (!node.asText().matches("^http(s)?://")) {
return buildResponse("Issuer Profile " + node.toString() + " not hosted with HTTP-based identifier." +
"Many platforms can only handle HTTP(s)-hosted issuers.", ctx);
}
return success(ctx);
}
private ReportItems buildResponse(String msg, RunContext ctx) {
if (validation.getMessageLevel() == MessageLevel.Warning) {
return warning(msg, ctx);
}
return error(msg, ctx);
}
}

View File

@ -2,8 +2,8 @@ package org.oneedtech.inspect.vc.probe.validation;
import static org.oneedtech.inspect.util.code.Defensives.checkNotNull;
import org.oneedtech.inspect.vc.Validation;
import org.oneedtech.inspect.vc.Assertion.ValueType;
import org.oneedtech.inspect.vc.Validation;
/**
* Factory for ValidationPropertyProbes
@ -22,6 +22,9 @@ public class ValidationPropertyProbeFactory {
if (validation.getType() == ValueType.IMAGE) {
return new ValidationImagePropertyProbe(validation);
}
if (validation.getType() == ValueType.ISSUER) {
return new ValidationIssuerPropertyProbe(validation);
}
return new ValidationPropertyProbe(validation, fullValidate);
}
}