use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project wildfly by wildfly.
the class PasswordMechTestBase method testSuccess.
@Test
public void testSuccess() throws Exception {
HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role1"));
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password1");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credentials);
try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()) {
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", SimpleServlet.RESPONSE_BODY, EntityUtils.toString(response.getEntity()));
}
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project wildfly by wildfly.
the class PasswordMechTestBase method testInsufficientRole.
@Test
public void testInsufficientRole() throws Exception {
HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role2"));
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password1");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credentials);
try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()) {
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_FORBIDDEN, statusCode);
}
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project wildfly by wildfly.
the class SilentBasicMechTestCase method testInvalidPrincipal.
@Override
@Test
public void testInvalidPrincipal() throws Exception {
HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role1"));
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1wrong", "password1");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credentials);
try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()) {
request.addHeader(new BasicScheme().authenticate(credentials, request, null));
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", HttpStatus.SC_UNAUTHORIZED, statusCode);
assertEquals("Unexpected content of HTTP response.", LOGIN_PAGE_CONTENT, EntityUtils.toString(response.getEntity()));
}
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project wildfly by wildfly.
the class SilentBasicMechTestCase method testInsufficientRole.
@Test
public void testInsufficientRole() throws Exception {
HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role2"));
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password1");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credentials);
try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()) {
request.addHeader(new BasicScheme().authenticate(credentials, request, null));
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", HttpStatus.SC_FORBIDDEN, statusCode);
assertTrue("Unexpected content of HTTP response.", EntityUtils.toString(response.getEntity()).contains(FORBIDDEN_CONTENT));
}
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project wildfly by wildfly.
the class BasicAuthenticationWebFailoverTestCase method test.
@Test
public void test(@ArquillianResource(SecureServlet.class) @OperateOnDeployment(DEPLOYMENT_1) URL baseURL1, @ArquillianResource(SecureServlet.class) @OperateOnDeployment(DEPLOYMENT_2) URL baseURL2) throws IOException, URISyntaxException {
CredentialsProvider provider = new BasicCredentialsProvider();
HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
URI uri1 = SecureServlet.createURI(baseURL1);
URI uri2 = SecureServlet.createURI(baseURL2);
try {
// Valid login, invalid role
setCredentials(provider, "forbidden", "password", baseURL1, baseURL2);
HttpResponse response = client.execute(new HttpGet(uri1));
try {
Assert.assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatusLine().getStatusCode());
} finally {
HttpClientUtils.closeQuietly(response);
}
// Invalid login, valid role
setCredentials(provider, "allowed", "bad", baseURL1, baseURL2);
response = client.execute(new HttpGet(uri1));
try {
Assert.assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
} finally {
HttpClientUtils.closeQuietly(response);
}
// Valid login, valid role
setCredentials(provider, "allowed", "password", baseURL1, baseURL2);
String sessionId = null;
response = client.execute(new HttpGet(uri1));
try {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(response.getFirstHeader(SecureServlet.SESSION_ID_HEADER));
sessionId = response.getFirstHeader(SecureServlet.SESSION_ID_HEADER).getValue();
} finally {
HttpClientUtils.closeQuietly(response);
}
undeploy(DEPLOYMENT_1);
response = client.execute(new HttpGet(uri2));
try {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals(sessionId, response.getFirstHeader(SecureServlet.SESSION_ID_HEADER).getValue());
} finally {
HttpClientUtils.closeQuietly(response);
}
deploy(DEPLOYMENT_1);
response = client.execute(new HttpGet(uri1));
try {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals(sessionId, response.getFirstHeader(SecureServlet.SESSION_ID_HEADER).getValue());
} finally {
HttpClientUtils.closeQuietly(response);
}
} finally {
HttpClientUtils.closeQuietly(client);
}
}
Aggregations