Search in sources :

Example 6 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project keycloak by keycloak.

the class HoKTest method getUserInfoByHoKAccessTokenWithClientCertificate.

// verify HoK Token - Get UserInfo
@Test
public void getUserInfoByHoKAccessTokenWithClientCertificate() throws Exception {
    // get an access token
    oauth.doLogin("test-user@localhost", "password");
    EventRepresentation loginEvent = events.expectLogin().assertEvent();
    String sessionId = loginEvent.getSessionId();
    String codeId = loginEvent.getDetails().get(Details.CODE_ID);
    String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
    AccessTokenResponse tokenResponse = null;
    try (CloseableHttpClient client = MutualTLSUtils.newCloseableHttpClientWithDefaultKeyStoreAndTrustStore()) {
        tokenResponse = oauth.doAccessTokenRequest(code, "password", client);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    verifyHoKTokenDefaultCertThumbPrint(tokenResponse);
    events.expectCodeToToken(codeId, sessionId).assertEvent();
    // execute the access token to get UserInfo with token binded client certificate in mutual authentication TLS
    ClientBuilder clientBuilder = ClientBuilder.newBuilder();
    KeyStore keystore = null;
    keystore = KeystoreUtil.loadKeyStore(MutualTLSUtils.DEFAULT_KEYSTOREPATH, MutualTLSUtils.DEFAULT_KEYSTOREPASSWORD);
    clientBuilder.keyStore(keystore, MutualTLSUtils.DEFAULT_KEYSTOREPASSWORD);
    Client client = clientBuilder.build();
    WebTarget userInfoTarget = null;
    Response response = null;
    try {
        userInfoTarget = UserInfoClientUtil.getUserInfoWebTarget(client);
        response = userInfoTarget.request().header(HttpHeaders.AUTHORIZATION, "Bearer " + tokenResponse.getAccessToken()).get();
        testSuccessfulUserInfoResponse(response);
    } finally {
        response.close();
        client.close();
    }
}
Also used : AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) EventRepresentation(org.keycloak.representations.idm.EventRepresentation) IOException(java.io.IOException) WebTarget(javax.ws.rs.client.WebTarget) OAuthClient(org.keycloak.testsuite.util.OAuthClient) Client(javax.ws.rs.client.Client) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) KeyStore(java.security.KeyStore) ClientBuilder(javax.ws.rs.client.ClientBuilder) RefreshTokenTest(org.keycloak.testsuite.oauth.RefreshTokenTest) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 7 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project nifi-registry by apache.

the class IntegrationTestBase method createClientFromConfig.

private static Client createClientFromConfig(NiFiRegistryClientConfig registryClientConfig) {
    final ClientConfig clientConfig = new ClientConfig();
    clientConfig.register(jacksonJaxbJsonProvider());
    final ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);
    final SSLContext sslContext = registryClientConfig.getSslContext();
    if (sslContext != null) {
        clientBuilder.sslContext(sslContext);
    }
    final HostnameVerifier hostnameVerifier = registryClientConfig.getHostnameVerifier();
    if (hostnameVerifier != null) {
        clientBuilder.hostnameVerifier(hostnameVerifier);
    }
    return clientBuilder.build();
}
Also used : SSLContext(javax.net.ssl.SSLContext) NiFiRegistryClientConfig(org.apache.nifi.registry.client.NiFiRegistryClientConfig) ClientConfig(org.glassfish.jersey.client.ClientConfig) ClientBuilder(javax.ws.rs.client.ClientBuilder) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 8 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project cloudbreak by hortonworks.

the class RestClientUtil method createClient.

private static Client createClient(ConfigKey configKey) {
    LOGGER.debug("Constructing jax rs client: {}", configKey);
    ClientConfig config = new ClientConfig();
    config.property(ClientProperties.FOLLOW_REDIRECTS, "false");
    config.property(ClientProperties.CONNECT_TIMEOUT, CONNECT_TIMEOUT_MS);
    config.register(MultiPartFeature.class);
    ClientBuilder builder = ClientBuilder.newBuilder().withConfig(config);
    if (configKey.isDebug()) {
        builder = builder.register(new LoggingFilter(java.util.logging.Logger.getLogger(RestClientUtil.class.getName()), true));
    }
    if (!configKey.isSecure()) {
        builder.sslContext(CertificateTrustManager.sslContext());
        builder.hostnameVerifier(CertificateTrustManager.hostnameVerifier());
    }
    Client client = builder.build();
    client.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, configKey.isIgnorePreValidation());
    SSLContext sslContext = client.getSslContext();
    LOGGER.warn("RestClient has been constructed: {}, client: {}, sslContext: {}", configKey, client, sslContext);
    return client;
}
Also used : LoggingFilter(org.glassfish.jersey.filter.LoggingFilter) SSLContext(javax.net.ssl.SSLContext) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 9 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project openstack4j by ContainX.

the class ClientFactory method buildClientFromConfig.

private static Client buildClientFromConfig(Config config) {
    ClientConfig clientConfig = new ClientConfig();
    if (config.getProxy() != null) {
        addProxy(clientConfig, config);
    }
    ClientBuilder cb = ClientBuilder.newBuilder().withConfig(clientConfig).property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, "true").register(JacksonFeature.class).register(RESOLVER).register(new RequestFilter());
    if (config.getSslContext() != null)
        cb.sslContext(config.getSslContext());
    else if (config.isIgnoreSSLVerification())
        cb.sslContext(UntrustedSSL.getSSLContext());
    if (config.getHostNameVerifier() != null)
        cb.hostnameVerifier(config.getHostNameVerifier());
    else if (config.isIgnoreSSLVerification())
        cb.hostnameVerifier(UntrustedSSL.getHostnameVerifier());
    if (config.getReadTimeout() > 0)
        cb.property(ClientProperties.READ_TIMEOUT, config.getReadTimeout());
    if (config.getConnectTimeout() > 0)
        cb.property(ClientProperties.CONNECT_TIMEOUT, config.getConnectTimeout());
    return cb.build();
}
Also used : ClientConfig(org.glassfish.jersey.client.ClientConfig) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 10 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project wildfly by wildfly.

the class UndertowSSLv2HelloTestCase method testOneWayElytronClientServerSupportsSSLv2Hello.

/**
 * One way SSL - RESTEasy client sends SSLv2Hello message and server supports the protocol.
 * Handshake should succeed.
 */
@Test
public void testOneWayElytronClientServerSupportsSSLv2Hello() throws Exception {
    configureSSLContext(SSLV2HELLO_CONTEXT_ONE_WAY);
    AuthenticationContext context = doPrivileged((PrivilegedAction<AuthenticationContext>) () -> {
        try {
            URL config = getClass().getResource("wildfly-config-one-way-sslv2hello.xml");
            return ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI()).create();
        } catch (Throwable t) {
            throw new InvalidAuthenticationConfigurationException(t);
        }
    });
    context.run(() -> {
        ClientBuilder clientBuilder = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true);
        Client client = clientBuilder.build();
        Response response = client.target(String.valueOf(securedRootUrl)).request().get();
        Assert.assertEquals(200, response.getStatus());
    });
    restoreConfiguration();
}
Also used : Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) InvalidAuthenticationConfigurationException(org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) URL(java.net.URL) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Aggregations

ClientBuilder (javax.ws.rs.client.ClientBuilder)57 Client (javax.ws.rs.client.Client)41 Response (javax.ws.rs.core.Response)26 Test (org.junit.Test)26 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)24 AuthenticationContext (org.wildfly.security.auth.client.AuthenticationContext)24 URL (java.net.URL)20 SSLContext (javax.net.ssl.SSLContext)16 ClientConfig (org.glassfish.jersey.client.ClientConfig)15 AuthenticationConfiguration (org.wildfly.security.auth.client.AuthenticationConfiguration)13 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)11 InvalidAuthenticationConfigurationException (org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException)11 WebTarget (javax.ws.rs.client.WebTarget)10 IOException (java.io.IOException)9 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)9 KeyStore (java.security.KeyStore)7 HttpClient (org.apache.http.client.HttpClient)7 AuthenticationContextConfigurationClient (org.wildfly.security.auth.client.AuthenticationContextConfigurationClient)6 MalformedURLException (java.net.MalformedURLException)5 HostnameVerifier (javax.net.ssl.HostnameVerifier)5