Search in sources :

Example 1 with AuthScope

use of org.apache.hc.client5.http.auth.AuthScope in project ecf by eclipse.

the class HttpClientRetrieveFileTransfer method setupAuthentication.

protected void setupAuthentication(String urlString) throws UnsupportedCallbackException, IOException {
    Credentials credentials = null;
    if (username == null) {
        credentials = getFileRequestCredentials();
    }
    if (credentials != null && username != null) {
        final AuthScope authScope = new AuthScope(getHostFromURL(urlString), getPortFromURL(urlString));
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "retrieve credentials=" + credentials);
        credentialsProvider.setCredentials(authScope, credentials);
    }
}
Also used : AuthScope(org.apache.hc.client5.http.auth.AuthScope) Credentials(org.apache.hc.client5.http.auth.Credentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 2 with AuthScope

use of org.apache.hc.client5.http.auth.AuthScope in project ecf by eclipse.

the class HttpClientProxyCredentialProvider method getCredentials.

@Override
public Credentials getCredentials(AuthScope authscope, HttpContext httpContext) {
    // $NON-NLS-1$
    Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, HttpClientProxyCredentialProvider.class, "getCredentials " + authscope);
    // First check to see whether given authscope matches any authscope
    // already cached.
    Credentials result = super.getCredentials(authscope, httpContext);
    // If we have a match, return credentials
    if (result != null) {
        if ("ntlm".equalsIgnoreCase(authscope.getSchemeName())) {
            // $NON-NLS-1$
            // We might have gotten these from a password prompt, making them
            // UsernamePasswordCredentials...
            Credentials fixed = fixNTCredentials(result);
            if (fixed != result) {
                result = fixed;
                setCredentials(authscope, fixed);
            }
        }
        return result;
    }
    // If we don't have a match, first get ECF proxy, if any
    Proxy proxy = getECFProxy();
    if (proxy == null)
        return null;
    // Make sure that authscope and proxy host and port match
    if (!matchAuthScopeAndProxy(authscope, proxy))
        return null;
    // Then match scheme, and get credentials from proxy (if it's scheme we know about)
    Credentials credentials = null;
    if ("ntlm".equalsIgnoreCase(authscope.getSchemeName())) {
        // $NON-NLS-1$
        credentials = getNTLMCredentials(proxy);
    } else if (// $NON-NLS-1$
    "basic".equalsIgnoreCase(authscope.getSchemeName()) || "digest".equalsIgnoreCase(authscope.getSchemeName())) {
        // $NON-NLS-1$
        final String proxyUsername = proxy.getUsername();
        final String proxyPassword = proxy.getPassword();
        // If credentials present for proxy then we're done
        if (proxyUsername != null) {
            credentials = new UsernamePasswordCredentials(proxyUsername, proxyPassword.toCharArray());
        }
    } else if ("negotiate".equalsIgnoreCase(authscope.getSchemeName())) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "SPNEGO is not supported, if you can contribute support, please do so.");
    } else {
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "Unrecognized authentication scheme.");
    }
    // Put found credentials in cache for next time
    if (credentials != null)
        setCredentials(authscope, credentials);
    return credentials;
}
Also used : Proxy(org.eclipse.ecf.core.util.Proxy) Credentials(org.apache.hc.client5.http.auth.Credentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.hc.client5.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 3 with AuthScope

use of org.apache.hc.client5.http.auth.AuthScope in project ecf by eclipse.

the class HttpClientFileSystemBrowser method setupAuthentication.

protected void setupAuthentication(String urlString) throws UnsupportedCallbackException, IOException {
    Credentials credentials = null;
    if (username == null) {
        credentials = getFileRequestCredentials();
    }
    if (credentials != null && username != null) {
        final AuthScope authScope = new AuthScope(HttpClientRetrieveFileTransfer.getHostFromURL(urlString), HttpClientRetrieveFileTransfer.getPortFromURL(urlString));
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "browse credentials=" + credentials);
        credentialsProvider.setCredentials(authScope, credentials);
    }
}
Also used : AuthScope(org.apache.hc.client5.http.auth.AuthScope) Credentials(org.apache.hc.client5.http.auth.Credentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 4 with AuthScope

use of org.apache.hc.client5.http.auth.AuthScope in project wiremock by wiremock.

the class WireMockTestClient method httpClientWithPreemptiveAuth.

private static CloseableHttpClient httpClientWithPreemptiveAuth(HttpHost target, String username, String password) {
    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials(username, password.toCharArray()));
    return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
}
Also used : BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthScope(org.apache.hc.client5.http.auth.AuthScope) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 5 with AuthScope

use of org.apache.hc.client5.http.auth.AuthScope in project jahia by Jahia.

the class HttpClientService method initHttpClient.

private CloseableHttpClient initHttpClient(HttpClientBuilder builder, String protocol) {
    String host = System.getProperty(protocol + ".proxyHost");
    int port = Integer.getInteger(protocol + ".proxyPort", -1);
    HttpHost proxy = new HttpHost(protocol, host, port);
    builder.setProxy(proxy);
    String key = host + ':' + port;
    BasicCredentialsProvider credsProvider = null;
    String user = System.getProperty(protocol + ".proxyUser");
    if (StringUtils.isNotEmpty(user)) {
        credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(user, System.getProperty(protocol + ".proxyPassword").toCharArray()));
        builder.setDefaultCredentialsProvider(credsProvider);
    }
    if (logger.isInfoEnabled()) {
        logger.info("Initialized HttpClient for {} protocol using proxy {} {} credentials", protocol.toUpperCase(), key, credsProvider != null ? "with" : "without");
    }
    CloseableHttpClient client = builder.build();
    httpClients.put(key, client);
    return client;
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) HttpHost(org.apache.hc.core5.http.HttpHost) AuthScope(org.apache.hc.client5.http.auth.AuthScope) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Aggregations

AuthScope (org.apache.hc.client5.http.auth.AuthScope)16 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)14 BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)13 Credentials (org.apache.hc.client5.http.auth.Credentials)6 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)5 HttpHost (org.apache.hc.core5.http.HttpHost)5 CredentialsStore (org.apache.hc.client5.http.auth.CredentialsStore)3 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)3 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)3 AuthCache (org.apache.hc.client5.http.auth.AuthCache)2 NTCredentials (org.apache.hc.client5.http.auth.NTCredentials)2 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)2 BasicAuthCache (org.apache.hc.client5.http.impl.auth.BasicAuthCache)2 Exceptions.throwUnchecked (com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked)1 LocalNotifier.notifier (com.github.tomakehurst.wiremock.common.LocalNotifier.notifier)1 ProxySettings (com.github.tomakehurst.wiremock.common.ProxySettings)1 NO_PROXY (com.github.tomakehurst.wiremock.common.ProxySettings.NO_PROXY)1 KeyStoreSettings (com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings)1 NO_STORE (com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings.NO_STORE)1 RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)1