Search in sources :

Example 16 with ClientBuilder

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

the class BasicAuthnTestCase method testClientConfigProviderUsernameWithoutPasswordWillBeIgnored.

/**
 * Test that access credentials from ClientConfigProvider are used only if both username and password are present.
 */
@Test
public void testClientConfigProviderUsernameWithoutPasswordWillBeIgnored(@ArquillianResource URL url) throws MalformedURLException {
    final URL servletUrl = new URL(url.toExternalForm() + "role1");
    AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty().useName("thisNameWillBeIgnoredBecausePasswordIsMissing");
    AuthenticationContext context = AuthenticationContext.empty();
    context = context.with(MatchRule.ALL, adminConfig);
    context.run(() -> {
        ClientBuilder builder = ClientBuilder.newBuilder();
        Client client = builder.build();
        client.register(new ClientConfigProviderNoBasicAuthorizationHeaderFilter(), Priorities.USER);
        try {
            client.target(servletUrl.toString()).request().get();
        } catch (Exception e) {
            assertTrue(e.getMessage().contains("The request authorization header is not correct expected:<Bearer myTestToken> but was:<null>"));
            client.close();
        }
        Response response = builder.build().target(servletUrl.toString()).request().get();
        Assert.assertEquals(SC_UNAUTHORIZED, response.getStatus());
        client.close();
    });
}
Also used : AuthenticationConfiguration(org.wildfly.security.auth.client.AuthenticationConfiguration) Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) ClientConfigProviderNoBasicAuthorizationHeaderFilter(org.wildfly.test.integration.elytron.util.ClientConfigProviderNoBasicAuthorizationHeaderFilter) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 17 with ClientBuilder

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

the class BasicAuthnTestCase method testClientConfigCredentialsAreIgnoredIfSpecified.

/**
 * Test that RESTEasy client ignores ClientConfigProvider credentials if credentials are specified directly by user for RESTEasy client.
 */
@Test
public void testClientConfigCredentialsAreIgnoredIfSpecified(@ArquillianResource URL url) throws MalformedURLException {
    final URL servletUrl = new URL(url.toExternalForm() + "role1");
    AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty().useName("incorrectUsername").usePassword("incorrectPassword");
    AuthenticationContext context = AuthenticationContext.empty();
    context = context.with(MatchRule.ALL, adminConfig);
    context.run(() -> {
        ClientBuilder builder = ClientBuilder.newBuilder();
        Client client = builder.build();
        client.register(HttpAuthorization.basic("user1", "password1"));
        Response response = client.target(servletUrl.toString()).request().get();
        Assert.assertEquals(SC_OK, response.getStatus());
        client.close();
    });
}
Also used : AuthenticationConfiguration(org.wildfly.security.auth.client.AuthenticationConfiguration) Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) URL(java.net.URL) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 18 with ClientBuilder

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

the class BasicAuthnTestCase method testClientConfigBearerTokenIsIgnoredIfBasicSpecified.

/**
 * Test secured resource with correct credentials of user that is authorized to the resource.
 * Bearer token from ClientConfigProvider impl is ignored since credentials are specified for RESTEasy client.
 */
@Test
public void testClientConfigBearerTokenIsIgnoredIfBasicSpecified(@ArquillianResource URL url) throws MalformedURLException {
    final URL servletUrl = new URL(url.toExternalForm() + "role1");
    BearerTokenCredential bearerTokenCredential = new BearerTokenCredential("myTestToken");
    AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty().useBearerTokenCredential(bearerTokenCredential);
    AuthenticationContext context = AuthenticationContext.empty();
    context = context.with(MatchRule.ALL, adminConfig);
    context.run(() -> {
        ClientBuilder builder = ClientBuilder.newBuilder();
        Client client = builder.build();
        client.register(HttpAuthorization.basic("user1", "password1"));
        client.register(ClientConfigProviderBearerTokenAbortFilter.class);
        try {
            client.target(servletUrl.toString()).request().get();
            fail("Configuration not found ex should be thrown.");
        } catch (Exception e) {
            // check that bearer token was not added
            assertTrue(e.getMessage().contains("The request authorization header is not correct expected:<B[earer myTestToken]> but was:<B[asic"));
            client.close();
        }
    });
}
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) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 19 with ClientBuilder

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

the class BasicAuthnTestCase method testClientUnauthenticatedUser.

/**
 * Test that access will be unauthenticated when accessing secured resource with RESTEasy client without credentials set on Elytron client config.
 */
@Test
public void testClientUnauthenticatedUser(@ArquillianResource URL url) throws MalformedURLException {
    final URL servletUrl = new URL(url.toExternalForm() + "role1");
    AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty();
    AuthenticationContext context = AuthenticationContext.empty();
    context = context.with(MatchRule.ALL, adminConfig);
    context.run(() -> {
        ClientBuilder builder = ClientBuilder.newBuilder();
        Client client = builder.build();
        Response response = client.target(servletUrl.toString()).request().get();
        Assert.assertEquals(SC_UNAUTHORIZED, response.getStatus());
        client.close();
    });
}
Also used : AuthenticationConfiguration(org.wildfly.security.auth.client.AuthenticationConfiguration) Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) URL(java.net.URL) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 20 with ClientBuilder

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

the class BasicAuthnTestCase method testClientConfigForbiddenUser.

/**
 * Unauthorized user's credentials were set on Elytron client and so authentication will fail with 403.
 */
@Test
public void testClientConfigForbiddenUser(@ArquillianResource URL url) throws MalformedURLException {
    final URL servletUrl = new URL(url.toExternalForm() + "role1");
    AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty().useName("user2").usePassword("password2");
    AuthenticationContext context = AuthenticationContext.empty();
    context = context.with(MatchRule.ALL, adminConfig);
    context.run(() -> {
        ClientBuilder builder = ClientBuilder.newBuilder();
        Client client = builder.build();
        Response response = client.target(servletUrl.toString()).request().get();
        Assert.assertEquals(SC_FORBIDDEN, response.getStatus());
        client.close();
    });
}
Also used : AuthenticationConfiguration(org.wildfly.security.auth.client.AuthenticationConfiguration) Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) 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