Skip to content

Commit c69c289

Browse files
committed
Simplify Challenge flow
1 parent e818783 commit c69c289

File tree

4 files changed

+21
-42
lines changed

4 files changed

+21
-42
lines changed

src/Microsoft.AspNet.Http.Core/Authentication/ChallengeContext.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@
44
using System;
55
using System.Collections.Generic;
66
using Microsoft.AspNet.Http.Authentication;
7-
using Microsoft.Framework.Internal;
87

98
namespace Microsoft.AspNet.Http.Core.Authentication
109
{
1110
public class ChallengeContext : IChallengeContext
1211
{
13-
private List<string> _accepted;
12+
private bool _accepted;
1413

15-
public ChallengeContext([NotNull] IEnumerable<string> authenticationSchemes, IDictionary<string, string> properties)
14+
public ChallengeContext(string authenticationScheme, IDictionary<string, string> properties)
1615
{
17-
AuthenticationSchemes = authenticationSchemes;
16+
AuthenticationScheme = authenticationScheme;
1817
Properties = properties ?? new Dictionary<string, string>(StringComparer.Ordinal);
19-
_accepted = new List<string>();
18+
19+
// The default Challenge with no scheme is always accepted
20+
_accepted = string.IsNullOrEmpty(authenticationScheme);
2021
}
2122

22-
public IEnumerable<string> AuthenticationSchemes { get; private set; }
23+
public string AuthenticationScheme { get; private set; }
2324

2425
public IDictionary<string, string> Properties { get; private set; }
2526

26-
public IEnumerable<string> Accepted
27+
public bool Accepted
2728
{
2829
get { return _accepted; }
2930
}
3031

31-
public void Accept(string authenticationType, IDictionary<string, object> description)
32+
public void Accept()
3233
{
33-
_accepted.Add(authenticationType);
34+
_accepted = true;
3435
}
3536
}
3637
}

src/Microsoft.AspNet.Http.Core/DefaultHttpResponse.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,20 @@ public override void Redirect(string location, bool permanent)
130130
Headers.Set(HeaderNames.Location, location);
131131
}
132132

133-
public override void Challenge(AuthenticationProperties properties, [NotNull] IEnumerable<string> authenticationSchemes)
133+
public override void Challenge(AuthenticationProperties properties, string authenticationScheme)
134134
{
135135
HttpResponseFeature.StatusCode = 401;
136136
var handler = HttpAuthenticationFeature.Handler;
137137

138-
var challengeContext = new ChallengeContext(authenticationSchemes, properties == null ? null : properties.Dictionary);
138+
var challengeContext = new ChallengeContext(authenticationScheme, properties == null ? null : properties.Dictionary);
139139
if (handler != null)
140140
{
141141
handler.Challenge(challengeContext);
142142
}
143143

144-
// Verify all types ack'd
145-
IEnumerable<string> leftovers = authenticationSchemes.Except(challengeContext.Accepted);
146-
if (leftovers.Any())
144+
if (!challengeContext.Accepted)
147145
{
148-
throw new InvalidOperationException("The following authentication types were not accepted: " + string.Join(", ", leftovers));
146+
throw new InvalidOperationException("The following authentication type was not accepted: " + authenticationScheme);
149147
}
150148
}
151149

src/Microsoft.AspNet.Http.Interfaces/Authentication/IChallengeContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Microsoft.AspNet.Http.Authentication
77
{
88
public interface IChallengeContext
99
{
10-
IEnumerable<string> AuthenticationSchemes {get;}
11-
IDictionary<string,string> Properties {get;}
10+
string AuthenticationScheme { get; }
11+
IDictionary<string, string> Properties { get; }
1212

13-
void Accept(string authenticationType, IDictionary<string,object> description);
13+
void Accept();
1414
}
1515
}

src/Microsoft.AspNet.Http/HttpResponse.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,20 @@ public virtual void Redirect(string location)
3838

3939
public virtual void Challenge()
4040
{
41-
Challenge(new string[0]);
41+
Challenge(properties: null, authenticationScheme: null);
4242
}
4343

4444
public virtual void Challenge(AuthenticationProperties properties)
4545
{
46-
Challenge(properties, new string[0]);
46+
Challenge(properties, "");
4747
}
4848

4949
public virtual void Challenge(string authenticationScheme)
5050
{
51-
Challenge(new[] { authenticationScheme });
51+
Challenge(properties: null, authenticationScheme: authenticationScheme);
5252
}
5353

54-
public virtual void Challenge(AuthenticationProperties properties, string authenticationScheme)
55-
{
56-
Challenge(properties, new[] { authenticationScheme });
57-
}
58-
59-
public virtual void Challenge(params string[] authenticationSchemes)
60-
{
61-
Challenge((IEnumerable<string>)authenticationSchemes);
62-
}
63-
64-
public virtual void Challenge(IEnumerable<string> authenticationSchemes)
65-
{
66-
Challenge(properties: null, authenticationSchemes: authenticationSchemes);
67-
}
68-
69-
public virtual void Challenge(AuthenticationProperties properties, params string[] authenticationSchemes)
70-
{
71-
Challenge(properties, (IEnumerable<string>)authenticationSchemes);
72-
}
73-
74-
public abstract void Challenge(AuthenticationProperties properties, IEnumerable<string> authenticationSchemes);
54+
public abstract void Challenge(AuthenticationProperties properties, string authenticationScheme);
7555

7656
public abstract void SignIn(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties = null);
7757

0 commit comments

Comments
 (0)