Added Assertion as a sibling of Credential
This commit is contained in:
parent
21fe60f21b
commit
75d7deffda
@ -0,0 +1,110 @@
|
||||
package org.oneedtech.inspect.vc;
|
||||
|
||||
import static org.oneedtech.inspect.util.code.Defensives.*;
|
||||
import static org.oneedtech.inspect.util.resource.ResourceType.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.oneedtech.inspect.core.probe.GeneratedObject;
|
||||
import org.oneedtech.inspect.schema.SchemaKey;
|
||||
import org.oneedtech.inspect.util.resource.Resource;
|
||||
import org.oneedtech.inspect.util.resource.ResourceType;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.google.common.base.MoreObjects;
|
||||
|
||||
|
||||
/**
|
||||
* Base credential class for OB 2.0 Assertions and OB 3.0 and CLR 2.0 Credentials.
|
||||
* This contains e.g. the origin resource and the extracted JSON data.
|
||||
* @author xaracil
|
||||
*/
|
||||
public abstract class AbstractBaseCredential extends GeneratedObject {
|
||||
final Resource resource;
|
||||
final JsonNode jsonData;
|
||||
final String jwt;
|
||||
final Map<String, SchemaKey> schemas;
|
||||
|
||||
protected AbstractBaseCredential(String id, Resource resource, JsonNode data, String jwt, Map<String, SchemaKey> schemas) {
|
||||
super(id, GeneratedObject.Type.INTERNAL);
|
||||
this.resource = checkNotNull(resource);
|
||||
this.jsonData = checkNotNull(data);
|
||||
this.jwt = jwt; //may be null
|
||||
this.schemas = schemas;
|
||||
|
||||
checkTrue(RECOGNIZED_PAYLOAD_TYPES.contains(resource.getType()));
|
||||
}
|
||||
|
||||
public Resource getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
public JsonNode getJson() {
|
||||
return jsonData;
|
||||
}
|
||||
|
||||
public Optional<String> getJwt() {
|
||||
return Optional.ofNullable(jwt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the canonical schema for this credential if such exists.
|
||||
*/
|
||||
public Optional<SchemaKey> getSchemaKey() {
|
||||
return Optional.ofNullable(schemas.get(getCredentialType()));
|
||||
}
|
||||
|
||||
public abstract String getCredentialType();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper(this)
|
||||
.add("resource", resource.getID())
|
||||
.add("resourceType", resource.getType())
|
||||
.add("json", jsonData)
|
||||
.add("jwt", jwt)
|
||||
.toString();
|
||||
}
|
||||
|
||||
public static final List<ResourceType> RECOGNIZED_PAYLOAD_TYPES = List.of(SVG, PNG, JSON, JWT);
|
||||
public static final String CREDENTIAL_KEY = "CREDENTIAL_KEY";
|
||||
|
||||
public abstract static class Builder<B extends AbstractBaseCredential> {
|
||||
private Resource resource;
|
||||
private JsonNode jsonData;
|
||||
private String jwt;
|
||||
|
||||
public abstract B build();
|
||||
|
||||
public Builder<B> resource(Resource resource) {
|
||||
this.resource = resource;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder<B> jsonData(JsonNode node) {
|
||||
this.jsonData = node;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder<B> jwt(String jwt) {
|
||||
this.jwt = jwt;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected Resource getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
protected JsonNode getJsonData() {
|
||||
return jsonData;
|
||||
}
|
||||
|
||||
protected String getJwt() {
|
||||
return jwt;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package org.oneedtech.inspect.vc;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.oneedtech.inspect.schema.Catalog;
|
||||
import org.oneedtech.inspect.schema.SchemaKey;
|
||||
import org.oneedtech.inspect.util.resource.Resource;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.google.common.base.MoreObjects;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
/**
|
||||
* A wrapper object for a OB 2.0 assertion. This contains e.g. the origin resource
|
||||
* and the extracted JSON data plus any other stuff Probes need.
|
||||
* @author xaracil
|
||||
*/
|
||||
public class Assertion extends AbstractBaseCredential {
|
||||
|
||||
final Assertion.Type assertionType;
|
||||
|
||||
protected Assertion(Resource resource, JsonNode data, String jwt, Map<String, SchemaKey> schemas) {
|
||||
super(ID, resource, data, jwt, schemas);
|
||||
|
||||
ArrayNode typeNode = (ArrayNode)jsonData.get("type");
|
||||
this.assertionType = Assertion.Type.valueOf(typeNode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCredentialType() {
|
||||
return assertionType.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper(this)
|
||||
.add("super", super.toString())
|
||||
.add("assertionType", assertionType)
|
||||
.toString();
|
||||
}
|
||||
|
||||
private static final Map<Assertion.Type, SchemaKey> schemas = new ImmutableMap.Builder<Assertion.Type, SchemaKey>()
|
||||
.put(Type.Assertion, Catalog.OB_21_ASSERTION_JSON)
|
||||
.build();
|
||||
|
||||
public static class Builder extends AbstractBaseCredential.Builder<Assertion> {
|
||||
|
||||
@Override
|
||||
public Assertion build() {
|
||||
// transform key of schemas map to string because the type of the key in the base map is generic
|
||||
// and our specific key is an Enum
|
||||
return new Assertion(getResource(), getJsonData(), getJwt(),
|
||||
schemas.entrySet().stream().collect(Collectors.toMap(
|
||||
entry -> entry.getKey().toString(),
|
||||
entry -> entry.getValue())));
|
||||
}
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
Assertion,
|
||||
Unknown;
|
||||
|
||||
public static Assertion.Type valueOf (ArrayNode typeArray) {
|
||||
if(typeArray != null) {
|
||||
Iterator<JsonNode> iter = typeArray.iterator();
|
||||
while(iter.hasNext()) {
|
||||
String value = iter.next().asText();
|
||||
if(value.equals("Assertion")) {
|
||||
return Assertion;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
public static final String ID = Assertion.class.getCanonicalName();
|
||||
}
|
@ -1,19 +1,17 @@
|
||||
package org.oneedtech.inspect.vc;
|
||||
|
||||
import static org.oneedtech.inspect.util.code.Defensives.*;
|
||||
import static org.oneedtech.inspect.util.resource.ResourceType.*;
|
||||
import static org.oneedtech.inspect.vc.Credential.Type.*;
|
||||
import static org.oneedtech.inspect.vc.Credential.Type.AchievementCredential;
|
||||
import static org.oneedtech.inspect.vc.Credential.Type.ClrCredential;
|
||||
import static org.oneedtech.inspect.vc.Credential.Type.EndorsementCredential;
|
||||
import static org.oneedtech.inspect.vc.Credential.Type.VerifiablePresentation;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.oneedtech.inspect.core.probe.GeneratedObject;
|
||||
import org.oneedtech.inspect.schema.Catalog;
|
||||
import org.oneedtech.inspect.schema.SchemaKey;
|
||||
import org.oneedtech.inspect.util.resource.Resource;
|
||||
import org.oneedtech.inspect.util.resource.ResourceType;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
@ -25,49 +23,24 @@ import com.google.common.collect.ImmutableMap;
|
||||
* and the extracted JSON data plus any other stuff Probes need.
|
||||
* @author mgylling
|
||||
*/
|
||||
public class Credential extends GeneratedObject {
|
||||
final Resource resource;
|
||||
final JsonNode jsonData;
|
||||
public class Credential extends AbstractBaseCredential {
|
||||
final Credential.Type credentialType;
|
||||
final String jwt;
|
||||
|
||||
public Credential(Resource resource, JsonNode data, String jwt) {
|
||||
super(ID, GeneratedObject.Type.INTERNAL);
|
||||
this.resource = checkNotNull(resource);
|
||||
this.jsonData = checkNotNull(data);
|
||||
this.jwt = jwt; //may be null
|
||||
|
||||
checkTrue(RECOGNIZED_PAYLOAD_TYPES.contains(resource.getType()));
|
||||
protected Credential(Resource resource, JsonNode data, String jwt, Map<String, SchemaKey> schemas) {
|
||||
super(ID, resource, data, jwt, schemas);
|
||||
|
||||
ArrayNode typeNode = (ArrayNode)jsonData.get("type");
|
||||
this.credentialType = Credential.Type.valueOf(typeNode);
|
||||
}
|
||||
}
|
||||
|
||||
public Credential(Resource resource, JsonNode data) {
|
||||
this(resource, data, null);
|
||||
}
|
||||
|
||||
public Resource getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
public JsonNode getJson() {
|
||||
return jsonData;
|
||||
}
|
||||
|
||||
public Credential.Type getCredentialType() {
|
||||
return credentialType;
|
||||
}
|
||||
|
||||
public Optional<String> getJwt() {
|
||||
return Optional.ofNullable(jwt);
|
||||
public String getCredentialType() {
|
||||
return credentialType.toString();
|
||||
}
|
||||
|
||||
public ProofType getProofType() {
|
||||
return jwt == null ? ProofType.EMBEDDED : ProofType.EXTERNAL;
|
||||
}
|
||||
|
||||
|
||||
private static final Map<Credential.Type, SchemaKey> schemas = new ImmutableMap.Builder<Credential.Type, SchemaKey>()
|
||||
.put(AchievementCredential, Catalog.OB_30_ACHIEVEMENTCREDENTIAL_JSON)
|
||||
.put(ClrCredential, Catalog.CLR_20_CLRCREDENTIAL_JSON)
|
||||
@ -75,12 +48,6 @@ public class Credential extends GeneratedObject {
|
||||
.put(EndorsementCredential, Catalog.OB_30_ENDORSEMENTCREDENTIAL_JSON)
|
||||
.build();
|
||||
|
||||
/**
|
||||
* Get the canonical schema for this credential if such exists.
|
||||
*/
|
||||
public Optional<SchemaKey> getSchemaKey() {
|
||||
return Optional.ofNullable(schemas.get(credentialType));
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
AchievementCredential,
|
||||
@ -119,15 +86,23 @@ public class Credential extends GeneratedObject {
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper(this)
|
||||
.add("resource", resource.getID())
|
||||
.add("resourceType", resource.getType())
|
||||
.add("super", super.toString())
|
||||
.add("credentialType", credentialType)
|
||||
.add("json", jsonData)
|
||||
.toString();
|
||||
}
|
||||
|
||||
public static class Builder extends AbstractBaseCredential.Builder<Credential> {
|
||||
@Override
|
||||
public Credential build() {
|
||||
// transform key of schemas map to string because the type of the key in the base map is generic
|
||||
// and our specific key is an Enum
|
||||
return new Credential(getResource(), getJsonData(), getJwt(),
|
||||
schemas.entrySet().stream().collect(Collectors.toMap(
|
||||
entry -> entry.getKey().toString(),
|
||||
entry -> entry.getValue())));
|
||||
}
|
||||
}
|
||||
|
||||
public static final String ID = Credential.class.getCanonicalName();
|
||||
public static final List<ResourceType> RECOGNIZED_PAYLOAD_TYPES = List.of(SVG, PNG, JSON, JWT);
|
||||
public static final String CREDENTIAL_KEY = "CREDENTIAL_KEY";
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user