diff --git a/src/main/java/me/braydon/license/controller/LicenseController.java b/src/main/java/me/braydon/license/controller/LicenseController.java index ed7d6c7..225e023 100644 --- a/src/main/java/me/braydon/license/controller/LicenseController.java +++ b/src/main/java/me/braydon/license/controller/LicenseController.java @@ -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())); diff --git a/src/main/java/me/braydon/license/dto/LicenseDTO.java b/src/main/java/me/braydon/license/dto/LicenseDTO.java new file mode 100644 index 0000000..06f13bc --- /dev/null +++ b/src/main/java/me/braydon/license/dto/LicenseDTO.java @@ -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. + *

+ * If -1, the license will be permanent. + *

+ */ + private long duration; +} diff --git a/src/main/java/me/braydon/license/service/LicenseService.java b/src/main/java/me/braydon/license/service/LicenseService.java index 4f0d9dc..54596f2 100644 --- a/src/main/java/me/braydon/license/service/LicenseService.java +++ b/src/main/java/me/braydon/license/service/LicenseService.java @@ -95,11 +95,13 @@ 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 String ip, @NonNull String hwid) throws APIException { + @NonNull + public License check(@NonNull String key, @NonNull String product, + @NonNull String ip, @NonNull String hwid) throws APIException { Optional optionalLicense = repository.getLicense(BCrypt.hashpw(key, licensesSalt), product); // Get the license if (optionalLicense.isEmpty()) { // License key not found log.error("License key {} for product {} not found", key, product); // Log the error @@ -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; } }