Search in sources :

Example 36 with AuthenticationContext

use of org.wildfly.security.auth.client.AuthenticationContext 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 37 with AuthenticationContext

use of org.wildfly.security.auth.client.AuthenticationContext 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 38 with AuthenticationContext

use of org.wildfly.security.auth.client.AuthenticationContext 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)

Example 39 with AuthenticationContext

use of org.wildfly.security.auth.client.AuthenticationContext 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 40 with AuthenticationContext

use of org.wildfly.security.auth.client.AuthenticationContext in project wildfly by wildfly.

the class RemoteNamingHTTPTestCase method setup.

@BeforeClass
public static void setup() {
    AuthenticationConfiguration config = AuthenticationConfiguration.EMPTY.useName("user1").usePassword("password1");
    AuthenticationContext context = AuthenticationContext.empty().with(MatchRule.ALL, config);
    old = AuthenticationContext.captureCurrent();
    AuthenticationContext.getContextManager().setGlobalDefault(context);
}
Also used : AuthenticationConfiguration(org.wildfly.security.auth.client.AuthenticationConfiguration) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) BeforeClass(org.junit.BeforeClass)

Aggregations

AuthenticationContext (org.wildfly.security.auth.client.AuthenticationContext)48 AuthenticationConfiguration (org.wildfly.security.auth.client.AuthenticationConfiguration)28 Client (javax.ws.rs.client.Client)24 ClientBuilder (javax.ws.rs.client.ClientBuilder)24 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)24 Test (org.junit.Test)24 Response (javax.ws.rs.core.Response)21 URL (java.net.URL)19 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)11 InvalidAuthenticationConfigurationException (org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException)11 HttpClient (org.apache.http.client.HttpClient)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 BeforeClass (org.junit.BeforeClass)6 AuthenticationContextConfigurationClient (org.wildfly.security.auth.client.AuthenticationContextConfigurationClient)6 BearerTokenCredential (org.wildfly.security.credential.BearerTokenCredential)5 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 NamingException (javax.naming.NamingException)4 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)4 OptionMap (org.xnio.OptionMap)4