KEYCLOAK-2434

BasicAuthRequestAuthenticator ignores HttpClientBuilder configuration
This commit is contained in:
Stian Thorgersen 2016-02-08 20:25:46 +01:00
parent 5f41215e27
commit 528e3127e6
2 changed files with 29 additions and 33 deletions

View file

@ -85,42 +85,38 @@ public class BasicAuthRequestAuthenticator extends BearerTokenRequestAuthenticat
private AccessTokenResponse getToken(String username, String password) throws Exception { private AccessTokenResponse getToken(String username, String password) throws Exception {
AccessTokenResponse tokenResponse=null; AccessTokenResponse tokenResponse=null;
HttpClient client = new HttpClientBuilder().disableTrustManager().build(); HttpClient client = deployment.getClient();
try { HttpPost post = new HttpPost(
HttpPost post = new HttpPost( KeycloakUriBuilder.fromUri(deployment.getAuthServerBaseUrl())
KeycloakUriBuilder.fromUri(deployment.getAuthServerBaseUrl()) .path(ServiceUrlConstants.TOKEN_PATH).build(deployment.getRealm()));
.path(ServiceUrlConstants.TOKEN_PATH).build(deployment.getRealm())); java.util.List <NameValuePair> formparams = new java.util.ArrayList <NameValuePair>();
java.util.List <NameValuePair> formparams = new java.util.ArrayList <NameValuePair>(); formparams.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD));
formparams.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD)); formparams.add(new BasicNameValuePair("username", username));
formparams.add(new BasicNameValuePair("username", username)); formparams.add(new BasicNameValuePair("password", password));
formparams.add(new BasicNameValuePair("password", password));
ClientCredentialsProviderUtils.setClientCredentials(deployment, post, formparams); ClientCredentialsProviderUtils.setClientCredentials(deployment, post, formparams);
UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8"); UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8");
post.setEntity(form); post.setEntity(form);
HttpResponse response = client.execute(post); HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode(); int status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
if (status != 200) { if (status != 200) {
throw new java.io.IOException("Bad status: " + status); throw new java.io.IOException("Bad status: " + status);
} }
if (entity == null) { if (entity == null) {
throw new java.io.IOException("No Entity"); throw new java.io.IOException("No Entity");
} }
java.io.InputStream is = entity.getContent(); java.io.InputStream is = entity.getContent();
try { try {
tokenResponse = JsonSerialization.readValue(is, AccessTokenResponse.class); tokenResponse = JsonSerialization.readValue(is, AccessTokenResponse.class);
} finally { } finally {
try { try {
is.close(); is.close();
} catch (java.io.IOException ignored) { } } catch (java.io.IOException ignored) { }
} }
} finally {
client.getConnectionManager().shutdown();
}
return (tokenResponse); return (tokenResponse);
} }

View file

@ -98,7 +98,7 @@ public class KeycloakDeploymentBuilder {
if (realmKeyPem == null && adapterConfig.isBearerOnly() && adapterConfig.getAuthServerUrl() == null) { if (realmKeyPem == null && adapterConfig.isBearerOnly() && adapterConfig.getAuthServerUrl() == null) {
throw new IllegalArgumentException("For bearer auth, you must set the realm-public-key or auth-server-url"); throw new IllegalArgumentException("For bearer auth, you must set the realm-public-key or auth-server-url");
} }
if (realmKeyPem == null || !deployment.isBearerOnly() || deployment.isRegisterNodeAtStartup() || deployment.getRegisterNodePeriod() != -1) { if (realmKeyPem == null || !deployment.isBearerOnly() || deployment.isEnableBasicAuth() || deployment.isRegisterNodeAtStartup() || deployment.getRegisterNodePeriod() != -1) {
deployment.setClient(new HttpClientBuilder().build(adapterConfig)); deployment.setClient(new HttpClientBuilder().build(adapterConfig));
} }
if (adapterConfig.getAuthServerUrl() == null && (!deployment.isBearerOnly() || realmKeyPem == null)) { if (adapterConfig.getAuthServerUrl() == null && (!deployment.isBearerOnly() || realmKeyPem == null)) {