Change to a better expiration system

This commit is contained in:
Braydon 2023-06-02 00:25:50 -04:00
parent 18ce6548f6
commit c28694c878
6 changed files with 33 additions and 33 deletions

@ -16,6 +16,9 @@ import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@ -85,14 +88,21 @@ public final class LicenseExample {
JsonElement description = json.get("description");
JsonElement ownerSnowflake = json.get("ownerSnowflake");
JsonElement ownerName = json.get("ownerName");
JsonElement duration = json.get("duration");
// Parsing the expiration date if we have one
JsonElement expires = json.get("expires");
Date expiresDate = null;
if (!expires.isJsonNull()) {
OffsetDateTime offsetDateTime = OffsetDateTime.parse(expires.getAsString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
expiresDate = Date.from(offsetDateTime.toInstant());
}
// Return the license response
return new LicenseResponse(200, null,
description.isJsonNull() ? null : description.getAsString(),
ownerSnowflake.isJsonNull() ? -1 : ownerSnowflake.getAsLong(),
ownerName.isJsonNull() ? null : ownerName.getAsString(),
duration.isJsonNull() ? -1 : duration.getAsLong()
expires.isJsonNull() ? null : expiresDate
);
} else {
ResponseBody errorBody = response.body(); // Get the error body
@ -177,12 +187,9 @@ public final class LicenseExample {
private String ownerName;
/**
* The duration of the license, present if valid.
* <p>
* If -1, the license will be permanent.
* </p>
* The optional expiration {@link Date} of the license.
*/
private long duration;
private Date expires;
public LicenseResponse(long status, @NonNull String error) {
this.status = status;
@ -204,7 +211,7 @@ public final class LicenseExample {
* @return true if permanent, otherwise false
*/
public boolean isPermanent() {
return duration == -1;
return expires == null;
}
}
}

@ -1,13 +1,11 @@
package me.braydon.example;
import java.util.concurrent.TimeUnit;
/**
* @author Braydon
*/
public final class Main {
public static void main(String[] args) {
LicenseExample.LicenseResponse response = LicenseExample.check("XXXX-XXXX-XXXX-XXXX", "Example");
LicenseExample.LicenseResponse response = LicenseExample.check("C45E-40F6-924C-753B", "CloudSpigot");
if (!response.isValid()) { // License isn't valid
System.err.println("Invalid license: " + response.getError());
return;
@ -22,9 +20,8 @@ public final class Main {
}
if (response.isPermanent()) { // License is permanent
System.out.println("Your license is permanent");
} else { // License has a duration
long durationSeconds = TimeUnit.SECONDS.toMillis(response.getDuration()); // The duration in seconds
System.out.println("Your license will expire in " + durationSeconds + " seconds");
} else { // License has an expiration date
System.out.printf("Your license will expire at: %s%n", response.getExpires().toInstant());
}
}
}

@ -74,7 +74,7 @@ public final class LicenseController {
license.getDescription(),
license.getOwnerSnowflake(),
license.getOwnerName(),
license.getDuration()
license.getExpires()
));
} catch (APIException ex) { // Handle the exception
return ResponseEntity.status(ex.getStatus())

@ -5,6 +5,8 @@ import lombok.Getter;
import lombok.ToString;
import me.braydon.license.model.License;
import java.util.Date;
/**
* A data transfer object for a {@link License}.
*
@ -34,10 +36,7 @@ public class LicenseDTO {
private String ownerName;
/**
* The duration that this licensee is valid for.
* <p>
* If -1, the license will be permanent.
* </p>
* The optional expiration {@link Date} of this license.
*/
private long duration;
private Date expires;
}

@ -80,12 +80,9 @@ public class License {
private int hwidLimit;
/**
* The duration that this licensee is valid for.
* <p>
* If -1, the license will be permanent.
* </p>
* The optional expiration {@link Date} of this license.
*/
private long duration;
private Date expires;
/**
* The {@link Date} this license was last used.
@ -113,7 +110,7 @@ public class License {
return false;
}
// Check if the license has expired
return System.currentTimeMillis() - created.getTime() >= duration;
return expires.before(new Date());
}
/**
@ -122,7 +119,7 @@ public class License {
* @return true if permanent, otherwise false
*/
public boolean isPermanent() {
return duration == -1L;
return expires == null;
}
/**

@ -63,12 +63,12 @@ public final class LicenseService {
* @param ownerName the optional owner name of the license
* @param ipLimit the IP limit of the license
* @param hwidLimit the HWID limit of the license
* @param duration the duration of the license, -1 for permanent
* @param expires the optional expiration date of the license
* @return the created license
* @see License for license
*/
public License create(@NonNull String key, @NonNull String product, String description, long ownerSnowflake,
String ownerName, int ipLimit, int hwidLimit, long duration) {
String ownerName, int ipLimit, int hwidLimit, Date expires) {
// Create the new license
License license = new License();
license.setKey(BCrypt.hashpw(key, licensesSalt)); // Hash the key
@ -80,7 +80,7 @@ public final class LicenseService {
license.setHwids(new HashSet<>());
license.setIpLimit(ipLimit); // Use the given IP limit
license.setHwidLimit(hwidLimit); // Use the given HWID limit
license.setDuration(duration);
license.setExpires(expires);
license.setCreated(new Date());
repository.insert(license); // Insert the newly created license
return license;
@ -107,7 +107,7 @@ public final class LicenseService {
}
License license = optionalLicense.get(); // The license found
String hashedIp = BCrypt.hashpw(ip, ipsSalt); // Hash the IP
String obfuscateKey = MiscUtils.obfuscateKey(key);
String obfuscateKey = MiscUtils.obfuscateKey(key); // Obfuscate the key
boolean newIp = !license.getIps().contains(hashedIp); // Is the IP new?
boolean newHwid = !license.getHwids().contains(hwid); // Is the HWID new?
@ -126,7 +126,7 @@ public final class LicenseService {
}
tags.append("HWID");
}
long expirationDate = (license.getCreated().getTime() + license.getDuration()) / 1000L;
long expires = license.isPermanent() ? -1L : license.getExpires().getTime() / 1000L;
int ipCount = license.getIps().size();
int hwidCount = license.getHwids().size();
discordService.sendLog(new EmbedBuilder()
@ -144,7 +144,7 @@ public final class LicenseService {
true
)
.addField("Expires",
license.isPermanent() ? "Never" : "<t:" + expirationDate + ":R>",
expires == -1L ? "Never" : "<t:" + expires + ":R>",
true
)
.addField("IP", ip, true)