Changes for error handling in Authentication

This commit is contained in:
Hao Kung 2015-10-14 14:44:50 -07:00
parent 0581bcf008
commit d28c6e1dbb
7 changed files with 40 additions and 28 deletions

View File

@ -11,6 +11,11 @@ namespace Microsoft.AspNet.Http.Authentication
{ {
public abstract class AuthenticationManager public abstract class AuthenticationManager
{ {
/// <summary>
/// Constant used to represent the automatic scheme
/// </summary>
public const string AutomaticScheme = "Automatic";
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes(); public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();
public abstract Task AuthenticateAsync(AuthenticateContext context); public abstract Task AuthenticateAsync(AuthenticateContext context);
@ -34,14 +39,14 @@ namespace Microsoft.AspNet.Http.Authentication
public virtual Task ChallengeAsync(AuthenticationProperties properties) public virtual Task ChallengeAsync(AuthenticationProperties properties)
{ {
return ChallengeAsync(authenticationScheme: string.Empty, properties: properties); return ChallengeAsync(authenticationScheme: AutomaticScheme, properties: properties);
} }
public virtual Task ChallengeAsync(string authenticationScheme) public virtual Task ChallengeAsync(string authenticationScheme)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
return ChallengeAsync(authenticationScheme: authenticationScheme, properties: null); return ChallengeAsync(authenticationScheme: authenticationScheme, properties: null);
@ -50,9 +55,9 @@ namespace Microsoft.AspNet.Http.Authentication
// Leave it up to authentication handler to do the right thing for the challenge // Leave it up to authentication handler to do the right thing for the challenge
public virtual Task ChallengeAsync(string authenticationScheme, AuthenticationProperties properties) public virtual Task ChallengeAsync(string authenticationScheme, AuthenticationProperties properties)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
return ChallengeAsync(authenticationScheme, properties, ChallengeBehavior.Automatic); return ChallengeAsync(authenticationScheme, properties, ChallengeBehavior.Automatic);
@ -60,9 +65,9 @@ namespace Microsoft.AspNet.Http.Authentication
public virtual Task SignInAsync(string authenticationScheme, ClaimsPrincipal principal) public virtual Task SignInAsync(string authenticationScheme, ClaimsPrincipal principal)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
if (principal == null) if (principal == null)

View File

@ -11,9 +11,9 @@ namespace Microsoft.AspNet.Http.Features.Authentication
{ {
public AuthenticateContext(string authenticationScheme) public AuthenticateContext(string authenticationScheme)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
AuthenticationScheme = authenticationScheme; AuthenticationScheme = authenticationScheme;
@ -29,6 +29,8 @@ namespace Microsoft.AspNet.Http.Features.Authentication
public IDictionary<string, object> Description { get; private set; } public IDictionary<string, object> Description { get; private set; }
public Exception Error { get; private set; }
public virtual void Authenticated(ClaimsPrincipal principal, IDictionary<string, string> properties, IDictionary<string, object> description) public virtual void Authenticated(ClaimsPrincipal principal, IDictionary<string, string> properties, IDictionary<string, object> description)
{ {
Accepted = true; Accepted = true;
@ -41,5 +43,11 @@ namespace Microsoft.AspNet.Http.Features.Authentication
{ {
Accepted = true; Accepted = true;
} }
public virtual void Failed(Exception error)
{
Error = error;
Accepted = true;
}
} }
} }

View File

@ -15,9 +15,9 @@ namespace Microsoft.AspNet.Http.Features.Authentication
public ChallengeContext(string authenticationScheme, IDictionary<string, string> properties, ChallengeBehavior behavior) public ChallengeContext(string authenticationScheme, IDictionary<string, string> properties, ChallengeBehavior behavior)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
AuthenticationScheme = authenticationScheme; AuthenticationScheme = authenticationScheme;

View File

@ -11,9 +11,9 @@ namespace Microsoft.AspNet.Http.Features.Authentication
{ {
public SignInContext(string authenticationScheme, ClaimsPrincipal principal, IDictionary<string, string> properties) public SignInContext(string authenticationScheme, ClaimsPrincipal principal, IDictionary<string, string> properties)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
if (principal == null) if (principal == null)

View File

@ -10,9 +10,9 @@ namespace Microsoft.AspNet.Http.Features.Authentication
{ {
public SignOutContext(string authenticationScheme, IDictionary<string, string> properties) public SignOutContext(string authenticationScheme, IDictionary<string, string> properties)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
AuthenticationScheme = authenticationScheme; AuthenticationScheme = authenticationScheme;

View File

@ -54,7 +54,6 @@ namespace Microsoft.AspNet.Http.Authentication.Internal
} }
var handler = HttpAuthenticationFeature.Handler; var handler = HttpAuthenticationFeature.Handler;
if (handler != null) if (handler != null)
{ {
await handler.AuthenticateAsync(context); await handler.AuthenticateAsync(context);
@ -62,15 +61,15 @@ namespace Microsoft.AspNet.Http.Authentication.Internal
if (!context.Accepted) if (!context.Accepted)
{ {
throw new InvalidOperationException($"The following authentication scheme was not accepted: {context.AuthenticationScheme}"); throw new InvalidOperationException($"No authentication handler is configured to authenticate for the scheme: {context.AuthenticationScheme}");
} }
} }
public override async Task ChallengeAsync(string authenticationScheme, AuthenticationProperties properties, ChallengeBehavior behavior) public override async Task ChallengeAsync(string authenticationScheme, AuthenticationProperties properties, ChallengeBehavior behavior)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
var handler = HttpAuthenticationFeature.Handler; var handler = HttpAuthenticationFeature.Handler;
@ -83,15 +82,15 @@ namespace Microsoft.AspNet.Http.Authentication.Internal
if (!challengeContext.Accepted) if (!challengeContext.Accepted)
{ {
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}"); throw new InvalidOperationException($"No authentication handler is configured to handle the scheme: {authenticationScheme}");
} }
} }
public override async Task SignInAsync(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties) public override async Task SignInAsync(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
if (principal == null) if (principal == null)
@ -109,15 +108,15 @@ namespace Microsoft.AspNet.Http.Authentication.Internal
if (!signInContext.Accepted) if (!signInContext.Accepted)
{ {
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}"); throw new InvalidOperationException($"No authentication handler is configured to handle the scheme: {authenticationScheme}");
} }
} }
public override async Task SignOutAsync(string authenticationScheme, AuthenticationProperties properties) public override async Task SignOutAsync(string authenticationScheme, AuthenticationProperties properties)
{ {
if (authenticationScheme == null) if (string.IsNullOrEmpty(authenticationScheme))
{ {
throw new ArgumentNullException(nameof(authenticationScheme)); throw new ArgumentException(nameof(authenticationScheme));
} }
var handler = HttpAuthenticationFeature.Handler; var handler = HttpAuthenticationFeature.Handler;
@ -130,7 +129,7 @@ namespace Microsoft.AspNet.Http.Authentication.Internal
if (!signOutContext.Accepted) if (!signOutContext.Accepted)
{ {
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}"); throw new InvalidOperationException($"No authentication handler is configured to handle the scheme: {authenticationScheme}");
} }
} }
} }

View File

@ -12,7 +12,7 @@ using Xunit;
namespace Microsoft.AspNet.Http.Authentication.Internal namespace Microsoft.AspNet.Http.Authentication.Internal
{ {
public class AuthenticationManagerTests public class DefaultAuthenticationManagerTests
{ {
[Fact] [Fact]
@ -23,7 +23,7 @@ namespace Microsoft.AspNet.Http.Authentication.Internal
} }
[Theory] [Theory]
[InlineData("")] [InlineData("Automatic")]
[InlineData("Foo")] [InlineData("Foo")]
public async Task ChallengeWithNoAuthMiddlewareMayThrow(string scheme) public async Task ChallengeWithNoAuthMiddlewareMayThrow(string scheme)
{ {