|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.math.BigInteger; |
5 | 5 | import java.net.URI; |
| 6 | +import java.net.URLEncoder; |
6 | 7 | import java.net.http.HttpClient; |
7 | 8 | import java.net.http.HttpRequest; |
8 | 9 | import java.net.http.HttpResponse; |
| 10 | +import java.nio.charset.StandardCharsets; |
9 | 11 | import java.security.KeyFactory; |
10 | 12 | import java.security.PublicKey; |
11 | 13 | import java.security.spec.RSAPublicKeySpec; |
|
27 | 29 | @RequiredArgsConstructor |
28 | 30 | public class OauthTokenManager { |
29 | 31 |
|
| 32 | + public String accessToken = null; |
| 33 | + public String refreshToken = null; |
| 34 | + |
30 | 35 | private final String host; |
31 | 36 | private final String realm; |
32 | 37 |
|
@@ -133,4 +138,47 @@ private TokenVerifier<AccessToken> persistUserInfoInContext(String authorization |
133 | 138 | return null; |
134 | 139 | } |
135 | 140 | } |
| 141 | + |
| 142 | + public void getTokensFromCredentials(String clientId, String clientSecret, String username, String password) { |
| 143 | + try { |
| 144 | + String tokenEndpoint = host; |
| 145 | + if (!tokenEndpoint.endsWith("/")) |
| 146 | + tokenEndpoint += "/"; |
| 147 | + tokenEndpoint += "realms/" + realm + "/protocol/openid-connect/token"; |
| 148 | + |
| 149 | + String form = "grant_type=password" + "&client_id=" + URLEncoder.encode(clientId, StandardCharsets.UTF_8) |
| 150 | + + "&username=" + URLEncoder.encode(username, StandardCharsets.UTF_8) + "&password=" |
| 151 | + + URLEncoder.encode(password, StandardCharsets.UTF_8) + "&client_secret=" |
| 152 | + + URLEncoder.encode(clientSecret, StandardCharsets.UTF_8); |
| 153 | + |
| 154 | + HttpRequest request = HttpRequest.newBuilder() |
| 155 | + .uri(URI.create(tokenEndpoint)) |
| 156 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 157 | + .POST(HttpRequest.BodyPublishers.ofString(form)) |
| 158 | + .build(); |
| 159 | + |
| 160 | + HttpClient client = HttpClient.newHttpClient(); |
| 161 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 162 | + |
| 163 | + if (response.statusCode() >= 300) { |
| 164 | + throw new IOException("Token request failed: HTTP " + response.statusCode() + " - " + response.body()); |
| 165 | + } |
| 166 | + |
| 167 | + ObjectMapper mapper = new ObjectMapper(); |
| 168 | + JsonNode json = mapper.readTree(response.body()); |
| 169 | + accessToken = json.get("access_token").asText(); |
| 170 | + refreshToken = json.get("refresh_token").asText(); |
| 171 | + |
| 172 | + log.info("Token received successfully."); |
| 173 | + log.debug("Access token: {}", json.get("access_token").asText()); |
| 174 | + log.debug("Refresh token: {}", json.get("refresh_token").asText()); |
| 175 | + |
| 176 | + return json; |
| 177 | + |
| 178 | + } catch (Exception e) { |
| 179 | + log.error("Error obtaining tokens from Keycloak.", e); |
| 180 | + throw new IllegalStateException("Unable to get token", e); |
| 181 | + } |
| 182 | + } |
| 183 | + |
136 | 184 | } |
0 commit comments