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");
	}

}

@@ -206,48 +206,8 @@
 n;%0A%0A
-// TODO Update to new signature scheme%0A%0A
 publ
@@ -479,25 +479,16 @@
 opub) %7B%0A
-%09%09try %7B%0A%09
 %09%09return
@@ -480,38 +480,32 @@
 pub) %7B%0A%09%09return 
-Legacy
 SignatureUtils.g
@@ -495,35 +495,43 @@
  SignatureUtils.
-get
+seemsToHave
 SignatureElement
@@ -515,39 +515,32 @@
 sToHaveSignature
-Element
 (nanopub) != nul
@@ -536,82 +536,9 @@
 pub)
- != null;%0A%09%09%7D catch (MalformedSignatureException ex) %7B%0A%09%09%09return true;%0A%09%09%7D
+;
 %0A%09%7D%0A
@@ -602,16 +602,25 @@
 opub) %7B%0A
+%09%09try %7B%0A%09
 %09%09Nanopu
@@ -643,32 +643,11 @@
 t se
-;%0A%09%09try %7B%0A%09%09%09se = Legacy
+ = 
 Sign
@@ -693,196 +693,310 @@
 ;%0A%09%09
-%7D catch (MalformedSignatureException ex) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09if (
+%09if (se != null) %7B%0A%09%09%09%09return SignatureUtils.hasValidSignature(se);%0A%09%09%09%7D else %7B%0A%09%09%09%09
 se =
-= null) %7B%0A%09%09%09return false;%0A%09%09%7D else %7B%0A%09%09%09try %7B%0A%09%09%09%09return LegacySignatureUtils.hasValidSignature(se);%0A%09%09%09%7D catch (
+ LegacySignatureUtils.getSignatureElement(nanopub);%0A%09%09%09%09if (se != null) %7B%0A%09%09%09%09%09return LegacySignatureUtils.hasValidSignature(se);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D catch (MalformedSignatureException %7C 
 Gene
@@ -1021,25 +1021,24 @@
 on ex) %7B%0A%09%09%09
-%09
 return false
@@ -1039,21 +1039,16 @@
  false;%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A
@@ -1153,95 +1153,418 @@
 %0A%09%09%09
-return %22Valid digital signature%22;%0A%09%09%7D else %7B%0A%09%09%09return %22Digital signature is not valid%22
+if (hasLegacySignature(nanopub)) %7B%0A%09%09%09%09return %22Valid digital signature (LEGACY)%22;%0A%09%09%09%7D else %7B%0A%09%09%09%09return %22Valid digital signature%22;%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09return %22Digital signature is not valid%22;%0A%09%09%7D%0A%09%7D%0A%0A%09private boolean hasLegacySignature(Nanopub nanopub) %7B%0A%09%09try %7B%0A%09%09%09return LegacySignatureUtils.getSignatureElement(nanopub) != null;%0A%09%09%7D catch (MalformedSignatureException ex) %7B%0A%09%09%09throw new RuntimeException(ex)
 ;%0A%09%09
@@ -1766,13 +1766,15 @@
 ity/
-Check
+Nanopub
 Sign
@@ -1778,16 +1778,23 @@
 ignature
+Element
 .java%22);

Update digital signature pattern to recognize new signatures