KEYCLOAK-2721: Do not recreate TokenService proxy

By re-using the service proxy, classloading issues can be prevented.
This commit is contained in:
Guus der Kinderen 2016-03-29 11:11:35 +02:00
parent dfc4cf36a2
commit 89158c9dcf

View file

@ -18,7 +18,6 @@
package org.keycloak.admin.client.token;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.keycloak.admin.client.Config;
import org.keycloak.admin.client.resource.BasicAuthFilter;
@ -27,8 +26,6 @@ import org.keycloak.representations.AccessTokenResponse;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.Form;
import java.util.Calendar;
import java.util.Date;
/**
* @author rodrigo.sasaki@icarros.com.br
@ -41,11 +38,15 @@ public class TokenManager {
private long expirationTime;
private long minTokenValidity = DEFAULT_MIN_VALIDITY;
private final Config config;
private final ResteasyClient client;
private final TokenService tokenService;
public TokenManager(Config config, ResteasyClient client){
this.config = config;
this.client = client;
ResteasyWebTarget target = client.target(config.getServerUrl());
if(!config.isPublicClient()){
target.register(new BasicAuthFilter(config.getClientId(), config.getClientSecret()));
}
tokenService = target.proxy(TokenService.class);
}
public String getAccessTokenString(){
@ -62,8 +63,6 @@ public class TokenManager {
}
public AccessTokenResponse grantToken(){
ResteasyWebTarget target = client.target(config.getServerUrl());
Form form = new Form()
.param("grant_type", "password")
.param("username", config.getUsername())
@ -71,12 +70,8 @@ public class TokenManager {
if(config.isPublicClient()){
form.param("client_id", config.getClientId());
} else {
target.register(new BasicAuthFilter(config.getClientId(), config.getClientSecret()));
}
TokenService tokenService = target.proxy(TokenService.class);
int requestTime = Time.currentTime();
currentToken = tokenService.grantToken(config.getRealm(), form.asMap());
expirationTime = requestTime + currentToken.getExpiresIn();
@ -85,20 +80,14 @@ public class TokenManager {
}
public AccessTokenResponse refreshToken(){
ResteasyWebTarget target = client.target(config.getServerUrl());
Form form = new Form()
.param("grant_type", "refresh_token")
.param("refresh_token", currentToken.getRefreshToken());
if(config.isPublicClient()){
form.param("client_id", config.getClientId());
} else {
target.register(new BasicAuthFilter(config.getClientId(), config.getClientSecret()));
}
TokenService tokenService = target.proxy(TokenService.class);
try {
int requestTime = Time.currentTime();
currentToken = tokenService.refreshToken(config.getRealm(), form.asMap());