Added a DTO for licenses

This commit is contained in:
Braydon 2023-05-31 19:19:41 -04:00
parent dcb5e222e7
commit feaf965859
3 changed files with 39 additions and 4 deletions

@ -5,6 +5,7 @@ import com.google.gson.JsonObject;
import jakarta.servlet.http.HttpServletRequest;
import lombok.NonNull;
import me.braydon.license.LicenseServer;
import me.braydon.license.dto.LicenseDTO;
import me.braydon.license.exception.APIException;
import me.braydon.license.model.License;
import me.braydon.license.service.LicenseService;
@ -56,13 +57,17 @@ public final class LicenseController {
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid request body");
}
// Check the license
service.check(
License license = service.check(
key.getAsString(),
product.getAsString(),
ip,
hwid.getAsString()
);
return ResponseEntity.ok().build(); // Return OK
// Return OK with the license DTO
return ResponseEntity.ok(new LicenseDTO(
license.getDescription(),
license.getDuration()
));
} catch (APIException ex) { // Handle the exception
return ResponseEntity.status(ex.getStatus())
.body(Map.of("error", ex.getLocalizedMessage()));

@ -0,0 +1,27 @@
package me.braydon.license.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import me.braydon.license.model.License;
/**
* A data transfer object for a {@link License}.
*
* @author Braydon
*/
@AllArgsConstructor @Getter @ToString
public class LicenseDTO {
/**
* The optional description of this license.
*/
private String description;
/**
* The duration that this licensee is valid for.
* <p>
* If -1, the license will be permanent.
* </p>
*/
private long duration;
}

@ -95,10 +95,12 @@ public final class LicenseService {
* @param product the product of the license
* @param ip the ip using the license
* @param hwid the hwid using the license
* @return the checked license
* @throws APIException if there was an error checking the license
* @see License for license
*/
public void check(@NonNull String key, @NonNull String product,
@NonNull
public License check(@NonNull String key, @NonNull String product,
@NonNull String ip, @NonNull String hwid) throws APIException {
Optional<License> optionalLicense = repository.getLicense(BCrypt.hashpw(key, licensesSalt), product); // Get the license
if (optionalLicense.isEmpty()) { // License key not found
@ -112,5 +114,6 @@ public final class LicenseService {
license.use(ip, ipsSalt, hwid); // Use the license
repository.save(license); // Save the used license
log.info("License key {} for product {} was used by {} ({})", key, product, ip, hwid);
return license;
}
}