Build auth provider
This commit is contained in:
parent
c4ed20644c
commit
1f34d041b7
@ -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;
|
||||
}
|
||||
}
|
@ -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());
|
||||
|
||||
}
|
||||
}
|
@ -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
|
Loading…
Reference in New Issue
Block a user