Search in sources :

Example 41 with ClientBuilder

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

the class ClientConfigProviderBearerTokenTest method testClientWithBearerToken.

/**
 * Test that bearer token is loaded from Elytron client config and is used in Authorization header.
 * This is done with registered filter that checks Authorization header.
 */
@Test
public void testClientWithBearerToken() {
    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, 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 42 with ClientBuilder

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

the class BasicAuthnTestCase method testClientConfigProviderChooseCredentialsBasedOnDestination2.

/**
 * Test that ClientConfigProvider credentials are used when specified for requested  URL.
 */
@Test
public void testClientConfigProviderChooseCredentialsBasedOnDestination2(@ArquillianResource URL url) throws MalformedURLException {
    final URL servletUrl = new URL(url.toExternalForm() + "role1");
    AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty().useName("user1").usePassword("password1");
    AuthenticationContext context = AuthenticationContext.empty();
    context = context.with(MatchRule.ALL.matchHost(servletUrl.getHost()), adminConfig);
    context.run(() -> {
        ClientBuilder builder = ClientBuilder.newBuilder();
        Client client = builder.build();
        Response response = client.target(servletUrl.toString()).request().get();
        // will be authorized because we are calling hostname that credentials are set for
        Assert.assertEquals(SC_OK, response.getStatus());
        Assert.assertEquals("response was not GOOD", "GOOD", response.readEntity(String.class));
        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 43 with ClientBuilder

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

the class BasicAuthnTestCase method testRESTEasyClientUsesElytronConfigAuthenticatedUser.

/**
 *  Test that RESTEasy client successfully uses Elytron client configuration to authenticate to the secured server with HTTP BASIC auth.
 */
@Test
public void testRESTEasyClientUsesElytronConfigAuthenticatedUser(@ArquillianResource URL url) throws MalformedURLException {
    final URL servletUrl = new URL(url.toExternalForm() + "role1");
    AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty().useName("user1").usePassword("password1");
    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_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 44 with ClientBuilder

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

the class BasicAuthnTestCase method testClientConfigProviderChooseCredentialsBasedOnDestination.

/**
 * Test that Elytron config credentials are not used when specified for different destination of the request.
 */
@Test
public void testClientConfigProviderChooseCredentialsBasedOnDestination(@ArquillianResource URL url) throws MalformedURLException {
    final URL servletUrl = new URL(url.toExternalForm() + "role1");
    AuthenticationConfiguration adminConfig = AuthenticationConfiguration.empty().useName("user1").usePassword("password1");
    AuthenticationContext context = AuthenticationContext.empty();
    context = context.with(MatchRule.ALL.matchHost("www.some-example.com"), adminConfig);
    context.run(() -> {
        ClientBuilder builder = ClientBuilder.newBuilder();
        Client client = builder.build();
        Response response = client.target(servletUrl.toString()).request().get();
        // will be unauthorized because credentials were set for different hostname than we are calling
        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 45 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project Payara by payara.

the class ProxyImpl method addAuthenticationInfo.

/**
 * Use SSL to authenticate
 */
private Client addAuthenticationInfo(Client client, Server server, ServiceLocator habitat) {
    SecureAdmin secureAdmin = habitat.getService(SecureAdmin.class);
    // TODO need to get hardcoded "TLS" from corresponding ServerRemoteAdminCommand constant);
    final SSLContext sslContext = habitat.<SSLUtils>getService(SSLUtils.class).getAdminSSLContext(SecureAdmin.Util.DASAlias(secureAdmin), "TLS");
    // Instruct Jersey to use HostNameVerifier and SSLContext provided by us.
    final ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(client.getConfiguration()).hostnameVerifier(new BasicHostnameVerifier(server.getAdminHost())).sslContext(sslContext);
    return clientBuilder.build();
}
Also used : SecureAdmin(com.sun.enterprise.config.serverbeans.SecureAdmin) SSLContext(javax.net.ssl.SSLContext) SSLUtils(com.sun.enterprise.security.ssl.SSLUtils) ClientBuilder(javax.ws.rs.client.ClientBuilder)

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