Add a rudimentary API auth client master
authorJacob Casper <dev@jacobcasper.com>
Mon, 10 Jun 2024 02:37:32 +0000 (21:37 -0500)
committerJacob Casper <dev@jacobcasper.com>
Tue, 11 Jun 2024 13:33:42 +0000 (08:33 -0500)
fflogsclient/src/main/java/com/jacobcasper/fflogs/ApiAuthClient.java [new file with mode: 0644]
fflogsclient/src/main/java/com/jacobcasper/fflogs/BearerToken.java [new file with mode: 0644]
fflogsclient/src/main/java/com/jacobcasper/fflogs/ClientCredentials.java [new file with mode: 0644]
fflogsclient/src/main/java/com/jacobcasper/fflogs/Main.java [new file with mode: 0644]
pom.xml

diff --git a/fflogsclient/src/main/java/com/jacobcasper/fflogs/ApiAuthClient.java b/fflogsclient/src/main/java/com/jacobcasper/fflogs/ApiAuthClient.java
new file mode 100644 (file)
index 0000000..a98a2d1
--- /dev/null
@@ -0,0 +1,39 @@
+package com.jacobcasper.fflogs;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.util.Base64;
+
+public class ApiAuthClient {
+    private static final String TOKEN_URL = "https://www.fflogs.com/oauth/token";
+
+    private final String clientId;
+    private final String clientSecret;
+    private final HttpClient client;
+
+    public ApiAuthClient(final String clientId, final String clientSecret) {
+        this.clientId = clientId;
+        this.clientSecret = clientSecret;
+        this.client = HttpClient.newHttpClient();
+    }
+
+    public String getToken() throws URISyntaxException, IOException, InterruptedException {
+        final var req = HttpRequest.newBuilder()
+                .POST(HttpRequest.BodyPublishers.ofString("grant_type=client_credentials"))
+                .header("Content-Type", "application/x-www-form-urlencoded")
+                .header("Authorization", getBasicAuthenticationHeader(clientId, clientSecret))
+                .uri(new URI(TOKEN_URL))
+                .build();
+
+        return client.send(req, HttpResponse.BodyHandlers.ofString()).body();
+    }
+
+    private static String getBasicAuthenticationHeader(String username, String password) {
+        String valueToEncode = username + ":" + password;
+        return "Basic " + Base64.getEncoder().encodeToString(valueToEncode.getBytes());
+    }
+}
diff --git a/fflogsclient/src/main/java/com/jacobcasper/fflogs/BearerToken.java b/fflogsclient/src/main/java/com/jacobcasper/fflogs/BearerToken.java
new file mode 100644 (file)
index 0000000..5692077
--- /dev/null
@@ -0,0 +1,4 @@
+package com.jacobcasper.fflogs;
+
+public record BearerToken(String tokenType, Long expiresIn, String accessToken) {
+}
diff --git a/fflogsclient/src/main/java/com/jacobcasper/fflogs/ClientCredentials.java b/fflogsclient/src/main/java/com/jacobcasper/fflogs/ClientCredentials.java
new file mode 100644 (file)
index 0000000..311adee
--- /dev/null
@@ -0,0 +1,4 @@
+package com.jacobcasper.fflogs;
+
+public record ClientCredentials(String clientId, String clientSecret) {
+}
diff --git a/fflogsclient/src/main/java/com/jacobcasper/fflogs/Main.java b/fflogsclient/src/main/java/com/jacobcasper/fflogs/Main.java
new file mode 100644 (file)
index 0000000..305f247
--- /dev/null
@@ -0,0 +1,17 @@
+package com.jacobcasper.fflogs;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.PropertyNamingStrategies;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+
+public class Main {
+    public static void main(String... args) throws IOException, URISyntaxException, InterruptedException {
+        final var om = new ObjectMapper();
+        om.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
+        final var creds = om.readValue(Main.class.getClassLoader().getResource("client_secret.json"), ClientCredentials.class);
+        final var token = om.readValue(new ApiAuthClient(creds.clientId(), creds.clientSecret()).getToken(), BearerToken.class);
+        System.out.println(token);
+    }
+}
diff --git a/pom.xml b/pom.xml
index 96e365a..8191121 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <artifactId>graphql-java-common-runtime</artifactId>
             <version>${graphqlMavenPluginVersion}</version>
         </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>2.17.1</version>
+        </dependency>
     </dependencies>
 
     <build>