From: Jacob Casper Date: Mon, 10 Jun 2024 02:37:32 +0000 (-0500) Subject: Add a rudimentary API auth client X-Git-Url: https://git.jacobcasper.com/?a=commitdiff_plain;ds=sidebyside;p=xiv-graph.git Add a rudimentary API auth client --- 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 index 0000000..a98a2d1 --- /dev/null +++ b/fflogsclient/src/main/java/com/jacobcasper/fflogs/ApiAuthClient.java @@ -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 index 0000000..5692077 --- /dev/null +++ b/fflogsclient/src/main/java/com/jacobcasper/fflogs/BearerToken.java @@ -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 index 0000000..311adee --- /dev/null +++ b/fflogsclient/src/main/java/com/jacobcasper/fflogs/ClientCredentials.java @@ -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 index 0000000..305f247 --- /dev/null +++ b/fflogsclient/src/main/java/com/jacobcasper/fflogs/Main.java @@ -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 --- a/pom.xml +++ b/pom.xml @@ -77,6 +77,11 @@ graphql-java-common-runtime ${graphqlMavenPluginVersion} + + com.fasterxml.jackson.core + jackson-databind + 2.17.1 +