Exemple #1
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);
                    }
                }
            }
        }