Search in sources :

Example 31 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project stdlib by petergeneric.

the class PreemptiveBasicAuthInterceptor method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    final AuthState state = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    // Try to initialise an auth scheme if one is not already set
    if (state.getAuthScheme() == null) {
        CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
        HttpHost host = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
        final Credentials credentials = credentialsProvider.getCredentials(new AuthScope(host));
        if (credentials == null)
            throw new HttpException("No credentials for preemptive authentication against: " + host);
        else
            state.update(new BasicScheme(), credentials);
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpException(org.apache.http.HttpException) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 32 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project service-proxy by membrane.

the class AssertUtils method getAuthenticatingHttpClient.

private static HttpClient getAuthenticatingHttpClient(String host, int port, String user, String pass) {
    Credentials defaultcreds = new UsernamePasswordCredentials(user, pass);
    BasicCredentialsProvider bcp = new BasicCredentialsProvider();
    bcp.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
    HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {

        public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
            CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
            if (authState.getAuthScheme() == null) {
                AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                Credentials creds = credsProvider.getCredentials(authScope);
                if (creds != null) {
                    authState.update(new BasicScheme(), creds);
                }
            }
        }
    };
    HttpClient hc = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).setDefaultCredentialsProvider(bcp).addInterceptorFirst(preemptiveAuth).build();
    return hc;
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpContext(org.apache.http.protocol.HttpContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpClient(org.apache.http.client.HttpClient) AuthScope(org.apache.http.auth.AuthScope) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 33 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project fess by codelibs.

the class WebAuthentication method getAuthScheme.

private AuthScheme getAuthScheme() {
    final String scheme = getProtocolScheme();
    if (Constants.BASIC.equals(scheme)) {
        return new BasicScheme();
    }
    if (Constants.DIGEST.equals(scheme)) {
        return new DigestScheme();
    }
    if (Constants.NTLM.equals(scheme)) {
        final Properties props = new Properties();
        getWebConfig().getConfigParameterMap(ConfigName.CONFIG).entrySet().stream().filter(e -> e.getKey().startsWith(Config.JCIFS_PREFIX)).forEach(e -> {
            props.setProperty(e.getKey(), e.getValue());
        });
        return new NTLMScheme(new JcifsEngine(props));
    }
    if (Constants.FORM.equals(scheme)) {
        final Map<String, String> parameterMap = ParameterUtil.parse(getParameters());
        return new FormScheme(parameterMap);
    }
    return null;
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) Constants(org.codelibs.fess.Constants) AuthenticationImpl(org.codelibs.fess.crawler.client.http.impl.AuthenticationImpl) WebConfigService(org.codelibs.fess.app.service.WebConfigService) FormScheme(org.codelibs.fess.crawler.client.http.form.FormScheme) Authentication(org.codelibs.fess.crawler.client.http.Authentication) JcifsEngine(org.codelibs.fess.crawler.client.http.ntlm.JcifsEngine) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) ConfigName(org.codelibs.fess.es.config.exentity.CrawlingConfig.ConfigName) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) Map(java.util.Map) Config(org.codelibs.fess.es.config.exentity.CrawlingConfig.Param.Config) AuthScheme(org.apache.http.auth.AuthScheme) ParameterUtil(org.codelibs.fess.util.ParameterUtil) BasicScheme(org.apache.http.impl.auth.BasicScheme) Properties(java.util.Properties) StringUtil(org.codelibs.core.lang.StringUtil) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Logger(org.apache.logging.log4j.Logger) NTLMScheme(org.apache.http.impl.auth.NTLMScheme) ComponentUtil(org.codelibs.fess.util.ComponentUtil) AuthScope(org.apache.http.auth.AuthScope) BsWebAuthentication(org.codelibs.fess.es.config.bsentity.BsWebAuthentication) DigestScheme(org.apache.http.impl.auth.DigestScheme) LogManager(org.apache.logging.log4j.LogManager) BasicScheme(org.apache.http.impl.auth.BasicScheme) NTLMScheme(org.apache.http.impl.auth.NTLMScheme) JcifsEngine(org.codelibs.fess.crawler.client.http.ntlm.JcifsEngine) Properties(java.util.Properties) FormScheme(org.codelibs.fess.crawler.client.http.form.FormScheme)

Example 34 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project frontend-maven-plugin by eirslett.

the class DefaultFileDownloader method makeLocalContext.

private HttpClientContext makeLocalContext(URL requestUrl) {
    // Auth target host
    HttpHost target = new HttpHost(requestUrl.getHost(), requestUrl.getPort(), requestUrl.getProtocol());
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(target, basicAuth);
    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);
    return localContext;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpHost(org.apache.http.HttpHost) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Example 35 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project orientdb by orientechnologies.

the class BaseHttpTest method exec.

protected BaseHttpTest exec() throws IOException {
    final HttpHost targetHost = new HttpHost(getHost(), getPort(), getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost), new UsernamePasswordCredentials(getUserName(), getUserPassword()));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    if (keepAlive != null)
        request.addHeader("Connection", keepAlive ? "Keep-Alive" : "Close");
    if (payload != null && request instanceof HttpEntityEnclosingRequestBase)
        ((HttpEntityEnclosingRequestBase) request).setEntity(payload);
    final CloseableHttpClient httpClient = HttpClients.createDefault();
    // DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(retry, false);
    // context.setAttribute(HttpMethodParams.RETRY_HANDLER, retryhandler);
    response = httpClient.execute(targetHost, request, context);
    return this;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

BasicScheme (org.apache.http.impl.auth.BasicScheme)83 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)58 CredentialsProvider (org.apache.http.client.CredentialsProvider)49 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)48 AuthCache (org.apache.http.client.AuthCache)47 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)47 HttpHost (org.apache.http.HttpHost)45 AuthScope (org.apache.http.auth.AuthScope)39 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)32 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)25 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)24 HttpGet (org.apache.http.client.methods.HttpGet)23 URI (java.net.URI)21 Test (org.junit.Test)18 IOException (java.io.IOException)13 Credentials (org.apache.http.auth.Credentials)13 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)10 HttpResponse (org.apache.http.HttpResponse)9 HttpPost (org.apache.http.client.methods.HttpPost)9 Header (org.apache.http.Header)8