Clean this up

This commit is contained in:
Braydon 2023-12-02 00:59:46 -05:00
parent d7dc635ffb
commit 9215ac87b0
2 changed files with 47 additions and 18 deletions

@ -1,11 +1,9 @@
package me.braydon.license.controller;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import jakarta.servlet.http.HttpServletRequest;
import lombok.NonNull;
import me.braydon.license.LicenseServer;
import me.braydon.license.common.IPUtils;
import me.braydon.license.dto.LicenseCheckBodyDTO;
import me.braydon.license.dto.LicenseDTO;
import me.braydon.license.exception.APIException;
import me.braydon.license.model.License;
@ -40,30 +38,23 @@ public final class LicenseController {
* @param body the body of the request
* @return the response entity
* @see License for license
* @see LicenseCheckBodyDTO for body
* @see ResponseEntity for response entity
*/
@PostMapping("/check")
@ResponseBody
public ResponseEntity<?> check(@NonNull HttpServletRequest request, @RequestBody @NonNull String body) {
public ResponseEntity<?> check(@NonNull HttpServletRequest request, @RequestBody @NonNull LicenseCheckBodyDTO body) {
try { // Attempt to check the license
String ip = IPUtils.getRealIp(request); // The IP of the requester
JsonObject jsonObject = LicenseServer.GSON.fromJson(body, JsonObject.class);
JsonElement key = jsonObject.get("key"); // Get the key
JsonElement product = jsonObject.get("product"); // Get the product
JsonElement hwid = jsonObject.get("hwid"); // Get the hwid
// Ensure the body keys aren't null
if (key.isJsonNull() || product.isJsonNull() || hwid.isJsonNull()) {
if (!body.isValid()) {
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid request body");
}
// Ensure the IP is valid
String ip = IPUtils.getRealIp(request); // The IP of the requester
if (IPUtils.getIpType(ip) == -1) {
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid IP address");
}
// Ensure the HWID is valid
// TODO: improve :)
String hwidString = hwid.getAsString();
String hwidString = body.getHwid();
boolean invalidHwid = true;
if (hwidString.contains("-")) {
int segments = hwidString.substring(0, hwidString.lastIndexOf("-")).split("-").length;
@ -74,11 +65,10 @@ public final class LicenseController {
if (invalidHwid) {
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid HWID");
}
// Check the license
License license = service.check(
key.getAsString(),
product.getAsString(),
body.getKey(),
body.getProduct(),
ip,
hwidString
);

@ -0,0 +1,39 @@
package me.braydon.license.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import me.braydon.license.model.License;
/**
* A data transfer object that contains
* the body for checking a {@link License}.
*
* @author Braydon
*/
@AllArgsConstructor @Getter @ToString
public class LicenseCheckBodyDTO {
/**
* The license key to check.
*/
private String key;
/**
* The product of the license to check.
*/
private String product;
/**
* The hardware id of the user checking the license.
*/
private String hwid;
/**
* Are these params valid?
*
* @return whether the params are valid
*/
public boolean isValid() {
return key != null && product != null && hwid != null;
}
}