Experiment with PGP signed messages

This commit is contained in:
Reinhard Pointner 2018-06-10 01:43:28 +07:00
parent 03fb5b3b94
commit be082ab863
3 changed files with 33 additions and 21 deletions

View File

@ -34,21 +34,23 @@ public class License implements Serializable {
private long id; private long id;
private long expires; private long expires;
public License(byte[] bytes) throws Exception { private Exception error;
public License(byte[] bytes) {
this.bytes = bytes; this.bytes = bytes;
// verify and get clear signed content try {
Map<String, String> properties = getProperties(); // verify and get clear signed content
Map<String, String> properties = getProperties();
this.id = Long.parseLong(properties.get("Order")); 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.expires = LocalDate.parse(properties.get("Valid-Until"), DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay(ZoneOffset.UTC).plusDays(1).minusSeconds(1).toInstant().toEpochMilli();
// verify license online // verify license online
verifyLicense(); verifyLicense();
} } catch (Exception e) {
error = e;
public boolean expired() { }
return expires > System.currentTimeMillis();
} }
public Map<String, String> getProperties() throws Exception { public Map<String, String> 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 @Override
public String toString() { public String toString() {
return String.format("%s (Valid-Until: %s)", id, Instant.ofEpochMilli(expires).atZone(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE)); return String.format("%s (Valid-Until: %s)", id, Instant.ofEpochMilli(expires).atZone(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE));

View File

@ -8,7 +8,7 @@ import java.io.File;
import net.filebot.util.SystemProperty; import net.filebot.util.SystemProperty;
public enum LicenseType { public enum LicenseModel {
MicrosoftStore { MicrosoftStore {
@ -33,16 +33,12 @@ public enum LicenseType {
PGPSignedMessage { PGPSignedMessage {
public final SystemProperty<File> LICENSE_FILE = SystemProperty.of("net.filebot.license", File::new, ApplicationFolder.AppData.resolve("license.txt")); public final SystemProperty<File> LICENSE_FILE = SystemProperty.of("net.filebot.license", File::new, ApplicationFolder.AppData.resolve("license.txt"));
public final Resource<License> LICENSE = Resource.lazy(() -> new License(readFile(LICENSE_FILE.get()))); public final MemoizedResource<License> LICENSE = Resource.lazy(() -> new License(readFile(LICENSE_FILE.get())));
@Override @Override
public void check() throws LicenseError { public void check() throws LicenseError {
try { try {
License license = LICENSE.get(); LICENSE.get().check();
if (license.expired()) {
throw new LicenseError("Expired: " + license);
}
} catch (Exception e) { } catch (Exception e) {
throw new LicenseError(e.getMessage()); throw new LicenseError(e.getMessage());
} }
@ -51,7 +47,7 @@ public enum LicenseType {
public abstract void check() throws LicenseError; public abstract void check() throws LicenseError;
public static LicenseType get() { public static LicenseModel get() {
if (isUWP()) if (isUWP())
return MicrosoftStore; return MicrosoftStore;
if (isMacSandbox()) if (isMacSandbox())

View File

@ -7,7 +7,7 @@ public interface Resource<R> {
R get() throws Exception; R get() throws Exception;
default Resource<R> memoize() { default MemoizedResource<R> memoize() {
return new MemoizedResource<R>(this); return new MemoizedResource<R>(this);
} }
@ -15,7 +15,7 @@ public interface Resource<R> {
return new TransformedResource<R, T>(this, function); return new TransformedResource<R, T>(this, function);
} }
static <T> Resource<T> lazy(Resource<T> resource) { static <T> MemoizedResource<T> lazy(Resource<T> resource) {
return resource.memoize(); return resource.memoize();
} }
@ -37,6 +37,10 @@ class MemoizedResource<R> implements Resource<R> {
} }
return value; return value;
} }
public synchronized void clear() {
value = null;
}
} }
class TransformedResource<R, T> implements Resource<T> { class TransformedResource<R, T> implements Resource<T> {