Search in sources :

Example 16 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project SmartAndroidSource by jaychou2012.

the class PreemtiveAuthorizationHttpRequestInterceptor method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }
    }
}
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) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 17 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project elasticsearch by elastic.

the class RestClient method setHosts.

/**
     * Replaces the hosts that the client communicates with.
     * @see HttpHost
     */
public synchronized void setHosts(HttpHost... hosts) {
    if (hosts == null || hosts.length == 0) {
        throw new IllegalArgumentException("hosts must not be null nor empty");
    }
    Set<HttpHost> httpHosts = new HashSet<>();
    AuthCache authCache = new BasicAuthCache();
    for (HttpHost host : hosts) {
        Objects.requireNonNull(host, "host cannot be null");
        httpHosts.add(host);
        authCache.put(host, new BasicScheme());
    }
    this.hostTuple = new HostTuple<>(Collections.unmodifiableSet(httpHosts), authCache);
    this.blacklist.clear();
}
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) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HashSet(java.util.HashSet)

Example 18 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project ats-framework by Axway.

the class HttpClient method setupAuthentication.

/**
     * Set up authentication for HTTP Basic/HTTP Digest/SPNEGO.
     *
     * @param httpClientBuilder The client builder
     * @return The context
     * @throws HttpException
     */
private HttpClientContext setupAuthentication(HttpClientBuilder httpClientBuilder) throws HttpException {
    HttpClientContext localContext = null;
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password));
    httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    if (authType == AuthType.always) {
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
        // auth cache
        BasicScheme basicAuth = new BasicScheme();
        URL uri = null;
        try {
            uri = new URL(url);
        } catch (MalformedURLException e) {
            throw new HttpException("Exception occurred creating URL from '" + url + "'.", e);
        }
        HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getProtocol());
        authCache.put(target, basicAuth);
        // Add AuthCache to the execution context
        localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);
    } else {
        if (!StringUtils.isNullOrEmpty(kerberosServicePrincipalName)) {
            GssClient gssClient = new GssClient(username, password, kerberosClientKeytab, krb5ConfFile);
            AuthSchemeProvider nsf = new SPNegoSchemeFactory(gssClient, kerberosServicePrincipalName, kerberosServicePrincipalType);
            final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, nsf).build();
            httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
        }
    }
    return localContext;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) MalformedURLException(java.net.MalformedURLException) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) 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) SPNegoSchemeFactory(com.axway.ats.core.gss.spnego.SPNegoSchemeFactory) URL(java.net.URL) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) GssClient(com.axway.ats.core.gss.GssClient) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider)

Example 19 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project opennms by OpenNMS.

the class RestSessionIT method queryUri.

private Header[] queryUri(final String uri, final String header) throws IOException {
    final HttpGet httpGet = new HttpGet(getBaseUrl() + uri);
    final HttpHost targetHost = new HttpHost(httpGet.getURI().getHost(), httpGet.getURI().getPort(), httpGet.getURI().getScheme());
    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("admin", "admin"));
    final AuthCache authCache = new BasicAuthCache();
    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    final HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    final CloseableHttpResponse closeableHttpResponse = HttpClients.createDefault().execute(targetHost, httpGet, context);
    closeableHttpResponse.close();
    return closeableHttpResponse.getHeaders(header);
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) 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)

Example 20 with BasicScheme

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

the class HTTPHC4Impl method executeRequest.

/**
     * Execute request either as is or under PrivilegedAction 
     * if a Subject is available for url
     * @param httpClient the {@link CloseableHttpClient} to be used to execute the httpRequest
     * @param httpRequest the {@link HttpRequest} to be executed
     * @param localContext th {@link HttpContext} to be used for execution
     * @param url the target url (will be used to look up a possible subject for the execution)
     * @return the result of the execution of the httpRequest
     * @throws IOException
     * @throws ClientProtocolException
     */
private CloseableHttpResponse executeRequest(final CloseableHttpClient httpClient, final HttpRequestBase httpRequest, final HttpContext localContext, final URL url) throws IOException, ClientProtocolException {
    AuthManager authManager = getAuthManager();
    if (authManager != null) {
        Subject subject = authManager.getSubjectForUrl(url);
        if (subject != null) {
            try {
                return Subject.doAs(subject, (PrivilegedExceptionAction<CloseableHttpResponse>) () -> httpClient.execute(httpRequest, localContext));
            } catch (PrivilegedActionException e) {
                log.error("Can't execute httpRequest with subject: {}", subject, e);
                throw new RuntimeException("Can't execute httpRequest with subject:" + subject, e);
            }
        }
        if (BASIC_AUTH_PREEMPTIVE) {
            Authorization authorization = authManager.getAuthForURL(url);
            if (authorization != null && Mechanism.BASIC_DIGEST.equals(authorization.getMechanism())) {
                HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.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
                localContext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
            }
        }
    }
    return httpClient.execute(httpRequest, localContext);
}
Also used : Authorization(org.apache.jmeter.protocol.http.control.Authorization) BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthManager(org.apache.jmeter.protocol.http.control.AuthManager) PrivilegedActionException(java.security.PrivilegedActionException) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) Subject(javax.security.auth.Subject)

Aggregations

BasicScheme (org.apache.http.impl.auth.BasicScheme)22 AuthCache (org.apache.http.client.AuthCache)15 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)15 HttpHost (org.apache.http.HttpHost)14 AuthScope (org.apache.http.auth.AuthScope)14 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)13 CredentialsProvider (org.apache.http.client.CredentialsProvider)12 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)12 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)11 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 Credentials (org.apache.http.auth.Credentials)4 HttpGet (org.apache.http.client.methods.HttpGet)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 IOException (java.io.IOException)3 AuthState (org.apache.http.auth.AuthState)3 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)3 URI (java.net.URI)2 URL (java.net.URL)2 Header (org.apache.http.Header)2 HttpContext (org.apache.http.protocol.HttpContext)2