Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 271faf1

Browse files
committed
Refactor IAuthenticationHandler/Result
1 parent f9e0439 commit 271faf1

11 files changed

+425
-78
lines changed

src/Microsoft.AspNetCore.Authentication.Abstractions/AuthenticateResult.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Authentication
1111
/// </summary>
1212
public class AuthenticateResult
1313
{
14-
private AuthenticateResult() { }
14+
protected AuthenticateResult() { }
1515

1616
/// <summary>
1717
/// If a ticket was produced, authenticate was successful.
@@ -21,7 +21,7 @@ private AuthenticateResult() { }
2121
/// <summary>
2222
/// The authentication ticket.
2323
/// </summary>
24-
public AuthenticationTicket Ticket { get; private set; }
24+
public AuthenticationTicket Ticket { get; protected set; }
2525

2626
/// <summary>
2727
/// Gets the claims-principal with authenticated user identities.
@@ -36,18 +36,12 @@ private AuthenticateResult() { }
3636
/// <summary>
3737
/// Holds failure information from the authentication.
3838
/// </summary>
39-
public Exception Failure { get; private set; }
40-
41-
/// <summary>
42-
/// Indicates that stage of authentication was directly handled by user intervention and no
43-
/// further processing should be attempted.
44-
/// </summary>
45-
public bool Handled { get; private set; }
39+
public Exception Failure { get; protected set; }
4640

4741
/// <summary>
4842
/// Indicates that there was no information returned for this authentication scheme.
4943
/// </summary>
50-
public bool Nothing { get; private set; }
44+
public bool None { get; protected set; }
5145

5246
/// <summary>
5347
/// Indicates that authentication was successful.
@@ -63,23 +57,13 @@ public static AuthenticateResult Success(AuthenticationTicket ticket)
6357
return new AuthenticateResult() { Ticket = ticket };
6458
}
6559

66-
/// <summary>
67-
/// Indicates that stage of authentication was directly handled by user intervention and no
68-
/// further processing should be attempted.
69-
/// </summary>
70-
/// <returns>The result.</returns>
71-
public static AuthenticateResult Handle()
72-
{
73-
return new AuthenticateResult() { Handled = true };
74-
}
75-
7660
/// <summary>
7761
/// Indicates that there was no information returned for this authentication scheme.
7862
/// </summary>
7963
/// <returns>The result.</returns>
80-
public static AuthenticateResult None()
64+
public static AuthenticateResult NoResult()
8165
{
82-
return new AuthenticateResult() { Nothing = true };
66+
return new AuthenticateResult() { None = true };
8367
}
8468

8569
/// <summary>

src/Microsoft.AspNetCore.Authentication.Abstractions/AuthenticationHttpContextExtensions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static Task ForbidAsync(this HttpContext context, string scheme) =>
7676
context.ForbidAsync(scheme, properties: null);
7777

7878
/// <summary>
79-
/// Extension method for Forbid.
79+
/// Extension method for Forbid using the <see cref="AuthenticationOptions.DefaultForbidScheme"/> scheme..
8080
/// </summary>
8181
/// <param name="context">The <see cref="HttpContext"/> context.</param>
8282
/// <returns>The task.</returns>
@@ -142,6 +142,21 @@ public static Task SignInAsync(this HttpContext context, ClaimsPrincipal princip
142142
public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal, AuthenticationProperties properties) =>
143143
context.RequestServices.GetRequiredService<IAuthenticationService>().SignInAsync(context, scheme, principal, properties);
144144

145+
/// <summary>
146+
/// Extension method for SignOut using the <see cref="AuthenticationOptions.DefaultSignOutScheme"/>.
147+
/// </summary>
148+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
149+
/// <returns>The task.</returns>
150+
public static Task SignOutAsync(this HttpContext context) => context.SignOutAsync(scheme: null, properties: null);
151+
152+
/// <summary>
153+
/// Extension method for SignOut using the <see cref="AuthenticationOptions.DefaultSignOutScheme"/>.
154+
/// </summary>
155+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
156+
/// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param>
157+
/// <returns>The task.</returns>
158+
public static Task SignOutAsync(this HttpContext context, AuthenticationProperties properties) => context.SignOutAsync(scheme: null, properties: properties);
159+
145160
/// <summary>
146161
/// Extension method for SignOut.
147162
/// </summary>

src/Microsoft.AspNetCore.Authentication.Abstractions/IAuthenticationHandler.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System.Security.Claims;
54
using System.Threading.Tasks;
65
using Microsoft.AspNetCore.Http;
76

@@ -39,20 +38,5 @@ public interface IAuthenticationHandler
3938
/// <param name="properties">The <see cref="AuthenticationProperties"/> that contains the extra meta-data arriving with the authentication.</param>
4039
/// <returns>A task.</returns>
4140
Task ForbidAsync(AuthenticationProperties properties);
42-
43-
/// <summary>
44-
/// Handle sign in.
45-
/// </summary>
46-
/// <param name="user">The <see cref="ClaimsPrincipal"/> user.</param>
47-
/// <param name="properties">The <see cref="AuthenticationProperties"/> that contains the extra meta-data arriving with the authentication.</param>
48-
/// <returns>A task.</returns>
49-
Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties);
50-
51-
/// <summary>
52-
/// Signout behavior.
53-
/// </summary>
54-
/// <param name="properties">The <see cref="AuthenticationProperties"/> that contains the extra meta-data arriving with the authentication.</param>
55-
/// <returns>A task.</returns>
56-
Task SignOutAsync(AuthenticationProperties properties);
5741
}
5842
}

src/Microsoft.AspNetCore.Authentication.Abstractions/IAuthenticationRequestHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace Microsoft.AspNetCore.Authentication
1010
/// </summary>
1111
public interface IAuthenticationRequestHandler : IAuthenticationHandler
1212
{
13-
1413
/// <summary>
1514
/// Returns true if request processing should stop.
1615
/// </summary>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Security.Claims;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.AspNetCore.Authentication
8+
{
9+
/// <summary>
10+
/// Used to determine if a handler supports SignIn.
11+
/// </summary>
12+
public interface IAuthenticationSignInHandler : IAuthenticationSignOutHandler
13+
{
14+
/// <summary>
15+
/// Handle sign in.
16+
/// </summary>
17+
/// <param name="user">The <see cref="ClaimsPrincipal"/> user.</param>
18+
/// <param name="properties">The <see cref="AuthenticationProperties"/> that contains the extra meta-data arriving with the authentication.</param>
19+
/// <returns>A task.</returns>
20+
Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties);
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.AspNetCore.Authentication
7+
{
8+
/// <summary>
9+
/// Used to determine if a handler supports SignOut.
10+
/// </summary>
11+
public interface IAuthenticationSignOutHandler : IAuthenticationHandler
12+
{
13+
/// <summary>
14+
/// Signout behavior.
15+
/// </summary>
16+
/// <param name="properties">The <see cref="AuthenticationProperties"/> that contains the extra meta-data arriving with the authentication.</param>
17+
/// <returns>A task.</returns>
18+
Task SignOutAsync(AuthenticationProperties properties);
19+
}
20+
21+
}

src/Microsoft.AspNetCore.Authentication.Abstractions/IClaimsTransformation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ public interface IClaimsTransformation
1818
/// <returns>The transformed principal.</returns>
1919
Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal);
2020
}
21-
}
21+
}

src/Microsoft.AspNetCore.Authentication.Core/AuthenticationSchemeProvider.cs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Reflection;
87
using System.Threading.Tasks;
98
using Microsoft.AspNetCore.Http;
109
using Microsoft.Extensions.Options;
@@ -36,14 +35,16 @@ public AuthenticationSchemeProvider(IOptions<AuthenticationOptions> options)
3635

3736
private IDictionary<string, AuthenticationScheme> _map = new Dictionary<string, AuthenticationScheme>(StringComparer.Ordinal);
3837
private List<AuthenticationScheme> _requestHandlers = new List<AuthenticationScheme>();
38+
private List<AuthenticationScheme> _signOutHandlers = new List<AuthenticationScheme>();
39+
private List<AuthenticationScheme> _signInHandlers = new List<AuthenticationScheme>();
3940

4041
/// <summary>
4142
/// Returns the scheme that will be used by default for <see cref="IAuthenticationService.AuthenticateAsync(HttpContext, string)"/>.
4243
/// This is typically specified via <see cref="AuthenticationOptions.DefaultAuthenticateScheme"/>.
4344
/// Otherwise, if only a single scheme exists, that will be used, if more than one exists, null will be returned.
4445
/// </summary>
4546
/// <returns>The scheme that will be used by default for <see cref="IAuthenticationService.AuthenticateAsync(HttpContext, string)"/>.</returns>
46-
public Task<AuthenticationScheme> GetDefaultAuthenticateSchemeAsync()
47+
public virtual Task<AuthenticationScheme> GetDefaultAuthenticateSchemeAsync()
4748
{
4849
if (_options.DefaultAuthenticateScheme != null)
4950
{
@@ -59,20 +60,16 @@ public Task<AuthenticationScheme> GetDefaultAuthenticateSchemeAsync()
5960
/// <summary>
6061
/// Returns the scheme that will be used by default for <see cref="IAuthenticationService.ChallengeAsync(HttpContext, string, AuthenticationProperties)"/>.
6162
/// This is typically specified via <see cref="AuthenticationOptions.DefaultChallengeScheme"/>.
62-
/// Otherwise, if only a single scheme exists, that will be used, if more than one exists, null will be returned.
63+
/// Otherwise, this will fallback to <see cref="GetDefaultAuthenticateSchemeAsync"/>.
6364
/// </summary>
6465
/// <returns>The scheme that will be used by default for <see cref="IAuthenticationService.ChallengeAsync(HttpContext, string, AuthenticationProperties)"/>.</returns>
65-
public Task<AuthenticationScheme> GetDefaultChallengeSchemeAsync()
66+
public virtual Task<AuthenticationScheme> GetDefaultChallengeSchemeAsync()
6667
{
6768
if (_options.DefaultChallengeScheme != null)
6869
{
6970
return GetSchemeAsync(_options.DefaultChallengeScheme);
7071
}
71-
if (_map.Count == 1)
72-
{
73-
return Task.FromResult(_map.Values.First());
74-
}
75-
return Task.FromResult<AuthenticationScheme>(null);
72+
return GetDefaultAuthenticateSchemeAsync();
7673
}
7774

7875
/// <summary>
@@ -81,7 +78,7 @@ public Task<AuthenticationScheme> GetDefaultChallengeSchemeAsync()
8178
/// Otherwise, this will fallback to <see cref="GetDefaultChallengeSchemeAsync"/> .
8279
/// </summary>
8380
/// <returns>The scheme that will be used by default for <see cref="IAuthenticationService.ForbidAsync(HttpContext, string, AuthenticationProperties)"/>.</returns>
84-
public Task<AuthenticationScheme> GetDefaultForbidSchemeAsync()
81+
public virtual Task<AuthenticationScheme> GetDefaultForbidSchemeAsync()
8582
{
8683
if (_options.DefaultForbidScheme != null)
8784
{
@@ -93,34 +90,40 @@ public Task<AuthenticationScheme> GetDefaultForbidSchemeAsync()
9390
/// <summary>
9491
/// Returns the scheme that will be used by default for <see cref="IAuthenticationService.SignInAsync(HttpContext, string, System.Security.Claims.ClaimsPrincipal, AuthenticationProperties)"/>.
9592
/// This is typically specified via <see cref="AuthenticationOptions.DefaultSignInScheme"/>.
96-
/// Otherwise, if only a single scheme exists, that will be used, if more than one exists, null will be returned.
93+
/// If only a single sign in handler scheme exists, that will be used, if more than one exists,
94+
/// this will fallback to <see cref="GetDefaultAuthenticateSchemeAsync"/>.
9795
/// </summary>
9896
/// <returns>The scheme that will be used by default for <see cref="IAuthenticationService.SignInAsync(HttpContext, string, System.Security.Claims.ClaimsPrincipal, AuthenticationProperties)"/>.</returns>
99-
public Task<AuthenticationScheme> GetDefaultSignInSchemeAsync()
97+
public virtual Task<AuthenticationScheme> GetDefaultSignInSchemeAsync()
10098
{
10199
if (_options.DefaultSignInScheme != null)
102100
{
103101
return GetSchemeAsync(_options.DefaultSignInScheme);
104102
}
105-
if (_map.Count == 1)
103+
if (_signInHandlers.Count == 1)
106104
{
107-
return Task.FromResult(_map.Values.First());
105+
return Task.FromResult(_signInHandlers[0]);
108106
}
109-
return Task.FromResult<AuthenticationScheme>(null);
107+
return GetDefaultAuthenticateSchemeAsync();
110108
}
111109

112110
/// <summary>
113111
/// Returns the scheme that will be used by default for <see cref="IAuthenticationService.SignOutAsync(HttpContext, string, AuthenticationProperties)"/>.
114112
/// This is typically specified via <see cref="AuthenticationOptions.DefaultSignOutScheme"/>.
115-
/// Otherwise, this will fallback to <see cref="GetDefaultSignInSchemeAsync"/> .
113+
/// If only a single sign out handler scheme exists, that will be used, if more than one exists,
114+
/// this will fallback to <see cref="GetDefaultSignInSchemeAsync"/> if that supoorts sign out.
116115
/// </summary>
117116
/// <returns>The scheme that will be used by default for <see cref="IAuthenticationService.SignOutAsync(HttpContext, string, AuthenticationProperties)"/>.</returns>
118-
public Task<AuthenticationScheme> GetDefaultSignOutSchemeAsync()
117+
public virtual Task<AuthenticationScheme> GetDefaultSignOutSchemeAsync()
119118
{
120119
if (_options.DefaultSignOutScheme != null)
121120
{
122121
return GetSchemeAsync(_options.DefaultSignOutScheme);
123122
}
123+
if (_signOutHandlers.Count == 1)
124+
{
125+
return Task.FromResult(_signOutHandlers[0]);
126+
}
124127
return GetDefaultSignInSchemeAsync();
125128
}
126129

@@ -129,7 +132,7 @@ public Task<AuthenticationScheme> GetDefaultSignOutSchemeAsync()
129132
/// </summary>
130133
/// <param name="name">The name of the authenticationScheme.</param>
131134
/// <returns>The scheme or null if not found.</returns>
132-
public Task<AuthenticationScheme> GetSchemeAsync(string name)
135+
public virtual Task<AuthenticationScheme> GetSchemeAsync(string name)
133136
{
134137
if (_map.ContainsKey(name))
135138
{
@@ -142,7 +145,7 @@ public Task<AuthenticationScheme> GetSchemeAsync(string name)
142145
/// Returns the schemes in priority order for request handling.
143146
/// </summary>
144147
/// <returns>The schemes in priority order for request handling</returns>
145-
public Task<IEnumerable<AuthenticationScheme>> GetRequestHandlerSchemesAsync()
148+
public virtual Task<IEnumerable<AuthenticationScheme>> GetRequestHandlerSchemesAsync()
146149
{
147150
return Task.FromResult<IEnumerable<AuthenticationScheme>>(_requestHandlers);
148151
}
@@ -151,7 +154,7 @@ public Task<IEnumerable<AuthenticationScheme>> GetRequestHandlerSchemesAsync()
151154
/// Registers a scheme for use by <see cref="IAuthenticationService"/>.
152155
/// </summary>
153156
/// <param name="scheme">The scheme.</param>
154-
public void AddScheme(AuthenticationScheme scheme)
157+
public virtual void AddScheme(AuthenticationScheme scheme)
155158
{
156159
if (_map.ContainsKey(scheme.Name))
157160
{
@@ -167,6 +170,14 @@ public void AddScheme(AuthenticationScheme scheme)
167170
{
168171
_requestHandlers.Add(scheme);
169172
}
173+
if (typeof(IAuthenticationSignInHandler).IsAssignableFrom(scheme.HandlerType))
174+
{
175+
_signInHandlers.Add(scheme);
176+
}
177+
if (typeof(IAuthenticationSignOutHandler).IsAssignableFrom(scheme.HandlerType))
178+
{
179+
_signOutHandlers.Add(scheme);
180+
}
170181
_map[scheme.Name] = scheme;
171182
}
172183
}
@@ -175,7 +186,7 @@ public void AddScheme(AuthenticationScheme scheme)
175186
/// Removes a scheme, preventing it from being used by <see cref="IAuthenticationService"/>.
176187
/// </summary>
177188
/// <param name="name">The name of the authenticationScheme being removed.</param>
178-
public void RemoveScheme(string name)
189+
public virtual void RemoveScheme(string name)
179190
{
180191
if (!_map.ContainsKey(name))
181192
{
@@ -186,15 +197,15 @@ public void RemoveScheme(string name)
186197
if (_map.ContainsKey(name))
187198
{
188199
var scheme = _map[name];
189-
_requestHandlers.Remove(_requestHandlers.Where(s => s.Name == name).FirstOrDefault());
200+
_requestHandlers.Remove(scheme);
201+
_signInHandlers.Remove(scheme);
202+
_signOutHandlers.Remove(scheme);
190203
_map.Remove(name);
191204
}
192205
}
193206
}
194207

195-
public Task<IEnumerable<AuthenticationScheme>> GetAllSchemesAsync()
196-
{
197-
return Task.FromResult<IEnumerable<AuthenticationScheme>>(_map.Values);
198-
}
208+
public virtual Task<IEnumerable<AuthenticationScheme>> GetAllSchemesAsync()
209+
=> Task.FromResult<IEnumerable<AuthenticationScheme>>(_map.Values);
199210
}
200211
}

0 commit comments

Comments
 (0)