Skip to content

Commit 36c7123

Browse files
committed
enhance license verification with exception handling and return status
1 parent 3174d08 commit 36c7123

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/main/java/org/lukittu/java_simple/LukittuLicenseVerify.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ public class LukittuLicenseVerify {
108108
* production)
109109
* @param publicKey The public key used to verify the server's signature
110110
* (should be hardcoded in production)
111+
* @throws IOException If a network or server error occurs
112+
* @throws Exception If validation fails for any reason
111113
*/
112-
public static void verifyKey(String licenseKey, String teamId, String productId, String publicKey) {
114+
public static void verifyKey(String licenseKey, String teamId, String productId, String publicKey)
115+
throws Exception {
113116
DEVICE_IDENTIFIER = getHardwareIdentifier();
114117

115118
// Generate a random challenge
@@ -127,7 +130,13 @@ public static void verifyKey(String licenseKey, String teamId, String productId,
127130
"deviceIdentifier": "%s"
128131
}""", licenseKey, productId, challenge, VERSION, DEVICE_IDENTIFIER);
129132

130-
fetchAndHandleResponse(url, jsonBody, publicKey, challenge);
133+
boolean verificationSuccess = fetchAndHandleResponse(url, jsonBody, publicKey, challenge);
134+
135+
// Throw exception if verification failed to ensure it's caught in Simple's
136+
// onEnable
137+
if (!verificationSuccess) {
138+
throw new Exception("License verification failed");
139+
}
131140
}
132141

133142
/**
@@ -165,10 +174,13 @@ public static String bytesToHex(byte[] bytes) {
165174
* @param publicKeyBase64 The public key to verify the response
166175
* @param challenge The challenge string that should be signed in the
167176
* response
177+
* @return true if verification succeeded, false if it failed
178+
* @throws IOException If a network error occurs
168179
*/
169-
public static void fetchAndHandleResponse(String urlString, String jsonBody, String publicKeyBase64,
170-
String challenge) {
180+
public static boolean fetchAndHandleResponse(String urlString, String jsonBody, String publicKeyBase64,
181+
String challenge) throws IOException {
171182
HttpURLConnection connection = null;
183+
boolean success = false;
172184

173185
try {
174186
var url = URI.create(urlString).toURL();
@@ -189,7 +201,7 @@ public static void fetchAndHandleResponse(String urlString, String jsonBody, Str
189201

190202
if (responseCode == HttpURLConnection.HTTP_OK) {
191203
try (var inputStream = connection.getInputStream()) {
192-
handleJsonResponse(inputStream, publicKeyBase64, challenge);
204+
success = handleJsonResponse(inputStream, publicKeyBase64, challenge);
193205
}
194206
} else {
195207
try (var errorStream = connection.getErrorStream()) {
@@ -214,11 +226,14 @@ public static void fetchAndHandleResponse(String urlString, String jsonBody, Str
214226
} catch (IOException e1) {
215227
Simple.INSTANCE.getLogger().log(Level.SEVERE, "Failed to parse error response", e1);
216228
}
229+
throw new IOException("Connection to license server failed", e);
217230
} finally {
218231
if (connection != null) {
219232
connection.disconnect();
220233
}
221234
}
235+
236+
return success;
222237
}
223238

224239
/**

src/main/java/org/lukittu/java_simple/Simple.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,14 @@ record LicenseConfig(String key, String teamId, String productId, String publicK
8484
licenseConfig.productId(),
8585
licenseConfig.publicKey());
8686

87+
// If license verification failed, disable the plugin
8788
if (!valid) {
8889
// Verification already showed the appropriate error message
8990
getServer().getPluginManager().disablePlugin(this);
9091
return;
9192
}
9293

93-
// Set up periodic heartbeat checks every 15 minutes
94+
// Only setup heartbeat and enable plugin if license is valid
9495
setupHeartbeatScheduler(
9596
licenseConfig.key(),
9697
licenseConfig.teamId(),
@@ -100,6 +101,7 @@ record LicenseConfig(String key, String teamId, String productId, String publicK
100101
} catch (Exception e) {
101102
getLogger().severe("Unexpected error during license verification: " + e.getMessage());
102103
logMessage("License verification failed due to an unexpected error");
104+
// Ensure plugin is always disabled on any exception
103105
getServer().getPluginManager().disablePlugin(this);
104106
}
105107
}
@@ -120,6 +122,7 @@ private void setupHeartbeatScheduler(String licenseKey, String teamId, String pr
120122
LukittuLicenseVerify.sendHeartbeat(teamId, licenseKey, productId);
121123
getLogger().fine("Heartbeat sent successfully");
122124
} catch (Exception e) {
125+
// Heartbeat failures should be silent, just log at warning level
123126
getLogger().log(Level.WARNING, "Failed to send heartbeat", e);
124127
}
125128
}, 15, 15, TimeUnit.MINUTES);

0 commit comments

Comments
 (0)