8000 Add scope parameter to TokenManager. by mteubner · Pull Request #10761 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,6 +47,7 @@ public Config(String serverUrl, String realm, String username, String password,
this.clientSecret = clientSecret;
this.grantType = grantType;
checkGrantType(grantType);
this.scope = scope;
}

public String getServerUrl() {
Expand Down Expand Up @@ -100,6 +102,14 @@ public boolean isPublicClient() {
return clientSecret == null;
}

public String getScope() {
return scope;
}

public void setScope(String scope) {
this.scope = scope;
}

public String getGrantType() {
return grantType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,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;
Expand All @@ -74,8 +74,12 @@ private static Client newRestEasyClient(ResteasyJackson2Provider customJacksonPr
return clientBuilder.build();
}

public static Keycloak getInstance(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, SSLContext sslContext, ResteasyJackson2Provider 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, ResteasyJackson2Provider 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class KeycloakBuilder {
private String grantType;
private ResteasyClient resteasyClient;
private String authorization;
private String scope;

public KeycloakBuilder serverUrl(String serverUrl) {
this.serverUrl = serverUrl;
Expand Down Expand Up @@ -95,6 +96,11 @@ public KeycloakBuilder clientId(String clientId) {
return this;
}

public KeycloakBuilder scope(String scope) {
this.scope = scope;
return this;
}

public KeycloakBuilder clientSecret(String clientSecret) {
this.clientSecret = clientSecret;
return this;
Expand Down Expand Up @@ -144,7 +150,7 @@ public Keycloak build() {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
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
Expand Down Expand Up @@ -77,8 +79,12 @@ public synchronized AccessTokenResponse getAccessToken() {
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()) {
Expand Down
0