Search in sources :

Example 1 with BasicAuthCache

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);
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) BasicScheme(org.apache.hc.client5.http.impl.auth.BasicScheme) HttpHost(org.apache.hc.core5.http.HttpHost) BasicAuthCache(org.apache.hc.client5.http.impl.auth.BasicAuthCache) AuthCache(org.apache.hc.client5.http.auth.AuthCache) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) BasicAuthCache(org.apache.hc.client5.http.impl.auth.BasicAuthCache) IOException(java.io.IOException)

Example 2 with BasicAuthCache

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;
}
Also used : UserAuthenticationData(org.apache.commons.vfs2.UserAuthenticationData) BasicScheme(org.apache.hc.client5.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) HttpHost(org.apache.hc.core5.http.HttpHost) UserAuthenticator(org.apache.commons.vfs2.UserAuthenticator) AuthScope(org.apache.hc.client5.http.auth.AuthScope) AuthCache(org.apache.hc.client5.http.auth.AuthCache) BasicAuthCache(org.apache.hc.client5.http.impl.auth.BasicAuthCache) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) BasicAuthCache(org.apache.hc.client5.http.impl.auth.BasicAuthCache) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 3 with BasicAuthCache

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);
}
Also used : BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthCache(org.apache.hc.client5.http.auth.AuthCache) BasicAuthCache(org.apache.hc.client5.http.impl.auth.BasicAuthCache) CredentialsStore(org.apache.hc.client5.http.auth.CredentialsStore) AuthScope(org.apache.hc.client5.http.auth.AuthScope) BasicAuthCache(org.apache.hc.client5.http.impl.auth.BasicAuthCache) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) CredentialsProvider(org.apache.hc.client5.http.auth.CredentialsProvider)

Aggregations

AuthCache (org.apache.hc.client5.http.auth.AuthCache)3 BasicAuthCache (org.apache.hc.client5.http.impl.auth.BasicAuthCache)3 AuthScope (org.apache.hc.client5.http.auth.AuthScope)2 BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)2 BasicScheme (org.apache.hc.client5.http.impl.auth.BasicScheme)2 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)2 HttpHost (org.apache.hc.core5.http.HttpHost)2 IOException (java.io.IOException)1 UserAuthenticationData (org.apache.commons.vfs2.UserAuthenticationData)1 UserAuthenticator (org.apache.commons.vfs2.UserAuthenticator)1 CredentialsProvider (org.apache.hc.client5.http.auth.CredentialsProvider)1 CredentialsStore (org.apache.hc.client5.http.auth.CredentialsStore)1 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)1 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)1 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)1