Search in sources :

Example 6 with HttpClientBuilder

use of org.keycloak.adapters.HttpClientBuilder in project keycloak by keycloak.

the class LoginPageTest method realmLocalizationMessagesAreNotCachedWithinTheTheme.

// KEYCLOAK-18590
@Test
public void realmLocalizationMessagesAreNotCachedWithinTheTheme() throws IOException {
    final String locale = Locale.ENGLISH.toLanguageTag();
    final String realmLocalizationMessageKey = "loginAccountTitle";
    final String realmLocalizationMessageValue = "Localization Test";
    try (CloseableHttpClient httpClient = (CloseableHttpClient) new HttpClientBuilder().build()) {
        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
        testRealm().localization().saveRealmLocalizationText(locale, realmLocalizationMessageKey, realmLocalizationMessageValue);
        ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
        loginPage.open();
        try (Response responseWithLocalization = client.target(driver.getCurrentUrl()).request().acceptLanguage(locale).get()) {
            assertThat(responseWithLocalization.readEntity(String.class), Matchers.containsString(realmLocalizationMessageValue));
            testRealm().localization().deleteRealmLocalizationText(locale, realmLocalizationMessageKey);
            loginPage.open();
            try (Response responseWithoutLocalization = client.target(driver.getCurrentUrl()).request().acceptLanguage(locale).get()) {
                assertThat(responseWithoutLocalization.readEntity(String.class), Matchers.not(Matchers.containsString(realmLocalizationMessageValue)));
            }
        }
        client.close();
    }
}
Also used : Response(javax.ws.rs.core.Response) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) HttpClientBuilder(org.keycloak.adapters.HttpClientBuilder) Test(org.junit.Test)

Example 7 with HttpClientBuilder

use of org.keycloak.adapters.HttpClientBuilder in project alfresco-repository by Alfresco.

the class AuthenticatorAuthzClientFactoryBean method getObject.

@Override
public AuthzClient getObject() throws Exception {
    // for instance when Keycloak is configured for 'bearer only' authentication or Direct Access Grants are disabled.
    if (!enabled) {
        return null;
    }
    // Build default http client using the keycloak client builder.
    int conTimeout = identityServiceConfig.getClientConnectionTimeout();
    int socTimeout = identityServiceConfig.getClientSocketTimeout();
    HttpClient client = new HttpClientBuilder().establishConnectionTimeout(conTimeout, TimeUnit.MILLISECONDS).socketTimeout(socTimeout, TimeUnit.MILLISECONDS).build(this.identityServiceConfig);
    // Add secret to credentials if needed.
    // AuthzClient configuration needs credentials with a secret even if the client in Keycloak is configured as public.
    Map<String, Object> credentials = identityServiceConfig.getCredentials();
    if (credentials == null || !credentials.containsKey("secret")) {
        credentials = credentials == null ? new HashMap<>() : new HashMap<>(credentials);
        credentials.put("secret", "");
    }
    // Create default AuthzClient for authenticating users against keycloak
    String authServerUrl = identityServiceConfig.getAuthServerUrl();
    String realm = identityServiceConfig.getRealm();
    String resource = identityServiceConfig.getResource();
    Configuration authzConfig = new Configuration(authServerUrl, realm, resource, credentials, client);
    AuthzClient authzClient = AuthzClient.create(authzConfig);
    if (logger.isDebugEnabled()) {
        logger.debug(" Created Keycloak AuthzClient");
        logger.debug(" Keycloak AuthzClient server URL: " + authzClient.getConfiguration().getAuthServerUrl());
        logger.debug(" Keycloak AuthzClient realm: " + authzClient.getConfiguration().getRealm());
        logger.debug(" Keycloak AuthzClient resource: " + authzClient.getConfiguration().getResource());
    }
    return authzClient;
}
Also used : AuthzClient(org.keycloak.authorization.client.AuthzClient) Configuration(org.keycloak.authorization.client.Configuration) HashMap(java.util.HashMap) HttpClient(org.apache.http.client.HttpClient) HttpClientBuilder(org.keycloak.adapters.HttpClientBuilder)

Example 8 with HttpClientBuilder

use of org.keycloak.adapters.HttpClientBuilder in project alfresco-repository by Alfresco.

the class IdentityServiceDeploymentFactoryBean method getObject.

@Override
public KeycloakDeployment getObject() throws Exception {
    KeycloakDeployment deployment = KeycloakDeploymentBuilder.build(this.identityServiceConfig);
    // This can be removed if the future versions of Keycloak accept timeout values through the config.
    if (deployment.getClient() != null) {
        int connectionTimeout = identityServiceConfig.getClientConnectionTimeout();
        int socketTimeout = identityServiceConfig.getClientSocketTimeout();
        HttpClient client = new HttpClientBuilder().establishConnectionTimeout(connectionTimeout, TimeUnit.MILLISECONDS).socketTimeout(socketTimeout, TimeUnit.MILLISECONDS).build(this.identityServiceConfig);
        deployment.setClient(client);
        if (logger.isDebugEnabled()) {
            logger.debug("Created HttpClient for Keycloak deployment with connection timeout: " + connectionTimeout + " ms, socket timeout: " + socketTimeout + " ms.");
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("HttpClient for Keycloak deployment was not set.");
        }
    }
    if (logger.isInfoEnabled()) {
        logger.info("Keycloak JWKS URL: " + deployment.getJwksUrl());
        logger.info("Keycloak Realm: " + deployment.getRealm());
        logger.info("Keycloak Client ID: " + deployment.getResourceName());
    }
    return deployment;
}
Also used : KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) HttpClient(org.apache.http.client.HttpClient) HttpClientBuilder(org.keycloak.adapters.HttpClientBuilder)

Aggregations

HttpClientBuilder (org.keycloak.adapters.HttpClientBuilder)8 HttpClient (org.apache.http.client.HttpClient)4 HttpResponse (org.apache.http.HttpResponse)3 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)3 ApacheHttpClient4Engine (org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Response (javax.ws.rs.core.Response)2 HttpEntity (org.apache.http.HttpEntity)2 HttpGet (org.apache.http.client.methods.HttpGet)2 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)2 Test (org.junit.Test)2 KeycloakSecurityContext (org.keycloak.KeycloakSecurityContext)2 AuthScope (org.apache.http.auth.AuthScope)1 Credentials (org.apache.http.auth.Credentials)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1