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();
});
}
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();
});
}
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();
}
});
}
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();
});
}
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();
});
}
Aggregations