Search in sources :

Example 1 with AuthScheme

use of org.apache.http.auth.AuthScheme 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 2 with AuthScheme

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

the class AbstractAuthenticationHandler method selectScheme.

public AuthScheme selectScheme(final Map<String, Header> challenges, final HttpResponse response, final HttpContext context) throws AuthenticationException {
    AuthSchemeRegistry registry = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    if (registry == null) {
        throw new IllegalStateException("AuthScheme registry not set in HTTP context");
    }
    List<?> authPrefs = (List<?>) context.getAttribute(ClientContext.AUTH_SCHEME_PREF);
    if (authPrefs == null) {
        authPrefs = getAuthPreferences();
    }
    if (this.log.isDebugEnabled()) {
        this.log.debug("Authentication schemes in the order of preference: " + authPrefs);
    }
    AuthScheme authScheme = null;
    for (int i = 0; i < authPrefs.size(); i++) {
        String id = (String) authPrefs.get(i);
        Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
        if (challenge != null) {
            if (this.log.isDebugEnabled()) {
                this.log.debug(id + " authentication scheme selected");
            }
            try {
                authScheme = registry.getAuthScheme(id, response.getParams());
                break;
            } catch (IllegalStateException e) {
                if (this.log.isWarnEnabled()) {
                    this.log.warn("Authentication scheme " + id + " not supported");
                // Try again
                }
            }
        } else {
            if (this.log.isDebugEnabled()) {
                this.log.debug("Challenge for " + id + " authentication scheme not available");
            // Try again
            }
        }
    }
    if (authScheme == null) {
        // If none selected, something is wrong
        throw new AuthenticationException("Unable to respond to any of these challenges: " + challenges);
    }
    return authScheme;
}
Also used : Header(org.apache.http.Header) FormattedHeader(org.apache.http.FormattedHeader) AuthenticationException(org.apache.http.auth.AuthenticationException) AuthSchemeRegistry(org.apache.http.auth.AuthSchemeRegistry) List(java.util.List) AuthScheme(org.apache.http.auth.AuthScheme)

Example 3 with AuthScheme

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

the class DefaultRequestDirector method updateAuthState.

private void updateAuthState(final AuthState authState, final HttpHost host, final CredentialsProvider credsProvider) {
    if (!authState.isValid()) {
        return;
    }
    String hostname = host.getHostName();
    int port = host.getPort();
    if (port < 0) {
        Scheme scheme = connManager.getSchemeRegistry().getScheme(host);
        port = scheme.getDefaultPort();
    }
    AuthScheme authScheme = authState.getAuthScheme();
    AuthScope authScope = new AuthScope(hostname, port, authScheme.getRealm(), authScheme.getSchemeName());
    if (this.log.isDebugEnabled()) {
        this.log.debug("Authentication scope: " + authScope);
    }
    Credentials creds = authState.getCredentials();
    if (creds == null) {
        creds = credsProvider.getCredentials(authScope);
        if (this.log.isDebugEnabled()) {
            if (creds != null) {
                this.log.debug("Found credentials");
            } else {
                this.log.debug("Credentials not found");
            }
        }
    } else {
        if (authScheme.isComplete()) {
            this.log.debug("Authentication failed");
            creds = null;
        }
    }
    authState.setAuthScope(authScope);
    authState.setCredentials(creds);
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) AuthScheme(org.apache.http.auth.AuthScheme) AuthScope(org.apache.http.auth.AuthScope) Credentials(org.apache.http.auth.Credentials) AuthScheme(org.apache.http.auth.AuthScheme)

Example 4 with AuthScheme

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

the class DefaultRequestDirector method processChallenges.

// abortConnection
private void processChallenges(final Map<String, Header> challenges, final AuthState authState, final AuthenticationHandler authHandler, final HttpResponse response, final HttpContext context) throws MalformedChallengeException, AuthenticationException {
    AuthScheme authScheme = authState.getAuthScheme();
    if (authScheme == null) {
        // Authentication not attempted before
        authScheme = authHandler.selectScheme(challenges, response, context);
        authState.setAuthScheme(authScheme);
    }
    String id = authScheme.getSchemeName();
    Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
    if (challenge == null) {
        throw new AuthenticationException(id + " authorization challenge expected, but not found");
    }
    authScheme.processChallenge(challenge);
    this.log.debug("Authorization challenge processed");
}
Also used : Header(org.apache.http.Header) AuthenticationException(org.apache.http.auth.AuthenticationException) AuthScheme(org.apache.http.auth.AuthScheme)

Example 5 with AuthScheme

use of org.apache.http.auth.AuthScheme 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)

Aggregations

AuthScheme (org.apache.http.auth.AuthScheme)30 Credentials (org.apache.http.auth.Credentials)20 AuthenticationException (org.apache.http.auth.AuthenticationException)17 AuthScope (org.apache.http.auth.AuthScope)14 Header (org.apache.http.Header)12 AuthState (org.apache.http.auth.AuthState)8 HttpHost (org.apache.http.HttpHost)7 Map (java.util.Map)6 HashMap (java.util.HashMap)5 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 Authentication (org.codelibs.fess.crawler.client.http.Authentication)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 HttpEntity (org.apache.http.HttpEntity)4 HttpException (org.apache.http.HttpException)4 HttpRequest (org.apache.http.HttpRequest)4 HttpResponse (org.apache.http.HttpResponse)4 NTCredentials (org.apache.http.auth.NTCredentials)4 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)4 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)4