Skip to content

Commit 565d080

Browse files
committed
add fetching of access and refresh-tokens
fix dependencies
1 parent f3014c8 commit 565d080

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

pom.xml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@
2424
<dependencies>
2525
<!--Websocket Client-->
2626
<!-- And add org.keycloak:keycloak-core to ignoredUnused below...-->
27-
<dependency>
28-
<groupId>org.eclipse.jetty.websocket</groupId>
29-
<artifactId>websocket-api</artifactId>
30-
<version>9.4.38.v20210224</version>
31-
<scope>compile</scope>
32-
</dependency>
33-
<dependency>
34-
<groupId>org.keycloak</groupId>
35-
<artifactId>keycloak-admin-client</artifactId>
36-
<version>26.0.4</version>
37-
</dependency>
3827
<dependency>
3928
<groupId>info.unterrainer.commons</groupId>
4029
<artifactId>http-server</artifactId>
@@ -56,6 +45,7 @@
5645
combine.children="append">
5746
<ignoredUsedUndeclaredDependencies>org.keycloak:keycloak-core</ignoredUsedUndeclaredDependencies>
5847
<ignoredUsedUndeclaredDependencies>org.keycloak:keycloak-common</ignoredUsedUndeclaredDependencies>
48+
<ignoredUsedUndeclaredDependencies>org.keycloak:keycloak-client-common-synced</ignoredUsedUndeclaredDependencies>
5949
</ignoredUsedUndeclaredDependencies>
6050
</configuration>
6151
</execution>

src/main/java/info/unterrainer/oauthtokenmanager/OauthTokenManager.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.io.IOException;
44
import java.math.BigInteger;
55
import java.net.URI;
6+
import java.net.URLEncoder;
67
import java.net.http.HttpClient;
78
import java.net.http.HttpRequest;
89
import java.net.http.HttpResponse;
10+
import java.nio.charset.StandardCharsets;
911
import java.security.KeyFactory;
1012
import java.security.PublicKey;
1113
import java.security.spec.RSAPublicKeySpec;
@@ -27,6 +29,9 @@
2729
@RequiredArgsConstructor
2830
public class OauthTokenManager {
2931

32+
public String accessToken = null;
33+
public String refreshToken = null;
34+
3035
private final String host;
3136
private final String realm;
3237

@@ -133,4 +138,47 @@ private TokenVerifier<AccessToken> persistUserInfoInContext(String authorization
133138
return null;
134139
}
135140
}
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+
136184
}

0 commit comments

Comments
 (0)