Exemplo n.º 1
0
            /// <exception cref="Apache.Http.HttpException"></exception>
            /// <exception cref="System.IO.IOException"></exception>
            public void Process(HttpWebRequest request, HttpContext context)
            {
                AuthState authState = (AuthState)context.GetAttribute(ClientContext.TargetAuthState
                                                                      );

                if (authState.GetAuthScheme() == null)
                {
                    authState.SetAuthScheme(new BasicScheme());
                    authState.SetCredentials(credentials);
                }
            }
Exemplo n.º 2
0
        public virtual bool IsAuthenticationRequested(HttpHost host, HttpResponse response
                                                      , AuthenticationStrategy authStrategy, AuthState authState, HttpContext context)
        {
            if (authStrategy.IsAuthenticationRequested(host, response, context))
            {
                this.log.Debug("Authentication required");
                if (authState.GetState() == AuthProtocolState.Success)
                {
                    authStrategy.AuthFailed(host, authState.GetAuthScheme(), context);
                }
                return(true);
            }
            else
            {
                switch (authState.GetState())
                {
                case AuthProtocolState.Challenged:
                case AuthProtocolState.Handshake:
                {
                    this.log.Debug("Authentication succeeded");
                    authState.SetState(AuthProtocolState.Success);
                    authStrategy.AuthSucceeded(host, authState.GetAuthScheme(), context);
                    break;
                }

                case AuthProtocolState.Success:
                {
                    break;
                }

                default:
                {
                    authState.SetState(AuthProtocolState.Unchallenged);
                    break;
                }
                }
                return(false);
            }
        }
        private static Principal GetAuthPrincipal(AuthState authState)
        {
            AuthScheme scheme = authState.GetAuthScheme();

            if (scheme != null && scheme.IsComplete() && scheme.IsConnectionBased())
            {
                Credentials creds = authState.GetCredentials();
                if (creds != null)
                {
                    return(creds.GetUserPrincipal());
                }
            }
            return(null);
        }
Exemplo n.º 4
0
            /// <exception cref="Apache.Http.HttpException"></exception>
            /// <exception cref="System.IO.IOException"></exception>
            public void Process(HttpWebRequest request, HttpContext context)
            {
                AuthState authState = (AuthState)context.GetAttribute(ClientContext.TargetAuthState
                                                                      );
                CredentialsProvider credsProvider = (CredentialsProvider)context.GetAttribute(ClientContext
                                                                                              .CredsProvider);
                HttpHost targetHost = (HttpHost)context.GetAttribute(ExecutionContext.HttpTargetHost
                                                                     );

                if (authState.GetAuthScheme() == null)
                {
                    AuthScope authScope = new AuthScope(targetHost.GetHostName(), targetHost.GetPort(
                                                            ));
                    authState.SetAuthScheme(new BasicScheme());
                    authState.SetCredentials(creds);
                }
            }
Exemplo n.º 5
0
        public virtual bool HandleAuthChallenge(HttpHost host, HttpResponse response, AuthenticationStrategy
                                                authStrategy, AuthState authState, HttpContext context)
        {
            try
            {
                if (this.log.IsDebugEnabled())
                {
                    this.log.Debug(host.ToHostString() + " requested authentication");
                }
                IDictionary <string, Header> challenges = authStrategy.GetChallenges(host, response
                                                                                     , context);
                if (challenges.IsEmpty())
                {
                    this.log.Debug("Response contains no authentication challenges");
                    return(false);
                }
                AuthScheme authScheme = authState.GetAuthScheme();
                switch (authState.GetState())
                {
                case AuthProtocolState.Failure:
                {
                    return(false);
                }

                case AuthProtocolState.Success:
                {
                    authState.Reset();
                    break;
                }

                case AuthProtocolState.Challenged:
                case AuthProtocolState.Handshake:
                {
                    if (authScheme == null)
                    {
                        this.log.Debug("Auth scheme is null");
                        authStrategy.AuthFailed(host, null, context);
                        authState.Reset();
                        authState.SetState(AuthProtocolState.Failure);
                        return(false);
                    }
                    goto case AuthProtocolState.Unchallenged;
                }

                case AuthProtocolState.Unchallenged:
                {
                    if (authScheme != null)
                    {
                        string id        = authScheme.GetSchemeName();
                        Header challenge = challenges.Get(id.ToLower(CultureInfo.InvariantCulture));
                        if (challenge != null)
                        {
                            this.log.Debug("Authorization challenge processed");
                            authScheme.ProcessChallenge(challenge);
                            if (authScheme.IsComplete())
                            {
                                this.log.Debug("Authentication failed");
                                authStrategy.AuthFailed(host, authState.GetAuthScheme(), context);
                                authState.Reset();
                                authState.SetState(AuthProtocolState.Failure);
                                return(false);
                            }
                            else
                            {
                                authState.SetState(AuthProtocolState.Handshake);
                                return(true);
                            }
                        }
                        else
                        {
                            authState.Reset();
                        }
                    }
                }
                }
                // Retry authentication with a different scheme
                Queue <AuthOption> authOptions = authStrategy.Select(challenges, host, response, context
                                                                     );
                if (authOptions != null && !authOptions.IsEmpty())
                {
                    if (this.log.IsDebugEnabled())
                    {
                        this.log.Debug("Selected authentication options: " + authOptions);
                    }
                    authState.SetState(AuthProtocolState.Challenged);
                    authState.Update(authOptions);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (MalformedChallengeException ex)
            {
                if (this.log.IsWarnEnabled())
                {
                    this.log.Warn("Malformed challenge: " + ex.Message);
                }
                authState.Reset();
                return(false);
            }
        }
Exemplo n.º 6
0
        /// <exception cref="Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void GenerateAuthResponse(IHttpRequest request, AuthState authState
                                                 , HttpContext context)
        {
            AuthScheme  authScheme = authState.GetAuthScheme();
            Credentials creds      = authState.GetCredentials();

            switch (authState.GetState())
            {
            case AuthProtocolState.Failure:
            {
                return;
            }

            case AuthProtocolState.Success:
            {
                EnsureAuthScheme(authScheme);
                if (authScheme.IsConnectionBased())
                {
                    return;
                }
                break;
            }

            case AuthProtocolState.Challenged:
            {
                Queue <AuthOption> authOptions = authState.GetAuthOptions();
                if (authOptions != null)
                {
                    while (!authOptions.IsEmpty())
                    {
                        AuthOption authOption = authOptions.Remove();
                        authScheme = authOption.GetAuthScheme();
                        creds      = authOption.GetCredentials();
                        authState.Update(authScheme, creds);
                        if (this.log.IsDebugEnabled())
                        {
                            this.log.Debug("Generating response to an authentication challenge using " + authScheme
                                           .GetSchemeName() + " scheme");
                        }
                        try
                        {
                            Header header = DoAuth(authScheme, creds, request, context);
                            request.AddHeader(header);
                            break;
                        }
                        catch (AuthenticationException ex)
                        {
                            if (this.log.IsWarnEnabled())
                            {
                                this.log.Warn(authScheme + " authentication error: " + ex.Message);
                            }
                        }
                    }
                    return;
                }
                else
                {
                    EnsureAuthScheme(authScheme);
                }
            }
            }
            if (authScheme != null)
            {
                try
                {
                    Header header = DoAuth(authScheme, creds, request, context);
                    request.AddHeader(header);
                }
                catch (AuthenticationException ex)
                {
                    if (this.log.IsErrorEnabled())
                    {
                        this.log.Error(authScheme + " authentication error: " + ex.Message);
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Apache.Http.HttpException"></exception>
        public virtual CloseableHttpResponse Execute(HttpRoute route, HttpRequestWrapper
                                                     request, HttpClientContext context, HttpExecutionAware execAware)
        {
            Args.NotNull(route, "HTTP route");
            Args.NotNull(request, "HTTP request");
            Args.NotNull(context, "HTTP context");
            IList <URI> redirectLocations = context.GetRedirectLocations();

            if (redirectLocations != null)
            {
                redirectLocations.Clear();
            }
            RequestConfig      config         = context.GetRequestConfig();
            int                maxRedirects   = config.GetMaxRedirects() > 0 ? config.GetMaxRedirects() : 50;
            HttpRoute          currentRoute   = route;
            HttpRequestWrapper currentRequest = request;

            for (int redirectCount = 0; ;)
            {
                CloseableHttpResponse response = requestExecutor.Execute(currentRoute, currentRequest
                                                                         , context, execAware);
                try
                {
                    if (config.IsRedirectsEnabled() && this.redirectStrategy.IsRedirected(currentRequest
                                                                                          , response, context))
                    {
                        if (redirectCount >= maxRedirects)
                        {
                            throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
                        }
                        redirectCount++;
                        IHttpRequest redirect = this.redirectStrategy.GetRedirect(currentRequest, response
                                                                                  , context);
                        if (!redirect.HeaderIterator().HasNext())
                        {
                            IHttpRequest original = request.GetOriginal();
                            redirect.SetHeaders(original.GetAllHeaders());
                        }
                        currentRequest = HttpRequestWrapper.Wrap(redirect);
                        if (currentRequest is HttpEntityEnclosingRequest)
                        {
                            Proxies.EnhanceEntity((HttpEntityEnclosingRequest)currentRequest);
                        }
                        URI      uri       = currentRequest.GetURI();
                        HttpHost newTarget = URIUtils.ExtractHost(uri);
                        if (newTarget == null)
                        {
                            throw new ProtocolException("Redirect URI does not specify a valid host name: " +
                                                        uri);
                        }
                        // Reset virtual host and auth states if redirecting to another host
                        if (!currentRoute.GetTargetHost().Equals(newTarget))
                        {
                            AuthState targetAuthState = context.GetTargetAuthState();
                            if (targetAuthState != null)
                            {
                                this.log.Debug("Resetting target auth state");
                                targetAuthState.Reset();
                            }
                            AuthState proxyAuthState = context.GetProxyAuthState();
                            if (proxyAuthState != null)
                            {
                                AuthScheme authScheme = proxyAuthState.GetAuthScheme();
                                if (authScheme != null && authScheme.IsConnectionBased())
                                {
                                    this.log.Debug("Resetting proxy auth state");
                                    proxyAuthState.Reset();
                                }
                            }
                        }
                        currentRoute = this.routePlanner.DetermineRoute(newTarget, currentRequest, context
                                                                        );
                        if (this.log.IsDebugEnabled())
                        {
                            this.log.Debug("Redirecting to '" + uri + "' via " + currentRoute);
                        }
                        EntityUtils.Consume(response.GetEntity());
                        response.Close();
                    }
                    else
                    {
                        return(response);
                    }
                }
                catch (RuntimeException ex)
                {
                    response.Close();
                    throw;
                }
                catch (IOException ex)
                {
                    response.Close();
                    throw;
                }
                catch (HttpException ex)
                {
                    // Protocol exception related to a direct.
                    // The underlying connection may still be salvaged.
                    try
                    {
                        EntityUtils.Consume(response.GetEntity());
                    }
                    catch (IOException ioex)
                    {
                        this.log.Debug("I/O error while releasing connection", ioex);
                    }
                    finally
                    {
                        response.Close();
                    }
                    throw;
                }
            }
        }