Search in sources :

Example 31 with Credentials

use of org.apache.http.auth.Credentials in project platform_external_apache-http by android.

the class BasicCredentialsProvider method matchCredentials.

/**
     * Find matching {@link Credentials credentials} for the given authentication scope.
     *
     * @param map the credentials hash map
     * @param authscope the {@link AuthScope authentication scope}
     * @return the credentials 
     * 
     */
private static Credentials matchCredentials(final HashMap<AuthScope, Credentials> map, final AuthScope authscope) {
    // see if we get a direct hit
    Credentials creds = map.get(authscope);
    if (creds == null) {
        // Nope.
        // Do a full scan
        int bestMatchFactor = -1;
        AuthScope bestMatch = null;
        for (AuthScope current : map.keySet()) {
            int factor = authscope.match(current);
            if (factor > bestMatchFactor) {
                bestMatchFactor = factor;
                bestMatch = current;
            }
        }
        if (bestMatch != null) {
            creds = map.get(bestMatch);
        }
    }
    return creds;
}
Also used : AuthScope(org.apache.http.auth.AuthScope) Credentials(org.apache.http.auth.Credentials)

Example 32 with Credentials

use of org.apache.http.auth.Credentials in project maven-plugins by apache.

the class JavadocUtil method createHttpClient.

/**
     * Creates a new {@code HttpClient} instance.
     *
     * @param settings The settings to use for setting up the client or {@code null}.
     * @param url The {@code URL} to use for setting up the client or {@code null}.
     *
     * @return A new {@code HttpClient} instance.
     *
     * @see #DEFAULT_TIMEOUT
     * @since 2.8
     */
private static HttpClient createHttpClient(Settings settings, URL url) {
    DefaultHttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
    httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_TIMEOUT);
    httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_TIMEOUT);
    httpClient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    // Some web servers don't allow the default user-agent sent by httpClient
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    if (settings != null && settings.getActiveProxy() != null) {
        Proxy activeProxy = settings.getActiveProxy();
        ProxyInfo proxyInfo = new ProxyInfo();
        proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts());
        if (StringUtils.isNotEmpty(activeProxy.getHost()) && (url == null || !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost()))) {
            HttpHost proxy = new HttpHost(activeProxy.getHost(), activeProxy.getPort());
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) {
                Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(), activeProxy.getPassword());
                httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
            }
        }
    }
    return httpClient;
}
Also used : ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) Proxy(org.apache.maven.settings.Proxy) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 33 with Credentials

use of org.apache.http.auth.Credentials in project jena by apache.

the class TestFusekiTestAuth method testServer_auth.

@Test
public void testServer_auth() {
    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    Credentials credentials = new UsernamePasswordCredentials(USER, PASSWORD);
    credsProvider.setCredentials(AuthScope.ANY, credentials);
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try (TypedInputStream in = HttpOp.execHttpGet(FusekiTestAuth.urlDataset(), "*/*", client, null)) {
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 34 with Credentials

use of org.apache.http.auth.Credentials in project jmeter by apache.

the class HTTPHC4Impl method setConnectionAuthorization.

/**
     * Setup credentials for url AuthScope but keeps Proxy AuthScope credentials
     * @param client HttpClient
     * @param url URL
     * @param authManager {@link AuthManager}
     * @param key key
     */
private void setConnectionAuthorization(CloseableHttpClient client, URL url, AuthManager authManager, HttpClientKey key) {
    CredentialsProvider credentialsProvider = ((AbstractHttpClient) client).getCredentialsProvider();
    if (authManager != null) {
        if (authManager.hasAuthForURL(url)) {
            authManager.setupCredentials(client, url, credentialsProvider, LOCALHOST);
        } else {
            credentialsProvider.clear();
        }
    } else {
        Credentials credentials = null;
        AuthScope authScope = null;
        if (key.hasProxy && !StringUtils.isEmpty(key.proxyUser)) {
            authScope = new AuthScope(key.proxyHost, key.proxyPort);
            credentials = credentialsProvider.getCredentials(authScope);
        }
        credentialsProvider.clear();
        if (credentials != null) {
            credentialsProvider.setCredentials(authScope, credentials);
        }
    }
}
Also used : AbstractHttpClient(org.apache.http.impl.client.AbstractHttpClient) AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) NTCredentials(org.apache.http.auth.NTCredentials)

Example 35 with Credentials

use of org.apache.http.auth.Credentials in project lucene-solr by apache.

the class Krb5HttpClientBuilder method getBuilder.

public SolrHttpClientBuilder getBuilder(SolrHttpClientBuilder builder) {
    if (System.getProperty(LOGIN_CONFIG_PROP) != null) {
        String configValue = System.getProperty(LOGIN_CONFIG_PROP);
        if (configValue != null) {
            logger.info("Setting up SPNego auth with config: " + configValue);
            final String useSubjectCredsProp = "javax.security.auth.useSubjectCredsOnly";
            String useSubjectCredsVal = System.getProperty(useSubjectCredsProp);
            // authentication mechanism can load the credentials from the JAAS configuration.
            if (useSubjectCredsVal == null) {
                System.setProperty(useSubjectCredsProp, "false");
            } else if (!useSubjectCredsVal.toLowerCase(Locale.ROOT).equals("false")) {
                // Don't overwrite the prop value if it's already been written to something else,
                // but log because it is likely the Credentials won't be loaded correctly.
                logger.warn("System Property: " + useSubjectCredsProp + " set to: " + useSubjectCredsVal + " not false.  SPNego authentication may not be successful.");
            }
            javax.security.auth.login.Configuration.setConfiguration(jaasConfig);
            //Enable only SPNEGO authentication scheme.
            builder.setAuthSchemeRegistryProvider(() -> {
                Lookup<AuthSchemeProvider> authProviders = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false)).build();
                return authProviders;
            });
            // Get the credentials from the JAAS configuration rather than here
            Credentials useJaasCreds = new Credentials() {

                public String getPassword() {
                    return null;
                }

                public Principal getUserPrincipal() {
                    return null;
                }
            };
            HttpClientUtil.setCookiePolicy(SolrPortAwareCookieSpecFactory.POLICY_NAME);
            builder.setCookieSpecRegistryProvider(() -> {
                SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory();
                Lookup<CookieSpecProvider> cookieRegistry = RegistryBuilder.<CookieSpecProvider>create().register(SolrPortAwareCookieSpecFactory.POLICY_NAME, cookieFactory).build();
                return cookieRegistry;
            });
            builder.setDefaultCredentialsProvider(() -> {
                CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(AuthScope.ANY, useJaasCreds);
                return credentialsProvider;
            });
            HttpClientUtil.addRequestInterceptor(bufferedEntityInterceptor);
        }
    } else {
        logger.warn("{} is configured without specifying system property '{}'", getClass().getName(), LOGIN_CONFIG_PROP);
    }
    return builder;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Aggregations

Credentials (org.apache.http.auth.Credentials)42 AuthScope (org.apache.http.auth.AuthScope)25 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)16 CredentialsProvider (org.apache.http.client.CredentialsProvider)16 AuthScheme (org.apache.http.auth.AuthScheme)15 AuthState (org.apache.http.auth.AuthState)11 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)10 AuthenticationException (org.apache.http.auth.AuthenticationException)9 HttpHost (org.apache.http.HttpHost)8 Test (org.junit.Test)6 NTCredentials (org.apache.http.auth.NTCredentials)5 IOException (java.io.IOException)4 Header (org.apache.http.Header)4 HttpException (org.apache.http.HttpException)4 HttpRequest (org.apache.http.HttpRequest)4 HttpResponse (org.apache.http.HttpResponse)4 Scheme (org.apache.http.conn.scheme.Scheme)4 BasicScheme (org.apache.http.impl.auth.BasicScheme)4 HttpEntity (org.apache.http.HttpEntity)3 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)3