Search in sources :

Example 36 with ClientBuilder

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

the class UndertowTwoWaySslNeedClientAuthTestCase method testResteasyElytronClientMissingTruststore.

/**
 * RESTEasy client loads SSL Context from Elytron client config.
 * This SSL Context does not have truststore configured, so exception is expected.
 */
@Test(expected = ProcessingException.class)
public void testResteasyElytronClientMissingTruststore() {
    AuthenticationContext context = doPrivileged((PrivilegedAction<AuthenticationContext>) () -> {
        try {
            URL config = getClass().getResource("wildfly-config-correct-truststore-missing.xml");
            return ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI()).create();
        } catch (Throwable t) {
            throw new InvalidAuthenticationConfigurationException(t);
        }
    });
    context.run(() -> {
        ClientBuilder resteasyClientBuilder = ClientBuilder.newBuilder();
        Client client = resteasyClientBuilder.build();
        Response response = client.target(String.valueOf(securedRootUrl)).request().get();
        Assert.assertEquals("Hello World!", response.readEntity(String.class));
        Assert.assertEquals(200, response.getStatus());
    });
}
Also used : Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) InvalidAuthenticationConfigurationException(org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException) AuthenticationContextConfigurationClient(org.wildfly.security.auth.client.AuthenticationContextConfigurationClient) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) HttpClient(org.apache.http.client.HttpClient) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) URL(java.net.URL) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 37 with ClientBuilder

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

the class UndertowTwoWaySslNeedClientAuthTestCase method testResteasyElytronClientTrustedServer.

/**
 * RESTEasy client loads truststore from Elytron client configuration. This truststore contains correct server certificate.
 */
@Test
public void testResteasyElytronClientTrustedServer() {
    AuthenticationContext context = doPrivileged((PrivilegedAction<AuthenticationContext>) () -> {
        try {
            URL config = getClass().getResource("wildfly-config-correct-truststore.xml");
            return ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI()).create();
        } catch (Throwable t) {
            throw new InvalidAuthenticationConfigurationException(t);
        }
    });
    context.run(() -> {
        ClientBuilder resteasyClientBuilder = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true);
        Client client = resteasyClientBuilder.build();
        Response response = client.target(String.valueOf(securedRootUrl)).request().get();
        Assert.assertEquals(200, response.getStatus());
    });
}
Also used : Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) InvalidAuthenticationConfigurationException(org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException) AuthenticationContextConfigurationClient(org.wildfly.security.auth.client.AuthenticationContextConfigurationClient) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) HttpClient(org.apache.http.client.HttpClient) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) URL(java.net.URL) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 38 with ClientBuilder

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

the class ClientConfigProviderBearerTokenTest method testClientChooseCorrectBearerToken.

/**
 * Test that request does choose bearer token based on destination of the request.
 * This test will fail since bearer token was set on different URL.
 */
@Test
public void testClientChooseCorrectBearerToken() {
    AuthenticationContext previousAuthContext = AuthenticationContext.getContextManager().getGlobalDefault();
    try {
        BearerTokenCredential bearerTokenCredential = new BearerTokenCredential("myTestToken");
        AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty().useBearerTokenCredential(bearerTokenCredential);
        AuthenticationContext context = AuthenticationContext.empty();
        context = context.with(MatchRule.ALL.matchHost("www.redhat.com"), adminConfig);
        AuthenticationContext.getContextManager().setGlobalDefault(context);
        context.run(() -> {
            ClientBuilder builder = ClientBuilder.newBuilder();
            Client client = builder.build();
            try {
                client.target(dummyUrl.toString()).register(ClientConfigProviderBearerTokenAbortFilter.class).request().get();
                fail("Configuration not found ex should be thrown.");
            } catch (Exception e) {
                assertTrue(e.getMessage().contains("The request authorization header is not correct expected:<Bearer myTestToken> but was:<null>"));
            } finally {
                client.close();
            }
        });
    } finally {
        AuthenticationContext.getContextManager().setGlobalDefault(previousAuthContext);
    }
}
Also used : AuthenticationConfiguration(org.wildfly.security.auth.client.AuthenticationConfiguration) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) BearerTokenCredential(org.wildfly.security.credential.BearerTokenCredential) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 39 with ClientBuilder

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

the class ClientConfigProviderBearerTokenTest method testClientWithBearerTokenAndCredentials.

/**
 * Test that RESTEasy client uses Bearer token auth and not HTTP BASIC if both username with password and bearer token are present in Elytron client config.
 * This is done with registered filter that checks Authorization header.
 */
@Test
public void testClientWithBearerTokenAndCredentials() {
    AuthenticationContext previousAuthContext = AuthenticationContext.getContextManager().getGlobalDefault();
    try {
        BearerTokenCredential bearerTokenCredential = new BearerTokenCredential("myTestToken");
        AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty().useName("username").usePassword("password").useBearerTokenCredential(bearerTokenCredential);
        AuthenticationContext context = AuthenticationContext.empty();
        context = context.with(MatchRule.ALL, adminConfig);
        AuthenticationContext.getContextManager().setGlobalDefault(context);
        context.run(() -> {
            ClientBuilder builder = ClientBuilder.newBuilder();
            Client client = builder.build();
            Response response = client.target(dummyUrl.toString()).register(ClientConfigProviderBearerTokenAbortFilter.class).request().get();
            Assert.assertEquals(SC_OK, response.getStatus());
            client.close();
        });
    } finally {
        AuthenticationContext.getContextManager().setGlobalDefault(previousAuthContext);
    }
}
Also used : AuthenticationConfiguration(org.wildfly.security.auth.client.AuthenticationConfiguration) Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) BearerTokenCredential(org.wildfly.security.credential.BearerTokenCredential) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 40 with ClientBuilder

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

the class ClientConfigProviderBearerTokenTest method testClientWithoutBearerToken.

/**
 * Test that request does not contain Bearer token if none is retrieved from Elytron client config.
 * This is done with registered filter that checks Authorization header.
 */
@Test
public void testClientWithoutBearerToken() {
    AuthenticationContext previousAuthContext = AuthenticationContext.getContextManager().getGlobalDefault();
    try {
        AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty();
        AuthenticationContext context = AuthenticationContext.empty();
        context = context.with(MatchRule.ALL, adminConfig);
        AuthenticationContext.getContextManager().setGlobalDefault(context);
        context.run(() -> {
            ClientBuilder builder = ClientBuilder.newBuilder();
            Client client = builder.build();
            try {
                client.target(dummyUrl.toString().toString()).register(ClientConfigProviderBearerTokenAbortFilter.class).request().get();
                fail("Configuration not found ex should be thrown.");
            } catch (Exception e) {
                assertTrue(e.getMessage().contains("The request authorization header is not correct expected:<Bearer myTestToken> but was:<null>"));
            } finally {
                client.close();
            }
        });
    } finally {
        AuthenticationContext.getContextManager().setGlobalDefault(previousAuthContext);
    }
}
Also used : AuthenticationConfiguration(org.wildfly.security.auth.client.AuthenticationConfiguration) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) 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