Search in sources :

Example 11 with AuthState

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

the class DefaultUserTokenHandler method getUserToken.

public Object getUserToken(final HttpContext context) {
    Principal userPrincipal = null;
    AuthState targetAuthState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (targetAuthState != null) {
        userPrincipal = getAuthPrincipal(targetAuthState);
        if (userPrincipal == null) {
            AuthState proxyAuthState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
            userPrincipal = getAuthPrincipal(proxyAuthState);
        }
    }
    if (userPrincipal == null) {
        ManagedClientConnection conn = (ManagedClientConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
        if (conn.isOpen()) {
            SSLSession sslsession = conn.getSSLSession();
            if (sslsession != null) {
                userPrincipal = sslsession.getLocalPrincipal();
            }
        }
    }
    return userPrincipal;
}
Also used : ManagedClientConnection(org.apache.http.conn.ManagedClientConnection) AuthState(org.apache.http.auth.AuthState) SSLSession(javax.net.ssl.SSLSession) Principal(java.security.Principal)

Example 12 with AuthState

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

the class DefaultUserTokenHandler method getUserToken.

public Object getUserToken(final HttpContext context) {
    Principal userPrincipal = null;
    AuthState targetAuthState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (targetAuthState != null) {
        userPrincipal = getAuthPrincipal(targetAuthState);
        if (userPrincipal == null) {
            AuthState proxyAuthState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
            userPrincipal = getAuthPrincipal(proxyAuthState);
        }
    }
    if (userPrincipal == null) {
        ManagedClientConnection conn = (ManagedClientConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
        if (conn.isOpen()) {
            SSLSession sslsession = conn.getSSLSession();
            if (sslsession != null) {
                userPrincipal = sslsession.getLocalPrincipal();
            }
        }
    }
    return userPrincipal;
}
Also used : ManagedClientConnection(org.apache.http.conn.ManagedClientConnection) AuthState(org.apache.http.auth.AuthState) SSLSession(javax.net.ssl.SSLSession) Principal(java.security.Principal)

Example 13 with AuthState

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

the class RequestTargetAuthentication 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.WWW_AUTH_RESP)) {
        return;
    }
    // Obtain authentication state
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_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("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 14 with AuthState

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

the class PreemptiveAuth method process.

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    // If no auth scheme available yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
        authState.update(authScheme, creds);
    }
}
Also used : AuthState(org.apache.http.auth.AuthState) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

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