Search in sources :

Example 46 with CredentialsProvider

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project wildfly-swarm by wildfly-swarm.

the class SimpleHttp method getUrlContents.

protected Response getUrlContents(String theUrl, boolean useAuth, boolean followRedirects) {
    StringBuilder content = new StringBuilder();
    int code;
    try {
        CredentialsProvider provider = new BasicCredentialsProvider();
        HttpClientBuilder builder = HttpClientBuilder.create();
        if (!followRedirects) {
            builder.disableRedirectHandling();
        }
        if (useAuth) {
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "password");
            provider.setCredentials(AuthScope.ANY, credentials);
            builder.setDefaultCredentialsProvider(provider);
        }
        HttpClient client = builder.build();
        HttpResponse response = client.execute(new HttpGet(theUrl));
        code = response.getStatusLine().getStatusCode();
        if (null == response.getEntity()) {
            throw new RuntimeException("No response content present");
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            content.append(line + "\n");
        }
        bufferedReader.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return new Response(code, content.toString());
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpResponse(org.apache.http.HttpResponse) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader)

Example 47 with CredentialsProvider

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project epp.mpc by eclipse.

the class CacheCredentialsAuthenticationStrategy method cacheCredentials.

private void cacheCredentials(HttpHost authhost, AuthScheme authScheme, HttpContext context) {
    Credentials credentials = getCredentials(context);
    if (credentials != null) {
        CredentialsProvider credentialsCache = getCredentialsCache(context);
        if (credentialsCache != null) {
            AuthScope scope = createAuthScope(authhost, authScheme);
            credentialsCache.setCredentials(scope, credentials);
        }
    }
}
Also used : AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 48 with CredentialsProvider

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project epp.mpc by eclipse.

the class HttpClientFactory method createCredentialsProvider.

private static CredentialsProvider createCredentialsProvider(HttpClientBuilder clientBuilder) {
    // TODO we should handle configured proxy passwords and dialogs to prompt for unknown credentials on our own...
    CredentialsProvider credentialsProvider = new SystemCredentialsProvider();
    credentialsProvider = customizeCredentialsProvider(credentialsProvider);
    final CacheCredentialsProvider cacheProvider = new CacheCredentialsProvider();
    credentialsProvider = new ChainedCredentialsProvider(cacheProvider, credentialsProvider);
    credentialsProvider = new SynchronizedCredentialsProvider(credentialsProvider);
    clientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {

        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            context.setAttribute(CacheCredentialsAuthenticationStrategy.CREDENTIALS_CACHE_ATTRIBUTE, cacheProvider);
        }
    });
    return credentialsProvider;
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpContext(org.apache.http.protocol.HttpContext) HttpException(org.apache.http.HttpException) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException)

Example 49 with CredentialsProvider

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project stdlib by petergeneric.

the class PreemptiveBearerAuthInterceptor 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 BearerAuthSchemeProvider().create(context), credentials);
    }
}
Also used : 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 50 with CredentialsProvider

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project stdlib by petergeneric.

the class ResteasyClientFactoryImpl method createHttpClientCustomiser.

/**
 * N.B. This method signature may change in the future to add new parameters
 *
 * @param fastFail
 * @param authScope
 * @param credentials
 * @param preemptiveAuth
 * @param storeCookies
 * @param customiser
 *
 * @return
 */
public Consumer<HttpClientBuilder> createHttpClientCustomiser(final boolean fastFail, final AuthScope authScope, final Credentials credentials, final boolean preemptiveAuth, final boolean storeCookies, Consumer<HttpClientBuilder> customiser) {
    // Customise timeouts if fast fail mode is enabled
    if (fastFail) {
        customiser = concat(customiser, b -> {
            RequestConfig.Builder requestBuilder = RequestConfig.custom();
            requestBuilder.setConnectTimeout((int) fastFailConnectionTimeout.getMilliseconds()).setSocketTimeout((int) fastFailSocketTimeout.getMilliseconds());
            b.setDefaultRequestConfig(requestBuilder.build());
        });
    }
    // If credentials were supplied then we should set them up
    if (credentials != null) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        if (authScope != null)
            credentialsProvider.setCredentials(authScope, credentials);
        else
            credentialsProvider.setCredentials(AuthScope.ANY, credentials);
        // Set up bearer auth scheme provider if we're using bearer credentials
        if (credentials instanceof BearerCredentials) {
            customiser = concat(customiser, b -> {
                Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register("Bearer", new BearerAuthSchemeProvider()).build();
                b.setDefaultAuthSchemeRegistry(authSchemeRegistry);
            });
        }
        // Set up the credentials customisation
        customiser = concat(customiser, b -> b.setDefaultCredentialsProvider(credentialsProvider));
        if (preemptiveAuth && credentials instanceof BearerCredentials)
            customiser = concat(customiser, b -> b.addInterceptorFirst(new PreemptiveBearerAuthInterceptor()));
        else
            customiser = concat(customiser, b -> b.addInterceptorLast(new PreemptiveBasicAuthInterceptor()));
    }
    // If cookies are enabled then set up a cookie store
    if (storeCookies)
        customiser = concat(customiser, b -> b.setDefaultCookieStore(new BasicCookieStore()));
    return customiser;
}
Also used : AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) RegistryBuilder(org.apache.http.config.RegistryBuilder) Inject(com.google.inject.Inject) Timeout(com.peterphi.std.threading.Timeout) LogReportMessageBodyWriter(com.peterphi.std.guice.common.logging.logreport.jaxrs.LogReportMessageBodyWriter) RequestConfig(org.apache.http.client.config.RequestConfig) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) CommonTypesParamConverterProvider(com.peterphi.std.guice.restclient.converter.CommonTypesParamConverterProvider) ProxySelector(java.net.ProxySelector) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) ShutdownManager(com.peterphi.std.guice.common.shutdown.iface.ShutdownManager) Registry(org.apache.http.config.Registry) Credentials(org.apache.http.auth.Credentials) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) Doc(com.peterphi.std.annotation.Doc) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) StoppableService(com.peterphi.std.guice.common.shutdown.iface.StoppableService) AuthScope(org.apache.http.auth.AuthScope) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) Named(com.google.inject.name.Named) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) CredentialsProvider(org.apache.http.client.CredentialsProvider) Singleton(com.google.inject.Singleton) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) RegistryBuilder(org.apache.http.config.RegistryBuilder) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Registry(org.apache.http.config.Registry)

Aggregations

CredentialsProvider (org.apache.http.client.CredentialsProvider)271 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)223 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)201 AuthScope (org.apache.http.auth.AuthScope)138 HttpHost (org.apache.http.HttpHost)104 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)73 HttpGet (org.apache.http.client.methods.HttpGet)62 BasicScheme (org.apache.http.impl.auth.BasicScheme)49 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)48 HttpResponse (org.apache.http.HttpResponse)45 Test (org.junit.Test)44 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)41 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)40 IOException (java.io.IOException)39 URI (java.net.URI)36 Credentials (org.apache.http.auth.Credentials)36 AuthCache (org.apache.http.client.AuthCache)33 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)33 HttpClient (org.apache.http.client.HttpClient)31 RequestConfig (org.apache.http.client.config.RequestConfig)29