2026-06-01 18:05:05 +02:00
|
|
|
package de.winniepat.licenselib;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
|
|
2026-06-01 18:35:35 +02:00
|
|
|
import java.net.URI;
|
2026-06-01 18:05:05 +02:00
|
|
|
import java.net.http.HttpClient;
|
2026-06-01 18:35:35 +02:00
|
|
|
import java.net.http.HttpRequest;
|
|
|
|
|
import java.net.http.HttpResponse;
|
|
|
|
|
import java.time.Duration;
|
2026-06-01 18:05:05 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A client for checking plugin licenses against a license server.
|
|
|
|
|
*/
|
|
|
|
|
public class LicenseClient {
|
|
|
|
|
|
|
|
|
|
private static final Gson GSON = new Gson();
|
2026-06-01 18:35:35 +02:00
|
|
|
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
|
|
|
|
|
.connectTimeout(Duration.ofSeconds(10))
|
|
|
|
|
.build();
|
2026-06-01 18:05:05 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Private constructor to prevent instantiation of this utility class.
|
|
|
|
|
*/
|
|
|
|
|
private LicenseClient() {
|
|
|
|
|
throw new UnsupportedOperationException("Utility class");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks the validity of a license key for a given plugin and server ID against the license server API.
|
|
|
|
|
* @param apiUrl Server API url
|
|
|
|
|
* @param plugin Plugin id matching the one on the backend api
|
|
|
|
|
* @param licenseKey License key issued by the api
|
|
|
|
|
* @param serverId Server id matching the one on the backend api
|
|
|
|
|
* @return LicenseResult with the data from the backend
|
|
|
|
|
*/
|
|
|
|
|
public static LicenseResult check(
|
|
|
|
|
String apiUrl,
|
|
|
|
|
String plugin,
|
|
|
|
|
String licenseKey,
|
2026-06-01 18:15:14 +02:00
|
|
|
String serverId // can be null
|
2026-06-01 18:05:05 +02:00
|
|
|
) {
|
2026-06-01 18:35:35 +02:00
|
|
|
try {
|
|
|
|
|
JsonObject payload = new JsonObject();
|
|
|
|
|
payload.addProperty("plugin", plugin);
|
|
|
|
|
payload.addProperty("licenseKey", licenseKey);
|
2026-06-01 18:05:05 +02:00
|
|
|
|
2026-06-01 18:35:35 +02:00
|
|
|
if (serverId != null) {
|
|
|
|
|
payload.addProperty("serverId", serverId);
|
|
|
|
|
}
|
2026-06-01 18:05:05 +02:00
|
|
|
|
2026-06-01 18:35:35 +02:00
|
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
|
.uri(URI.create(apiUrl))
|
|
|
|
|
.timeout(Duration.ofSeconds(10))
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(payload)))
|
|
|
|
|
.build();
|
2026-06-01 18:05:05 +02:00
|
|
|
|
2026-06-01 18:35:35 +02:00
|
|
|
HttpResponse<String> response = HTTP_CLIENT.send(
|
|
|
|
|
request,
|
|
|
|
|
HttpResponse.BodyHandlers.ofString()
|
|
|
|
|
);
|
2026-06-01 18:06:16 +02:00
|
|
|
|
2026-06-01 18:35:35 +02:00
|
|
|
JsonObject json = GSON.fromJson(response.body(), JsonObject.class);
|
2026-06-01 18:06:16 +02:00
|
|
|
|
2026-06-01 18:35:35 +02:00
|
|
|
return new LicenseResult(
|
|
|
|
|
json.get("valid").getAsBoolean(),
|
|
|
|
|
json.get("status").getAsString(),
|
|
|
|
|
json.get("message").getAsString()
|
|
|
|
|
);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return new LicenseResult(
|
|
|
|
|
false,
|
|
|
|
|
"error",
|
|
|
|
|
e.getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-06-01 18:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-01 18:05:05 +02:00
|
|
|
/**
|
|
|
|
|
* Represents the result of a license check.
|
|
|
|
|
* @param isValid boolean if the license is valid or not
|
|
|
|
|
* @param status license status
|
|
|
|
|
* @param message additional message from the backend
|
|
|
|
|
*/
|
|
|
|
|
public record LicenseResult(
|
|
|
|
|
boolean isValid,
|
|
|
|
|
String status,
|
|
|
|
|
String message
|
|
|
|
|
) { }
|
|
|
|
|
}
|