Added license durations
This commit is contained in:
parent
c1e1a9e462
commit
b60d965dec
@ -0,0 +1,17 @@
|
|||||||
|
package me.braydon.license.exception;
|
||||||
|
|
||||||
|
import me.braydon.license.model.License;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This exception is raised when
|
||||||
|
* a {@link License} has been used
|
||||||
|
* but is expired.
|
||||||
|
*
|
||||||
|
* @author Braydon
|
||||||
|
*/
|
||||||
|
public class LicenseExpiredException extends APIException {
|
||||||
|
public LicenseExpiredException() {
|
||||||
|
super(HttpStatus.BAD_REQUEST, "License has expired");
|
||||||
|
}
|
||||||
|
}
|
@ -64,6 +64,14 @@ public class License {
|
|||||||
*/
|
*/
|
||||||
private int hwidLimit;
|
private int hwidLimit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The duration that this licensee is valid for.
|
||||||
|
* <p>
|
||||||
|
* If -1, the license will be permanent.
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
private long duration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link Date} this license was last used.
|
* The {@link Date} this license was last used.
|
||||||
*/
|
*/
|
||||||
@ -74,6 +82,34 @@ public class License {
|
|||||||
*/
|
*/
|
||||||
@NonNull private Date created;
|
@NonNull private Date created;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this license has expired.
|
||||||
|
* <p>
|
||||||
|
* If this license has no
|
||||||
|
* expiration, this will
|
||||||
|
* always return false.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return true if expired, otherwise false
|
||||||
|
*/
|
||||||
|
public boolean hasExpired() {
|
||||||
|
// License is permanent, not expired
|
||||||
|
if (isPermanent()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Check if the license has expired
|
||||||
|
return System.currentTimeMillis() - created.getTime() >= duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this license has no expiration.
|
||||||
|
*
|
||||||
|
* @return true if permanent, otherwise false
|
||||||
|
*/
|
||||||
|
public boolean isPermanent() {
|
||||||
|
return duration == -1L;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoked when this license is used.
|
* Invoked when this license is used.
|
||||||
*
|
*
|
||||||
|
@ -4,6 +4,7 @@ import jakarta.annotation.PostConstruct;
|
|||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.braydon.license.exception.APIException;
|
import me.braydon.license.exception.APIException;
|
||||||
|
import me.braydon.license.exception.LicenseExpiredException;
|
||||||
import me.braydon.license.exception.LicenseNotFoundException;
|
import me.braydon.license.exception.LicenseNotFoundException;
|
||||||
import me.braydon.license.model.License;
|
import me.braydon.license.model.License;
|
||||||
import me.braydon.license.repository.LicenseRepository;
|
import me.braydon.license.repository.LicenseRepository;
|
||||||
@ -66,11 +67,12 @@ public final class LicenseService {
|
|||||||
* @param description the optional description of the license
|
* @param description the optional description of the license
|
||||||
* @param ipLimit the IP limit of the license
|
* @param ipLimit the IP limit of the license
|
||||||
* @param hwidLimit the HWID limit of the license
|
* @param hwidLimit the HWID limit of the license
|
||||||
|
* @param duration the duration of the license, -1 for permanent
|
||||||
* @return the created license
|
* @return the created license
|
||||||
* @see License for license
|
* @see License for license
|
||||||
*/
|
*/
|
||||||
public License create(@NonNull String key, @NonNull String product,
|
public License create(@NonNull String key, @NonNull String product, String description,
|
||||||
String description, int ipLimit, int hwidLimit) {
|
int ipLimit, int hwidLimit, long duration) {
|
||||||
// Create the new license
|
// Create the new license
|
||||||
License license = new License();
|
License license = new License();
|
||||||
license.setKey(BCrypt.hashpw(key, licensesSalt)); // Hash the key
|
license.setKey(BCrypt.hashpw(key, licensesSalt)); // Hash the key
|
||||||
@ -80,6 +82,7 @@ public final class LicenseService {
|
|||||||
license.setHwids(new HashSet<>());
|
license.setHwids(new HashSet<>());
|
||||||
license.setIpLimit(ipLimit); // Use the given IP limit
|
license.setIpLimit(ipLimit); // Use the given IP limit
|
||||||
license.setHwidLimit(hwidLimit); // Use the given HWID limit
|
license.setHwidLimit(hwidLimit); // Use the given HWID limit
|
||||||
|
license.setDuration(duration);
|
||||||
license.setCreated(new Date());
|
license.setCreated(new Date());
|
||||||
repository.insert(license); // Insert the newly created license
|
repository.insert(license); // Insert the newly created license
|
||||||
return license;
|
return license;
|
||||||
@ -103,6 +106,9 @@ public final class LicenseService {
|
|||||||
throw new LicenseNotFoundException();
|
throw new LicenseNotFoundException();
|
||||||
}
|
}
|
||||||
License license = optionalLicense.get(); // The license found
|
License license = optionalLicense.get(); // The license found
|
||||||
|
if (license.hasExpired()) { // The license has expired
|
||||||
|
throw new LicenseExpiredException();
|
||||||
|
}
|
||||||
license.use(ip, ipsSalt, hwid); // Use the license
|
license.use(ip, ipsSalt, hwid); // Use the license
|
||||||
repository.save(license); // Save the used license
|
repository.save(license); // Save the used license
|
||||||
log.info("License key {} for product {} was used by {} ({})", key, product, ip, hwid);
|
log.info("License key {} for product {} was used by {} ({})", key, product, ip, hwid);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user