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 expires;
public License(byte[] bytes) throws Exception {
private Exception error;
public License(byte[] bytes) {
this.bytes = bytes;
// verify and get clear signed content
Map<String, String> properties = getProperties();
try {
// verify and get clear signed content
Map<String, String> 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<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
public String toString() {
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;
public enum LicenseType {
public enum LicenseModel {
MicrosoftStore {
@ -33,16 +33,12 @@ public enum LicenseType {
PGPSignedMessage {
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
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())

View File

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