Build auth provider

This commit is contained in:
SebClem 2022-05-17 22:43:58 +02:00
parent c4ed20644c
commit 1f34d041b7
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
3 changed files with 69 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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<String, String> 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());
}
}

View File

@ -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