Add scope parameter to admin-client TokenManager.

Closes #10759
This commit is contained in:
Teubner, Malte 2022-03-29 16:39:45 +02:00 committed by Pedro Igor
parent 53aab7fc28
commit 1b36251a23
4 changed files with 34 additions and 8 deletions

View File

@ -32,12 +32,13 @@ public class Config {
private String clientId;
private String clientSecret;
private String grantType;
private String scope;
public Config(String serverUrl, String realm, String username, String password, String clientId, String clientSecret) {
this(serverUrl, realm, username, password, clientId, clientSecret, PASSWORD);
this(serverUrl, realm, username, password, clientId, clientSecret, PASSWORD, null);
}
public Config(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, String grantType) {
public Config(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, String grantType, String scope) {
this.serverUrl = serverUrl;
this.realm = realm;
this.username = username;
@ -46,6 +47,7 @@ public class Config {
this.clientSecret = clientSecret;
this.grantType = grantType;
checkGrantType(grantType);
this.scope = scope;
}
public String getServerUrl() {
@ -100,6 +102,14 @@ public class Config {
return clientSecret == null;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getGrantType() {
return grantType;
}

View File

@ -84,8 +84,8 @@ public class Keycloak implements AutoCloseable {
private final Client client;
private boolean closed = false;
Keycloak(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, String grantType, Client resteasyClient, String authtoken) {
config = new Config(serverUrl, realm, username, password, clientId, clientSecret, grantType);
Keycloak(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, String grantType, Client resteasyClient, String authtoken, String scope) {
config = new Config(serverUrl, realm, username, password, clientId, clientSecret, grantType, scope);
client = resteasyClient != null ? resteasyClient : newRestEasyClient(null, null, false);
authToken = authtoken;
tokenManager = authtoken == null ? new TokenManager(config, client) : null;
@ -101,9 +101,13 @@ public class Keycloak implements AutoCloseable {
private BearerAuthFilter newAuthFilter() {
return authToken != null ? new BearerAuthFilter(authToken) : new BearerAuthFilter(tokenManager);
}
public static Keycloak getInstance(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, SSLContext sslContext, Object customJacksonProvider, boolean disableTrustManager, String authToken, String scope) {
return new Keycloak(serverUrl, realm, username, password, clientId, clientSecret, PASSWORD, newRestEasyClient(customJacksonProvider, sslContext, disableTrustManager), authToken, scope);
}
public static Keycloak getInstance(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, SSLContext sslContext, Object customJacksonProvider, boolean disableTrustManager, String authToken) {
return new Keycloak(serverUrl, realm, username, password, clientId, clientSecret, PASSWORD, newRestEasyClient(customJacksonProvider, sslContext, disableTrustManager), authToken);
return new Keycloak(serverUrl, realm, username, password, clientId, clientSecret, PASSWORD, newRestEasyClient(customJacksonProvider, sslContext, disableTrustManager), authToken, null);
}
public static Keycloak getInstance(String serverUrl, String realm, String username, String password, String clientId, String clientSecret) {

View File

@ -62,6 +62,7 @@ public class KeycloakBuilder {
private String grantType;
private Client resteasyClient;
private String authorization;
private String scope;
public KeycloakBuilder serverUrl(String serverUrl) {
this.serverUrl = serverUrl;
@ -94,6 +95,11 @@ public class KeycloakBuilder {
return this;
}
public KeycloakBuilder scope(String scope) {
this.scope = scope;
return this;
}
public KeycloakBuilder clientSecret(String clientSecret) {
this.clientSecret = clientSecret;
return this;
@ -143,7 +149,7 @@ public class KeycloakBuilder {
throw new IllegalStateException("clientId required");
}
return new Keycloak(serverUrl, realm, username, password, clientId, clientSecret, grantType, resteasyClient, authorization);
return new Keycloak(serverUrl, realm, username, password, clientId, clientSecret, grantType, resteasyClient, authorization, scope);
}
private KeycloakBuilder() {

View File

@ -35,6 +35,8 @@ import static org.keycloak.OAuth2Constants.CLIENT_ID;
import static org.keycloak.OAuth2Constants.GRANT_TYPE;
import static org.keycloak.OAuth2Constants.PASSWORD;
import static org.keycloak.OAuth2Constants.REFRESH_TOKEN;
import static org.keycloak.OAuth2Constants.SCOPE;
import static org.keycloak.OAuth2Constants.USERNAME;
/**
* @author rodrigo.sasaki@icarros.com.br
@ -79,8 +81,12 @@ public class TokenManager {
public AccessTokenResponse grantToken() {
Form form = new Form().param(GRANT_TYPE, accessTokenGrantType);
if (PASSWORD.equals(accessTokenGrantType)) {
form.param("username", config.getUsername())
.param("password", config.getPassword());
form.param(USERNAME, config.getUsername())
.param(PASSWORD, config.getPassword());
}
if (config.getScope() != null) {
form.param(SCOPE, config.getScope());
}
if (config.isPublicClient()) {