use of org.apache.hc.client5.http.impl.auth.BasicAuthCache in project wiremock by wiremock.
the class WireMockTestClient method getWithPreemptiveCredentials.
public WireMockResponse getWithPreemptiveCredentials(String url, int port, String username, String password) {
HttpHost target = new HttpHost("localhost", port);
CloseableHttpClient httpClient = httpClientWithPreemptiveAuth(target, username, password);
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
try {
HttpGet httpget = new HttpGet(url);
ClassicHttpResponse response = httpClient.execute(target, httpget, localContext);
return new WireMockResponse(response);
} catch (IOException e) {
return throwUnchecked(e, WireMockResponse.class);
}
}
use of org.apache.hc.client5.http.impl.auth.BasicAuthCache in project commons-vfs by apache.
the class Http5FileProvider method createHttpClientContext.
/**
* Create an {@link HttpClientContext} object for an http4 file system.
*
* @param builder Configuration options builder for http4 provider
* @param rootName The root path
* @param fileSystemOptions The FileSystem options
* @param authData The {@code UserAuthentiationData} object
* @return an {@link HttpClientContext} object
*/
protected HttpClientContext createHttpClientContext(final Http5FileSystemConfigBuilder builder, final GenericFileName rootName, final FileSystemOptions fileSystemOptions, final UserAuthenticationData authData) {
final HttpClientContext clientContext = HttpClientContext.create();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
clientContext.setCredentialsProvider(credsProvider);
final String username = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())));
final char[] password = UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()));
if (!StringUtils.isEmpty(username)) {
// set root port
credsProvider.setCredentials(new AuthScope(rootName.getHostName(), rootName.getPort()), new UsernamePasswordCredentials(username, password));
}
final HttpHost proxyHost = getProxyHttpHost(builder, fileSystemOptions);
if (proxyHost != null) {
final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
if (proxyAuth != null) {
final UserAuthenticationData proxyAuthData = UserAuthenticatorUtils.authenticate(proxyAuth, new UserAuthenticationData.Type[] { UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD });
if (proxyAuthData != null) {
final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(proxyAuthData, UserAuthenticationData.USERNAME, null)), UserAuthenticatorUtils.getData(proxyAuthData, UserAuthenticationData.PASSWORD, null));
// set proxy host port
credsProvider.setCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), proxyCreds);
}
if (builder.isPreemptiveAuth(fileSystemOptions)) {
final AuthCache authCache = new BasicAuthCache();
final BasicScheme basicAuth = new BasicScheme();
authCache.put(proxyHost, basicAuth);
clientContext.setAuthCache(authCache);
}
}
}
return clientContext;
}
use of org.apache.hc.client5.http.impl.auth.BasicAuthCache in project gradle-download-task by michel-kraemer.
the class DownloadAction method addAuthentication.
/**
* Add authentication information for the given host
* @param host the host
* @param credentials the credentials
* @param context the context in which the authentication information
* should be saved
*/
private void addAuthentication(HttpHost host, Credentials credentials, HttpClientContext context) {
AuthCache authCache = context.getAuthCache();
if (authCache == null) {
authCache = new BasicAuthCache();
context.setAuthCache(authCache);
}
CredentialsProvider credsProvider = context.getCredentialsProvider();
if (credsProvider == null) {
credsProvider = new BasicCredentialsProvider();
context.setCredentialsProvider(credsProvider);
}
((CredentialsStore) credsProvider).setCredentials(new AuthScope(host), credentials);
}
Aggregations