Search in sources :

Example 1 with AuthState

use of org.apache.http.auth.AuthState in project camel by apache.

the class PreemptiveAuthInterceptor method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.update(authScheme, creds);
        }
    }
}
Also used : AuthState(org.apache.http.auth.AuthState) HttpException(org.apache.http.HttpException) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) AuthScheme(org.apache.http.auth.AuthScheme)

Example 2 with AuthState

use of org.apache.http.auth.AuthState in project robovm by robovm.

the class RequestProxyAuthentication method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) {
        return;
    }
    // Obtain authentication state
    AuthState authState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
    if (authState == null) {
        return;
    }
    AuthScheme authScheme = authState.getAuthScheme();
    if (authScheme == null) {
        return;
    }
    Credentials creds = authState.getCredentials();
    if (creds == null) {
        this.log.debug("User credentials not available");
        return;
    }
    if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) {
        try {
            request.addHeader(authScheme.authenticate(creds, request));
        } catch (AuthenticationException ex) {
            if (this.log.isErrorEnabled()) {
                this.log.error("Proxy authentication error: " + ex.getMessage());
            }
        }
    }
}
Also used : AuthState(org.apache.http.auth.AuthState) AuthenticationException(org.apache.http.auth.AuthenticationException) Credentials(org.apache.http.auth.Credentials) AuthScheme(org.apache.http.auth.AuthScheme)

Example 3 with AuthState

use of org.apache.http.auth.AuthState in project XobotOS by xamarin.

the class RequestProxyAuthentication method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) {
        return;
    }
    // Obtain authentication state
    AuthState authState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
    if (authState == null) {
        return;
    }
    AuthScheme authScheme = authState.getAuthScheme();
    if (authScheme == null) {
        return;
    }
    Credentials creds = authState.getCredentials();
    if (creds == null) {
        this.log.debug("User credentials not available");
        return;
    }
    if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) {
        try {
            request.addHeader(authScheme.authenticate(creds, request));
        } catch (AuthenticationException ex) {
            if (this.log.isErrorEnabled()) {
                this.log.error("Proxy authentication error: " + ex.getMessage());
            }
        }
    }
}
Also used : AuthState(org.apache.http.auth.AuthState) AuthenticationException(org.apache.http.auth.AuthenticationException) Credentials(org.apache.http.auth.Credentials) AuthScheme(org.apache.http.auth.AuthScheme)

Example 4 with AuthState

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

the class HttpClientWrapper method enablePreemptiveAuth.

protected void enablePreemptiveAuth(final HttpClientBuilder builder) {
    /**
         * Add an HttpRequestInterceptor that will perform preemptive authentication
         * @see http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/authentication.html
         */
    final HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {

        @Override
        public void process(final HttpRequest request, final HttpContext context) throws IOException {
            if (context instanceof HttpClientContext) {
                final HttpClientContext clientContext = (HttpClientContext) context;
                final AuthState authState = clientContext.getTargetAuthState();
                final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
                final HttpHost targetHost = clientContext.getTargetHost();
                // If not authentication scheme has been initialized yet
                if (authState.getAuthScheme() == null) {
                    final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                    // Obtain credentials matching the target host
                    final Credentials creds = credsProvider.getCredentials(authScope);
                    // If found, generate BasicScheme preemptively
                    if (creds != null) {
                        authState.update(new BasicScheme(), creds);
                    }
                }
            } else {
                throw new IllegalArgumentException("Not sure how to handle a non-HttpClientContext context.");
            }
        }
    };
    builder.addInterceptorFirst(preemptiveAuth);
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpContext(org.apache.http.protocol.HttpContext) AuthScope(org.apache.http.auth.AuthScope) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 5 with AuthState

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

the class RequestProxyAuthentication method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) {
        return;
    }
    // Obtain authentication state
    AuthState authState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
    if (authState == null) {
        return;
    }
    AuthScheme authScheme = authState.getAuthScheme();
    if (authScheme == null) {
        return;
    }
    Credentials creds = authState.getCredentials();
    if (creds == null) {
        this.log.debug("User credentials not available");
        return;
    }
    if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) {
        try {
            request.addHeader(authScheme.authenticate(creds, request));
        } catch (AuthenticationException ex) {
            if (this.log.isErrorEnabled()) {
                this.log.error("Proxy authentication error: " + ex.getMessage());
            }
        }
    }
}
Also used : AuthState(org.apache.http.auth.AuthState) AuthenticationException(org.apache.http.auth.AuthenticationException) Credentials(org.apache.http.auth.Credentials) AuthScheme(org.apache.http.auth.AuthScheme)

Aggregations

AuthState (org.apache.http.auth.AuthState)14 Credentials (org.apache.http.auth.Credentials)11 AuthScheme (org.apache.http.auth.AuthScheme)7 AuthenticationException (org.apache.http.auth.AuthenticationException)6 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 Principal (java.security.Principal)3 SSLSession (javax.net.ssl.SSLSession)3 HttpHost (org.apache.http.HttpHost)3 AuthScope (org.apache.http.auth.AuthScope)3 ManagedClientConnection (org.apache.http.conn.ManagedClientConnection)3 BasicScheme (org.apache.http.impl.auth.BasicScheme)3 HttpException (org.apache.http.HttpException)1 HttpRequest (org.apache.http.HttpRequest)1 HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 HttpContext (org.apache.http.protocol.HttpContext)1