Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
0
        /// <exception cref="Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Process(IHttpRequest request, HttpContext context)
        {
            Args.NotNull(request, "HTTP request");
            Args.NotNull(context, "HTTP context");
            HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context
                                                                                          ));
            AuthCache authCache = clientContext.GetAuthCache();

            if (authCache == null)
            {
                this.log.Debug("Auth cache not set in the context");
                return;
            }
            CredentialsProvider credsProvider = clientContext.GetCredentialsProvider();

            if (credsProvider == null)
            {
                this.log.Debug("Credentials provider not set in the context");
                return;
            }
            RouteInfo route  = clientContext.GetHttpRoute();
            HttpHost  target = clientContext.GetTargetHost();

            if (target.GetPort() < 0)
            {
                target = new HttpHost(target.GetHostName(), route.GetTargetHost().GetPort(), target
                                      .GetSchemeName());
            }
            AuthState targetState = clientContext.GetTargetAuthState();

            if (targetState != null && targetState.GetState() == AuthProtocolState.Unchallenged)
            {
                AuthScheme authScheme = authCache.Get(target);
                if (authScheme != null)
                {
                    DoPreemptiveAuth(target, authScheme, targetState, credsProvider);
                }
            }
            HttpHost  proxy      = route.GetProxyHost();
            AuthState proxyState = clientContext.GetProxyAuthState();

            if (proxy != null && proxyState != null && proxyState.GetState() == AuthProtocolState
                .Unchallenged)
            {
                AuthScheme authScheme = authCache.Get(proxy);
                if (authScheme != null)
                {
                    DoPreemptiveAuth(proxy, authScheme, proxyState, credsProvider);
                }
            }
        }
Exemplo n.º 3
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.º 4
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);
                    }
                }
            }
        }