parse now return the generic credential, with keys from context
This commit is contained in:
		
							parent
							
								
									75d7deffda
								
							
						
					
					
						commit
						6c9485cf02
					
				@ -4,10 +4,10 @@ import static java.nio.charset.StandardCharsets.UTF_8;
 | 
			
		||||
import static org.oneedtech.inspect.util.code.Defensives.checkTrue;
 | 
			
		||||
 | 
			
		||||
import org.oneedtech.inspect.core.probe.RunContext;
 | 
			
		||||
import org.oneedtech.inspect.core.probe.RunContext.Key;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.Resource;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.ResourceType;
 | 
			
		||||
import org.oneedtech.inspect.vc.Credential;
 | 
			
		||||
 | 
			
		||||
import org.oneedtech.inspect.vc.AbstractBaseCredential;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@ -22,11 +22,15 @@ public final class JsonParser extends PayloadParser {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Credential parse(Resource resource, RunContext ctx)  throws Exception {		
 | 
			
		||||
	public AbstractBaseCredential parse(Resource resource, RunContext ctx)  throws Exception {
 | 
			
		||||
		checkTrue(resource.getType() == ResourceType.JSON);
 | 
			
		||||
		String json = resource.asByteSource().asCharSource(UTF_8).read();
 | 
			
		||||
		JsonNode node = fromString(json, ctx);
 | 
			
		||||
		return new Credential(resource, node);				
 | 
			
		||||
 | 
			
		||||
		return getBuilder(ctx)
 | 
			
		||||
				.resource(resource)
 | 
			
		||||
				.jsonData(node)
 | 
			
		||||
				.build();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@ import static org.oneedtech.inspect.util.code.Defensives.checkTrue;
 | 
			
		||||
import org.oneedtech.inspect.core.probe.RunContext;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.Resource;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.ResourceType;
 | 
			
		||||
import org.oneedtech.inspect.vc.Credential;
 | 
			
		||||
import org.oneedtech.inspect.vc.AbstractBaseCredential;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
 | 
			
		||||
@ -22,11 +22,15 @@ public final class JwtParser extends PayloadParser {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Credential parse(Resource resource, RunContext ctx)  throws Exception {		
 | 
			
		||||
	public AbstractBaseCredential parse(Resource resource, RunContext ctx)  throws Exception {
 | 
			
		||||
		checkTrue(resource.getType() == ResourceType.JWT);
 | 
			
		||||
		String jwt = resource.asByteSource().asCharSource(UTF_8).read();
 | 
			
		||||
		JsonNode node = fromJwt(jwt, ctx);
 | 
			
		||||
		return new Credential(resource, node, jwt);				
 | 
			
		||||
		return getBuilder(ctx)
 | 
			
		||||
			.resource(resource)
 | 
			
		||||
			.jsonData(node)
 | 
			
		||||
			.jwt(jwt)
 | 
			
		||||
			.build();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -7,6 +7,7 @@ import java.util.Base64.Decoder;
 | 
			
		||||
import org.oneedtech.inspect.core.probe.RunContext;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.Resource;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.ResourceType;
 | 
			
		||||
import org.oneedtech.inspect.vc.AbstractBaseCredential;
 | 
			
		||||
import org.oneedtech.inspect.vc.Credential;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
@ -21,7 +22,12 @@ public abstract class PayloadParser {
 | 
			
		||||
 | 
			
		||||
	public abstract boolean supports(ResourceType type);
 | 
			
		||||
 | 
			
		||||
	public abstract Credential parse(Resource source, RunContext ctx) throws Exception;
 | 
			
		||||
	public abstract AbstractBaseCredential parse(Resource source, RunContext ctx) throws Exception;
 | 
			
		||||
 | 
			
		||||
	@SuppressWarnings("rawtypes")
 | 
			
		||||
	public static AbstractBaseCredential.Builder getBuilder(RunContext context) {
 | 
			
		||||
		return ((AbstractBaseCredential.Builder) context.get(RunContext.Key.GENERATED_OBJECT_BUILDER));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	protected static JsonNode fromString(String json, RunContext context) throws Exception {
 | 
			
		||||
		return ((ObjectMapper)context.get(RunContext.Key.JACKSON_OBJECTMAPPER)).readTree(json);
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,7 @@ import javax.imageio.metadata.IIOMetadata;
 | 
			
		||||
import org.oneedtech.inspect.core.probe.RunContext;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.Resource;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.ResourceType;
 | 
			
		||||
import org.oneedtech.inspect.vc.Credential;
 | 
			
		||||
import org.oneedtech.inspect.vc.AbstractBaseCredential;
 | 
			
		||||
import org.w3c.dom.NamedNodeMap;
 | 
			
		||||
import org.w3c.dom.Node;
 | 
			
		||||
 | 
			
		||||
@ -29,11 +29,12 @@ public final class PngParser extends PayloadParser {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Credential parse(Resource resource, RunContext ctx) throws Exception {
 | 
			
		||||
	public AbstractBaseCredential parse(Resource resource, RunContext ctx) throws Exception {
 | 
			
		||||
 | 
			
		||||
		checkTrue(resource.getType() == ResourceType.PNG);
 | 
			
		||||
 | 
			
		||||
		try(InputStream is = resource.asByteSource().openStream()) {
 | 
			
		||||
			final Keys credentialKey = (Keys) ctx.get(RunContext.Key.PNG_CREDENTIAL_KEY);
 | 
			
		||||
 | 
			
		||||
			ImageReader imageReader = ImageIO.getImageReadersByFormatName("png").next();
 | 
			
		||||
			imageReader.setInput(ImageIO.createImageInputStream(is), true);
 | 
			
		||||
@ -48,7 +49,7 @@ public final class PngParser extends PayloadParser {
 | 
			
		||||
			int length = names.length;
 | 
			
		||||
			for (int i = 0; i < length; i++) {
 | 
			
		||||
				//Check all names rather than limiting to PNG format to remain malleable through any library changes.  (Could limit to "javax_imageio_png_1.0")
 | 
			
		||||
				formatSearch = getOpenBadgeCredentialNodeText(metadata.getAsTree(names[i]));
 | 
			
		||||
				formatSearch = getOpenBadgeCredentialNodeText(metadata.getAsTree(names[i]), credentialKey);
 | 
			
		||||
				if(formatSearch != null) {
 | 
			
		||||
					vcString = formatSearch;
 | 
			
		||||
					break;
 | 
			
		||||
@ -69,16 +70,20 @@ public final class PngParser extends PayloadParser {
 | 
			
		||||
				vcNode = fromString(vcString, ctx);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			return new Credential(resource, vcNode, jwtString);
 | 
			
		||||
			return getBuilder(ctx)
 | 
			
		||||
					.resource(resource)
 | 
			
		||||
					.jsonData(vcNode)
 | 
			
		||||
					.jwt(jwtString)
 | 
			
		||||
					.build();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private String getOpenBadgeCredentialNodeText(Node node){
 | 
			
		||||
	private String getOpenBadgeCredentialNodeText(Node node, Keys credentialKey){
 | 
			
		||||
        NamedNodeMap attributes = node.getAttributes();
 | 
			
		||||
 | 
			
		||||
		//If this node is labeled with the attribute keyword: 'openbadgecredential' it is the right one.
 | 
			
		||||
        Node keyword = attributes.getNamedItem("keyword");
 | 
			
		||||
		if(keyword != null && keyword.getNodeValue().equals("openbadgecredential")){
 | 
			
		||||
		if(keyword != null && keyword.getNodeValue().equals(credentialKey.getNodeName())){
 | 
			
		||||
			Node textAttribute = attributes.getNamedItem("text");
 | 
			
		||||
			if(textAttribute != null) {
 | 
			
		||||
				return textAttribute.getNodeValue();
 | 
			
		||||
@ -88,7 +93,7 @@ public final class PngParser extends PayloadParser {
 | 
			
		||||
		//iterate over all children depth first and search for the credential node.
 | 
			
		||||
		Node child = node.getFirstChild();
 | 
			
		||||
		while (child != null) {
 | 
			
		||||
            String nodeValue = getOpenBadgeCredentialNodeText(child);
 | 
			
		||||
            String nodeValue = getOpenBadgeCredentialNodeText(child, credentialKey);
 | 
			
		||||
			if(nodeValue != null) {
 | 
			
		||||
				return nodeValue;
 | 
			
		||||
			}
 | 
			
		||||
@ -99,4 +104,19 @@ public final class PngParser extends PayloadParser {
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public enum Keys {
 | 
			
		||||
		OB20("openbadges"),
 | 
			
		||||
		OB30("openbadgecredential"),
 | 
			
		||||
		CLR20("clrcredential");
 | 
			
		||||
 | 
			
		||||
		private String nodeName;
 | 
			
		||||
 | 
			
		||||
		private Keys(String nodeName) {
 | 
			
		||||
			this.nodeName = nodeName;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public String getNodeName() {
 | 
			
		||||
			return nodeName;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -11,11 +11,10 @@ import javax.xml.stream.events.Characters;
 | 
			
		||||
import javax.xml.stream.events.XMLEvent;
 | 
			
		||||
 | 
			
		||||
import org.oneedtech.inspect.core.probe.RunContext;
 | 
			
		||||
import org.oneedtech.inspect.util.code.Defensives;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.Resource;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.ResourceType;
 | 
			
		||||
import org.oneedtech.inspect.util.xml.XMLInputFactoryCache;
 | 
			
		||||
import org.oneedtech.inspect.vc.Credential;
 | 
			
		||||
import org.oneedtech.inspect.vc.AbstractBaseCredential;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
 | 
			
		||||
@ -31,7 +30,8 @@ public final class SvgParser extends PayloadParser {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Credential parse(Resource resource, RunContext ctx)  throws Exception {
 | 
			
		||||
	public AbstractBaseCredential parse(Resource resource, RunContext ctx)  throws Exception {
 | 
			
		||||
		final QNames qNames = (QNames) ctx.get(RunContext.Key.SVG_CREDENTIAL_QNAME);
 | 
			
		||||
 | 
			
		||||
		checkTrue(resource.getType() == ResourceType.SVG);
 | 
			
		||||
 | 
			
		||||
@ -39,22 +39,22 @@ public final class SvgParser extends PayloadParser {
 | 
			
		||||
			XMLEventReader reader = XMLInputFactoryCache.getInstance().createXMLEventReader(is);
 | 
			
		||||
			while(reader.hasNext()) {
 | 
			
		||||
				XMLEvent ev = reader.nextEvent();
 | 
			
		||||
				if(isEndElem(ev, OB_CRED_ELEM)) break;					
 | 
			
		||||
				if(isStartElem(ev, OB_CRED_ELEM)) {
 | 
			
		||||
					Attribute verifyAttr = ev.asStartElement().getAttributeByName(OB_CRED_VERIFY_ATTR);
 | 
			
		||||
				if(isEndElem(ev, qNames.getCredentialElem())) break;
 | 
			
		||||
				if(isStartElem(ev, qNames.getCredentialElem())) {
 | 
			
		||||
					Attribute verifyAttr = ev.asStartElement().getAttributeByName(qNames.getVerifyElem());
 | 
			
		||||
					if(verifyAttr != null) {
 | 
			
		||||
						String jwt = verifyAttr.getValue();
 | 
			
		||||
						JsonNode node = fromJwt(jwt, ctx);
 | 
			
		||||
						return new Credential(resource, node, jwt);
 | 
			
		||||
						return getBuilder(ctx).resource(resource).jsonData(node).jwt(jwt).build();
 | 
			
		||||
					} else {
 | 
			
		||||
						while(reader.hasNext()) {
 | 
			
		||||
							ev = reader.nextEvent();
 | 
			
		||||
							if(isEndElem(ev, OB_CRED_ELEM)) break;							
 | 
			
		||||
							if(isEndElem(ev, qNames.getCredentialElem())) break;
 | 
			
		||||
							if(ev.getEventType() == XMLEvent.CHARACTERS) {
 | 
			
		||||
								Characters chars = ev.asCharacters();
 | 
			
		||||
								if(!chars.isWhiteSpace()) {
 | 
			
		||||
									JsonNode node = fromString(chars.getData(), ctx);
 | 
			
		||||
									return new Credential(resource, node);
 | 
			
		||||
									return getBuilder(ctx).resource(resource).jsonData(node).build();
 | 
			
		||||
								}
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
@ -74,6 +74,31 @@ public final class SvgParser extends PayloadParser {
 | 
			
		||||
		return ev.isStartElement() && ev.asStartElement().getName().equals(name);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static final QName OB_CRED_ELEM = new QName("https://purl.imsglobal.org/ob/v3p0", "credential");
 | 
			
		||||
	private static final QName OB_CRED_VERIFY_ATTR = new QName("verify");
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Know QNames whre the credential is baked into the SVG
 | 
			
		||||
	 */
 | 
			
		||||
	public enum QNames {
 | 
			
		||||
		OB20(new QName("http://openbadges.org", "assertion"), OB_CRED_VERIFY_ATTR),
 | 
			
		||||
		OB30(new QName("https://purl.imsglobal.org/ob/v3p0", "credential"), OB_CRED_VERIFY_ATTR),
 | 
			
		||||
		CLR20(new QName("https://purl.imsglobal.org/clr/v2p0", "credential"), OB_CRED_VERIFY_ATTR);
 | 
			
		||||
 | 
			
		||||
		private final QName credentialElem;
 | 
			
		||||
		private final QName verifyElem;
 | 
			
		||||
 | 
			
		||||
		private QNames(QName credentialElem, QName verifyElem) {
 | 
			
		||||
			this.credentialElem = credentialElem;
 | 
			
		||||
			this.verifyElem = verifyElem;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public QName getCredentialElem() {
 | 
			
		||||
			return credentialElem;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public QName getVerifyElem() {
 | 
			
		||||
			return verifyElem;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,7 @@ import org.oneedtech.inspect.core.report.ReportItems;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.Resource;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.ResourceType;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.detect.TypeDetector;
 | 
			
		||||
import org.oneedtech.inspect.vc.AbstractBaseCredential;
 | 
			
		||||
import org.oneedtech.inspect.vc.Credential;
 | 
			
		||||
import org.oneedtech.inspect.vc.payload.PayloadParserFactory;
 | 
			
		||||
 | 
			
		||||
@ -42,7 +43,7 @@ public class CredentialParseProbe extends Probe<Resource> {
 | 
			
		||||
				return fatal("Payload type not supported: " + type.get().getName(), context);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			Credential crd = PayloadParserFactory.of(resource).parse(resource, context);			
 | 
			
		||||
			AbstractBaseCredential crd = PayloadParserFactory.of(resource).parse(resource, context);
 | 
			
		||||
			context.addGeneratedObject(crd);
 | 
			
		||||
			return success(this, context);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,10 +1,9 @@
 | 
			
		||||
package org.oneedtech.inspect.vc.credential;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
 | 
			
		||||
import static org.oneedtech.inspect.util.json.ObjectMapperCache.Config.DEFAULT;
 | 
			
		||||
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.oneedtech.inspect.core.probe.RunContext;
 | 
			
		||||
import org.oneedtech.inspect.core.probe.RunContext.Key;
 | 
			
		||||
@ -12,7 +11,7 @@ import org.oneedtech.inspect.core.probe.json.JsonPathEvaluator;
 | 
			
		||||
import org.oneedtech.inspect.util.json.ObjectMapperCache;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.Resource;
 | 
			
		||||
import org.oneedtech.inspect.util.resource.ResourceType;
 | 
			
		||||
import org.oneedtech.inspect.vc.Credential;
 | 
			
		||||
import org.oneedtech.inspect.vc.AbstractBaseCredential;
 | 
			
		||||
import org.oneedtech.inspect.vc.OB30Inspector;
 | 
			
		||||
import org.oneedtech.inspect.vc.Samples;
 | 
			
		||||
import org.oneedtech.inspect.vc.payload.PayloadParser;
 | 
			
		||||
@ -28,7 +27,7 @@ public class PayloadParserTests {
 | 
			
		||||
			Resource res = Samples.OB30.SVG.SIMPLE_JSON_SVG.asFileResource(ResourceType.SVG);
 | 
			
		||||
			PayloadParser ext = PayloadParserFactory.of(res);
 | 
			
		||||
			assertNotNull(ext);
 | 
			
		||||
			Credential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			AbstractBaseCredential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			//System.out.println(crd.getJson().toPrettyString());
 | 
			
		||||
			assertNotNull(crd);
 | 
			
		||||
			assertNotNull(crd.getJson());
 | 
			
		||||
@ -42,7 +41,7 @@ public class PayloadParserTests {
 | 
			
		||||
			Resource res = Samples.OB30.SVG.SIMPLE_JWT_SVG.asFileResource(ResourceType.SVG);
 | 
			
		||||
			PayloadParser ext = PayloadParserFactory.of(res);
 | 
			
		||||
			assertNotNull(ext);
 | 
			
		||||
			Credential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			AbstractBaseCredential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			//System.out.println(crd.getJson().toPrettyString());
 | 
			
		||||
			assertNotNull(crd);
 | 
			
		||||
			assertNotNull(crd.getJson());
 | 
			
		||||
@ -56,7 +55,7 @@ public class PayloadParserTests {
 | 
			
		||||
			Resource res = Samples.OB30.PNG.SIMPLE_JSON_PNG.asFileResource(ResourceType.PNG);
 | 
			
		||||
			PayloadParser ext = PayloadParserFactory.of(res);
 | 
			
		||||
			assertNotNull(ext);
 | 
			
		||||
			Credential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			AbstractBaseCredential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			//System.out.println(crd.getJson().toPrettyString());
 | 
			
		||||
			assertNotNull(crd);
 | 
			
		||||
			assertNotNull(crd.getJson());
 | 
			
		||||
@ -70,7 +69,7 @@ public class PayloadParserTests {
 | 
			
		||||
			Resource res = Samples.OB30.PNG.SIMPLE_JWT_PNG.asFileResource(ResourceType.PNG);
 | 
			
		||||
			PayloadParser ext = PayloadParserFactory.of(res);
 | 
			
		||||
			assertNotNull(ext);
 | 
			
		||||
			Credential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			AbstractBaseCredential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			//System.out.println(crd.getJson().toPrettyString());
 | 
			
		||||
			assertNotNull(crd);
 | 
			
		||||
			assertNotNull(crd.getJson());
 | 
			
		||||
@ -84,7 +83,7 @@ public class PayloadParserTests {
 | 
			
		||||
			Resource res = Samples.OB30.JWT.SIMPLE_JWT.asFileResource(ResourceType.JWT);
 | 
			
		||||
			PayloadParser ext = PayloadParserFactory.of(res);
 | 
			
		||||
			assertNotNull(ext);
 | 
			
		||||
			Credential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			AbstractBaseCredential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			//System.out.println(crd.getJson().toPrettyString());
 | 
			
		||||
			assertNotNull(crd);
 | 
			
		||||
			assertNotNull(crd.getJson());
 | 
			
		||||
@ -98,7 +97,7 @@ public class PayloadParserTests {
 | 
			
		||||
			Resource res = Samples.OB30.JSON.SIMPLE_JSON.asFileResource(ResourceType.JSON);
 | 
			
		||||
			PayloadParser ext = PayloadParserFactory.of(res);
 | 
			
		||||
			assertNotNull(ext);
 | 
			
		||||
			Credential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			AbstractBaseCredential crd = ext.parse(res, mockOB30Context(res));
 | 
			
		||||
			//System.out.println(crd.getJson().toPrettyString());
 | 
			
		||||
			assertNotNull(crd);
 | 
			
		||||
			assertNotNull(crd.getJson());
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user