Simplify Challenge flow

This commit is contained in:
Hao Kung 2015-04-15 11:15:40 -07:00
parent e818783ba4
commit c69c289abf
4 changed files with 21 additions and 42 deletions

View File

@ -4,33 +4,34 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.Http.Authentication;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Http.Core.Authentication namespace Microsoft.AspNet.Http.Core.Authentication
{ {
public class ChallengeContext : IChallengeContext public class ChallengeContext : IChallengeContext
{ {
private List<string> _accepted; private bool _accepted;
public ChallengeContext([NotNull] IEnumerable<string> authenticationSchemes, IDictionary<string, string> properties) public ChallengeContext(string authenticationScheme, IDictionary<string, string> properties)
{ {
AuthenticationSchemes = authenticationSchemes; AuthenticationScheme = authenticationScheme;
Properties = properties ?? new Dictionary<string, string>(StringComparer.Ordinal); Properties = properties ?? new Dictionary<string, string>(StringComparer.Ordinal);
_accepted = new List<string>();
// The default Challenge with no scheme is always accepted
_accepted = string.IsNullOrEmpty(authenticationScheme);
} }
public IEnumerable<string> AuthenticationSchemes { get; private set; } public string AuthenticationScheme { get; private set; }
public IDictionary<string, string> Properties { get; private set; } public IDictionary<string, string> Properties { get; private set; }
public IEnumerable<string> Accepted public bool Accepted
{ {
get { return _accepted; } get { return _accepted; }
} }
public void Accept(string authenticationType, IDictionary<string, object> description) public void Accept()
{ {
_accepted.Add(authenticationType); _accepted = true;
} }
} }
} }

View File

@ -130,22 +130,20 @@ namespace Microsoft.AspNet.Http.Core
Headers.Set(HeaderNames.Location, location); Headers.Set(HeaderNames.Location, location);
} }
public override void Challenge(AuthenticationProperties properties, [NotNull] IEnumerable<string> authenticationSchemes) public override void Challenge(AuthenticationProperties properties, string authenticationScheme)
{ {
HttpResponseFeature.StatusCode = 401; HttpResponseFeature.StatusCode = 401;
var handler = HttpAuthenticationFeature.Handler; var handler = HttpAuthenticationFeature.Handler;
var challengeContext = new ChallengeContext(authenticationSchemes, properties == null ? null : properties.Dictionary); var challengeContext = new ChallengeContext(authenticationScheme, properties == null ? null : properties.Dictionary);
if (handler != null) if (handler != null)
{ {
handler.Challenge(challengeContext); handler.Challenge(challengeContext);
} }
// Verify all types ack'd if (!challengeContext.Accepted)
IEnumerable<string> leftovers = authenticationSchemes.Except(challengeContext.Accepted);
if (leftovers.Any())
{ {
throw new InvalidOperationException("The following authentication types were not accepted: " + string.Join(", ", leftovers)); throw new InvalidOperationException("The following authentication type was not accepted: " + authenticationScheme);
} }
} }

View File

@ -7,9 +7,9 @@ namespace Microsoft.AspNet.Http.Authentication
{ {
public interface IChallengeContext public interface IChallengeContext
{ {
IEnumerable<string> AuthenticationSchemes {get;} string AuthenticationScheme { get; }
IDictionary<string,string> Properties {get;} IDictionary<string, string> Properties { get; }
void Accept(string authenticationType, IDictionary<string,object> description); void Accept();
} }
} }

View File

@ -38,40 +38,20 @@ namespace Microsoft.AspNet.Http
public virtual void Challenge() public virtual void Challenge()
{ {
Challenge(new string[0]); Challenge(properties: null, authenticationScheme: null);
} }
public virtual void Challenge(AuthenticationProperties properties) public virtual void Challenge(AuthenticationProperties properties)
{ {
Challenge(properties, new string[0]); Challenge(properties, "");
} }
public virtual void Challenge(string authenticationScheme) public virtual void Challenge(string authenticationScheme)
{ {
Challenge(new[] { authenticationScheme }); Challenge(properties: null, authenticationScheme: authenticationScheme);
} }
public virtual void Challenge(AuthenticationProperties properties, string authenticationScheme) public abstract void Challenge(AuthenticationProperties properties, string authenticationScheme);
{
Challenge(properties, new[] { authenticationScheme });
}
public virtual void Challenge(params string[] authenticationSchemes)
{
Challenge((IEnumerable<string>)authenticationSchemes);
}
public virtual void Challenge(IEnumerable<string> authenticationSchemes)
{
Challenge(properties: null, authenticationSchemes: authenticationSchemes);
}
public virtual void Challenge(AuthenticationProperties properties, params string[] authenticationSchemes)
{
Challenge(properties, (IEnumerable<string>)authenticationSchemes);
}
public abstract void Challenge(AuthenticationProperties properties, IEnumerable<string> authenticationSchemes);
public abstract void SignIn(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties = null); public abstract void SignIn(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties = null);