package org.nanopub.extra.security;

import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;

import org.nanopub.Nanopub;
import org.nanopub.NanopubPattern;

// TODO Update to new signature scheme

public class DigitalSignaturePattern implements NanopubPattern {

	private static final long serialVersionUID = 669651544354988407L;

	@Override
	public String getName() {
		return "Digitally signed nanopublication";
	}

	@Override
	public boolean appliesTo(Nanopub nanopub) {
		try {
			return LegacySignatureUtils.getSignatureElement(nanopub) != null;
		} catch (MalformedSignatureException ex) {
			return true;
		}
	}

	@Override
	public boolean isCorrectlyUsedBy(Nanopub nanopub) {
		NanopubSignatureElement se;
		try {
			se = LegacySignatureUtils.getSignatureElement(nanopub);
		} catch (MalformedSignatureException ex) {
			return false;
		}
		if (se == null) {
			return false;
		} else {
			try {
				return LegacySignatureUtils.hasValidSignature(se);
			} catch (GeneralSecurityException ex) {
				return false;
			}
		}
	}

	@Override
	public String getDescriptionFor(Nanopub nanopub) {
		if (isCorrectlyUsedBy(nanopub)) {
			return "Valid digital signature";
		} else {
			return "Digital signature is not valid";
		}
	}

	@Override
	public URL getPatternInfoUrl() throws MalformedURLException {
		return new URL("https://github.com/Nanopublication/nanopub-java/blob/master/src/main/java/org/nanopub/extra/security/CheckSignature.java");
	}

}

package org.nanopub.extra.security;

import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;

import org.nanopub.Nanopub;
import org.nanopub.NanopubPattern;

public class DigitalSignaturePattern implements NanopubPattern {

	private static final long serialVersionUID = 669651544354988407L;

	@Override
	public String getName() {
		return "Digitally signed nanopublication";
	}

	@Override
	public boolean appliesTo(Nanopub nanopub) {
		return SignatureUtils.seemsToHaveSignature(nanopub);
	}

	@Override
	public boolean isCorrectlyUsedBy(Nanopub nanopub) {
		try {
			NanopubSignatureElement se = SignatureUtils.getSignatureElement(nanopub);
			if (se != null) {
				return SignatureUtils.hasValidSignature(se);
			} else {
				se = LegacySignatureUtils.getSignatureElement(nanopub);
				if (se != null) {
					return LegacySignatureUtils.hasValidSignature(se);
				} else {
					return false;
				}
			}
		} catch (MalformedSignatureException | GeneralSecurityException ex) {
			return false;
		}
	}

	@Override
	public String getDescriptionFor(Nanopub nanopub) {
		if (isCorrectlyUsedBy(nanopub)) {
			if (hasLegacySignature(nanopub)) {
				return "Valid digital signature (LEGACY)";
			} else {
				return "Valid digital signature";
			}
		} else {
			return "Digital signature is not valid";
		}
	}

	private boolean hasLegacySignature(Nanopub nanopub) {
		try {
			return LegacySignatureUtils.getSignatureElement(nanopub) != null;
		} catch (MalformedSignatureException ex) {
			throw new RuntimeException(ex);
		}
	}

	@Override
	public URL getPatternInfoUrl() throws MalformedURLException {
		return new URL("https://github.com/Nanopublication/nanopub-java/blob/master/src/main/java/org/nanopub/extra/security/NanopubSignatureElement.java");
	}

}

Update digital signature pattern to recognize new signatures