Clean this up
This commit is contained in:
@ -1,11 +1,9 @@
|
|||||||
package me.braydon.license.controller;
|
package me.braydon.license.controller;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import me.braydon.license.LicenseServer;
|
|
||||||
import me.braydon.license.common.IPUtils;
|
import me.braydon.license.common.IPUtils;
|
||||||
|
import me.braydon.license.dto.LicenseCheckBodyDTO;
|
||||||
import me.braydon.license.dto.LicenseDTO;
|
import me.braydon.license.dto.LicenseDTO;
|
||||||
import me.braydon.license.exception.APIException;
|
import me.braydon.license.exception.APIException;
|
||||||
import me.braydon.license.model.License;
|
import me.braydon.license.model.License;
|
||||||
@ -40,30 +38,23 @@ public final class LicenseController {
|
|||||||
* @param body the body of the request
|
* @param body the body of the request
|
||||||
* @return the response entity
|
* @return the response entity
|
||||||
* @see License for license
|
* @see License for license
|
||||||
|
* @see LicenseCheckBodyDTO for body
|
||||||
* @see ResponseEntity for response entity
|
* @see ResponseEntity for response entity
|
||||||
*/
|
*/
|
||||||
@PostMapping("/check")
|
@PostMapping("/check")
|
||||||
@ResponseBody
|
@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
|
try { // Attempt to check the license
|
||||||
String ip = IPUtils.getRealIp(request); // The IP of the requester
|
if (!body.isValid()) {
|
||||||
|
|
||||||
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()) {
|
|
||||||
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid request body");
|
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid request body");
|
||||||
}
|
}
|
||||||
// Ensure the IP is valid
|
// Ensure the IP is valid
|
||||||
|
String ip = IPUtils.getRealIp(request); // The IP of the requester
|
||||||
if (IPUtils.getIpType(ip) == -1) {
|
if (IPUtils.getIpType(ip) == -1) {
|
||||||
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid IP address");
|
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid IP address");
|
||||||
}
|
}
|
||||||
// Ensure the HWID is valid
|
// Ensure the HWID is valid
|
||||||
// TODO: improve :)
|
String hwidString = body.getHwid();
|
||||||
String hwidString = hwid.getAsString();
|
|
||||||
boolean invalidHwid = true;
|
boolean invalidHwid = true;
|
||||||
if (hwidString.contains("-")) {
|
if (hwidString.contains("-")) {
|
||||||
int segments = hwidString.substring(0, hwidString.lastIndexOf("-")).split("-").length;
|
int segments = hwidString.substring(0, hwidString.lastIndexOf("-")).split("-").length;
|
||||||
@ -74,11 +65,10 @@ public final class LicenseController {
|
|||||||
if (invalidHwid) {
|
if (invalidHwid) {
|
||||||
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid HWID");
|
throw new APIException(HttpStatus.BAD_REQUEST, "Invalid HWID");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the license
|
// Check the license
|
||||||
License license = service.check(
|
License license = service.check(
|
||||||
key.getAsString(),
|
body.getKey(),
|
||||||
product.getAsString(),
|
body.getProduct(),
|
||||||
ip,
|
ip,
|
||||||
hwidString
|
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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user