From be082ab863879e999c7047341c3eb359cd40af19 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 10 Jun 2018 01:43:28 +0700 Subject: [PATCH] Experiment with PGP signed messages --- source/net/filebot/License.java | 34 +++++++++++++------ .../{LicenseType.java => LicenseModel.java} | 12 +++---- source/net/filebot/Resource.java | 8 +++-- 3 files changed, 33 insertions(+), 21 deletions(-) rename source/net/filebot/{LicenseType.java => LicenseModel.java} (79%) diff --git a/source/net/filebot/License.java b/source/net/filebot/License.java index 109755b9..b5617742 100644 --- a/source/net/filebot/License.java +++ b/source/net/filebot/License.java @@ -34,21 +34,23 @@ public class License implements Serializable { private long id; private long expires; - public License(byte[] bytes) throws Exception { + private Exception error; + + public License(byte[] bytes) { this.bytes = bytes; - // verify and get clear signed content - Map properties = getProperties(); + try { + // verify and get clear signed content + Map properties = getProperties(); - this.id = Long.parseLong(properties.get("Order")); - this.expires = LocalDate.parse(properties.get("Valid-Until"), DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay(ZoneOffset.UTC).plusDays(1).minusSeconds(1).toInstant().toEpochMilli(); + this.id = Long.parseLong(properties.get("Order")); + this.expires = LocalDate.parse(properties.get("Valid-Until"), DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay(ZoneOffset.UTC).plusDays(1).minusSeconds(1).toInstant().toEpochMilli(); - // verify license online - verifyLicense(); - } - - public boolean expired() { - return expires > System.currentTimeMillis(); + // verify license online + verifyLicense(); + } catch (Exception e) { + error = e; + } } public Map getProperties() throws Exception { @@ -97,6 +99,16 @@ public class License implements Serializable { } } + public void check() throws Exception { + if (error != null) { + throw error; + } + + if (expires > System.currentTimeMillis()) { + throw new IllegalStateException("Expired: " + toString()); + } + } + @Override public String toString() { return String.format("%s (Valid-Until: %s)", id, Instant.ofEpochMilli(expires).atZone(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE)); diff --git a/source/net/filebot/LicenseType.java b/source/net/filebot/LicenseModel.java similarity index 79% rename from source/net/filebot/LicenseType.java rename to source/net/filebot/LicenseModel.java index 9f4602ef..0d8bcf05 100644 --- a/source/net/filebot/LicenseType.java +++ b/source/net/filebot/LicenseModel.java @@ -8,7 +8,7 @@ import java.io.File; import net.filebot.util.SystemProperty; -public enum LicenseType { +public enum LicenseModel { MicrosoftStore { @@ -33,16 +33,12 @@ public enum LicenseType { PGPSignedMessage { public final SystemProperty LICENSE_FILE = SystemProperty.of("net.filebot.license", File::new, ApplicationFolder.AppData.resolve("license.txt")); - public final Resource LICENSE = Resource.lazy(() -> new License(readFile(LICENSE_FILE.get()))); + public final MemoizedResource LICENSE = Resource.lazy(() -> new License(readFile(LICENSE_FILE.get()))); @Override public void check() throws LicenseError { try { - License license = LICENSE.get(); - - if (license.expired()) { - throw new LicenseError("Expired: " + license); - } + LICENSE.get().check(); } catch (Exception e) { throw new LicenseError(e.getMessage()); } @@ -51,7 +47,7 @@ public enum LicenseType { public abstract void check() throws LicenseError; - public static LicenseType get() { + public static LicenseModel get() { if (isUWP()) return MicrosoftStore; if (isMacSandbox()) diff --git a/source/net/filebot/Resource.java b/source/net/filebot/Resource.java index 49690a8e..2d4e0994 100644 --- a/source/net/filebot/Resource.java +++ b/source/net/filebot/Resource.java @@ -7,7 +7,7 @@ public interface Resource { R get() throws Exception; - default Resource memoize() { + default MemoizedResource memoize() { return new MemoizedResource(this); } @@ -15,7 +15,7 @@ public interface Resource { return new TransformedResource(this, function); } - static Resource lazy(Resource resource) { + static MemoizedResource lazy(Resource resource) { return resource.memoize(); } @@ -37,6 +37,10 @@ class MemoizedResource implements Resource { } return value; } + + public synchronized void clear() { + value = null; + } } class TransformedResource implements Resource {