Skip to content

Commit

Permalink
Merge pull request #1 from Hopefuls/main
Browse files Browse the repository at this point in the history
Use more lightweight library for parsing/handling JSON data
  • Loading branch information
DevLeoko authored Jun 2, 2024
2 parents 81b0c12 + a172576 commit 011b68b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,7 @@ buildNumber.properties
# JDT-specific (Eclipse Java Development Tools)
.classpath

# End of https://www.toptal.com/developers/gitignore/api/java,maven,intellij
# End of https://www.toptal.com/developers/gitignore/api/java,maven,intellij

# Exclude vscode Workspace configuration
.vscode/
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>dev.respark.licensegate</groupId>
<artifactId>license-gate</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand All @@ -24,11 +24,11 @@


<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.1</version>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -62,4 +62,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
20 changes: 9 additions & 11 deletions src/main/java/dev/respark/licensegate/LicenseGate.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.respark.licensegate;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -24,7 +23,6 @@
*/
public class LicenseGate {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String DEFAULT_SERVER = "https://api.licensegate.io";

private String userId;
Expand Down Expand Up @@ -132,16 +130,16 @@ public ValidationType verify(String licenseKey, String scope) {
public ValidationType verify(String licenseKey, String scope, String metadata) {
try {
String challenge = this.useChallenges ? String.valueOf(System.currentTimeMillis()) : null;
ObjectNode response = requestServer(buildUrl(licenseKey, scope, metadata, challenge));
JSONObject response = requestServer(buildUrl(licenseKey, scope, metadata, challenge));

if (response.has("error") || !response.has("result")) {
if (debug) System.out.println("Error: " + response.get("error").asText());
if (debug) System.out.println("Error: " + response.getString("error"));
return ValidationType.SERVER_ERROR;
}

// Non-valid response don't need a signed challenge
if (response.has("valid") && !response.get("valid").asBoolean()) {
ValidationType result = ValidationType.valueOf(response.get("result").asText());
if (response.has("valid") && !response.getBoolean("valid")) {
ValidationType result = ValidationType.valueOf(response.getString("result"));
if (result == ValidationType.VALID) {
return ValidationType.SERVER_ERROR;
} else {
Expand All @@ -155,13 +153,13 @@ public ValidationType verify(String licenseKey, String scope, String metadata) {
return ValidationType.FAILED_CHALLENGE;
}

if (!verifyChallenge(challenge, response.get("signedChallenge").asText())) {
if (!verifyChallenge(challenge, response.getString("signedChallenge"))) {
if (debug) System.out.println("Error: Challenge verification failed");
return ValidationType.FAILED_CHALLENGE;
}
}

return ValidationType.valueOf(response.get("result").asText());
return ValidationType.valueOf(response.getString("result"));
} catch (IOException e) {
if (debug) e.printStackTrace();
return ValidationType.CONNECTION_ERROR;
Expand Down Expand Up @@ -220,7 +218,7 @@ private String buildUrl(String licenseKey, String scope, String metadata, String
return validationServer + "/license/" + userId + "/" + licenseKey + "/verify" + queryString;
}

private ObjectNode requestServer(String urlStr) throws IOException {
private JSONObject requestServer(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
Expand Down Expand Up @@ -248,7 +246,7 @@ private ObjectNode requestServer(String urlStr) throws IOException {
}

// Parse JSON response
return OBJECT_MAPPER.readValue(jsonStr, ObjectNode.class);
return new JSONObject(jsonStr);
}
}

Expand Down

0 comments on commit 011b68b

Please sign in to comment.