private void DoPreemptiveAuth(HttpHost host, AuthScheme authScheme, AuthState authState , CredentialsProvider credsProvider) { string schemeName = authScheme.GetSchemeName(); if (this.log.IsDebugEnabled()) { this.log.Debug("Re-using cached '" + schemeName + "' auth scheme for " + host); } AuthScope authScope = new AuthScope(host, AuthScope.AnyRealm, schemeName); Credentials creds = credsProvider.GetCredentials(authScope); if (creds != null) { if (Sharpen.Runtime.EqualsIgnoreCase("BASIC", authScheme.GetSchemeName())) { authState.SetState(AuthProtocolState.Challenged); } else { authState.SetState(AuthProtocolState.Success); } authState.Update(authScheme, creds); } else { this.log.Debug("No credentials for preemptive authentication"); } }
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); } }
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); } }