use of org.apache.http.impl.auth.BasicScheme in project ats-framework by Axway.
the class HttpClient method setupAuthentication.
/**
* Set up authentication for HTTP Basic/HTTP Digest/SPNEGO.
*
* @param httpClientBuilder The client builder
* @return The context
* @throws HttpException
*/
private void setupAuthentication(HttpClientBuilder httpClientBuilder) throws HttpException {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password));
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
if (authType == AuthType.always) {
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
HttpHost target = new HttpHost(host, port, isOverSsl ? "https" : "http");
authCache.put(target, basicAuth);
// Add AuthCache to the execution context
httpContext.setAuthCache(authCache);
} else {
if (!StringUtils.isNullOrEmpty(kerberosServicePrincipalName)) {
GssClient gssClient = new GssClient(username, password, kerberosClientKeytab, krb5ConfFile);
AuthSchemeProvider nsf = new SPNegoSchemeFactory(gssClient, kerberosServicePrincipalName, kerberosServicePrincipalType);
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, nsf).build();
httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
}
}
}
use of org.apache.http.impl.auth.BasicScheme in project wildfly by wildfly.
the class SilentBasicMechTestCase method testBasicWithCredentialSuccess.
@Test
public void testBasicWithCredentialSuccess() 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()) {
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.", SC_OK, statusCode);
assertEquals("Unexpected content of HTTP response.", SimpleServlet.RESPONSE_BODY, EntityUtils.toString(response.getEntity()));
}
}
}
use of org.apache.http.impl.auth.BasicScheme in project wildfly by wildfly.
the class SilentBasicMechTestCase method testInvalidCredential.
@Override
@Test
public void testInvalidCredential() throws Exception {
HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role1"));
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password1wrong");
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.apache.http.impl.auth.BasicScheme in project wildfly by wildfly.
the class ProactiveAuthModeTestCase method testUnsecureResourceWithInvalidCredential.
@Test
public void testUnsecureResourceWithInvalidCredential() throws Exception {
HttpGet request = new HttpGet(new URI(url.toExternalForm() + "unsecure.jsp"));
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password2");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credentials);
request.addHeader(new BasicScheme().authenticate(credentials, request));
try (CloseableHttpClient httpClient = HttpClients.custom().build()) {
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
assertEquals("Unexpected status code in HTTP response.", SC_UNAUTHORIZED, statusCode);
}
}
}
use of org.apache.http.impl.auth.BasicScheme in project wildfly by wildfly.
the class ConstraintDrivenAuthModeTestCase method testUnsecuredResourceWithValidCredential.
@Test
public void testUnsecuredResourceWithValidCredential() throws Exception {
HttpGet request = new HttpGet(new URI(url.toExternalForm() + "unsecure.jsp"));
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password1");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credentials);
request.addHeader(new BasicScheme().authenticate(credentials, "UTF-8", false));
try (CloseableHttpClient httpClient = HttpClients.custom().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.", "", EntityUtils.toString(response.getEntity()));
}
}
}
Aggregations