From 1f34d041b78a35b6758de7aa2e41ad2fe247ede8 Mon Sep 17 00:00:00 2001 From: SebClem Date: Tue, 17 May 2022 22:43:58 +0200 Subject: [PATCH] Build auth provider --- .../DiscordAuthenticationProvider.java | 18 ++++++++ .../Api/Services/DiscordOauthService.java | 46 +++++++++++++++++++ src/main/resources/application.yml | 5 ++ 3 files changed, 69 insertions(+) create mode 100644 src/main/java/net/Broken/Api/Security/DiscordAuthenticationProvider.java create mode 100644 src/main/java/net/Broken/Api/Services/DiscordOauthService.java diff --git a/src/main/java/net/Broken/Api/Security/DiscordAuthenticationProvider.java b/src/main/java/net/Broken/Api/Security/DiscordAuthenticationProvider.java new file mode 100644 index 0000000..1a998f9 --- /dev/null +++ b/src/main/java/net/Broken/Api/Security/DiscordAuthenticationProvider.java @@ -0,0 +1,18 @@ +package net.Broken.Api.Security; + +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; + +public class DiscordAuthenticationProvider implements AuthenticationProvider { + @Override + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + authentication.getCredentials() + return null; + } + + @Override + public boolean supports(Class authentication) { + return false; + } +} diff --git a/src/main/java/net/Broken/Api/Services/DiscordOauthService.java b/src/main/java/net/Broken/Api/Services/DiscordOauthService.java new file mode 100644 index 0000000..39e9466 --- /dev/null +++ b/src/main/java/net/Broken/Api/Services/DiscordOauthService.java @@ -0,0 +1,46 @@ +package net.Broken.Api.Services; + +import com.google.gson.Gson; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.HashMap; + +@Service +public class DiscordOauthService { + + @Value("${discord.client-id}") + private String clientId; + + @Value("${discord.client-secret}") + private String clientSecret; + + @Value("${discord.token-endpoint}") + private String tokenEndpoint; + + public String getAccessToken(String code, String redirectUrl){ + HashMap data = new HashMap<>(); + data.put("client_id", this.clientId); + data.put("client_secret", this.clientSecret); + data.put("grant_type", "authorization_code"); + data.put("code", code); + data.put("redirect_uri", redirectUrl); + + Gson gson = new Gson(); + HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofString(gson.toJson(data)); + + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(tokenEndpoint)) + .header("Content-Type", "application/json") + .POST(body) + .build(); + HttpClient client = HttpClient.newHttpClient(); + client.send(request, HttpResponse.BodyHandlers.ofString()); + + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9418eac..4f17324 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -34,3 +34,8 @@ server: port: ${PORT} http2: enabled: 'true' + +discord: + client-id: ${CLIENT_ID} + client-secret: ${CLIENT_SECRET} + token-endpoint: https://discord.com/api/oauth2/token \ No newline at end of file