From 08ddbe853130fe10bdde6f8b529e5a885e0974be Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 16 Mar 2015 14:26:46 -0700 Subject: [PATCH] Auth cleanup - Rename Security folder -> Authentication - Change Authenticate to only take one scheme to match other APIs, the params overload did not make it any cleaner to consume (since it didn't produce a combined ClaimsPrincipal anyways) --- .../Authentication/AuthenticateContext.cs | 28 ++++++++----------- .../DefaultHttpContext.cs | 23 +++++++-------- .../IAuthenticateContext.cs | 4 +-- .../IAuthenticationHandler.cs | 0 .../IChallengeContext.cs | 0 .../IDescribeSchemesContext.cs | 0 .../IHttpAuthenticationFeature.cs | 1 + .../ISignInContext.cs | 0 .../ISignOutContext.cs | 0 src/Microsoft.AspNet.Http/HttpContext.cs | 14 ++-------- 10 files changed, 27 insertions(+), 43 deletions(-) rename src/Microsoft.AspNet.Http.Interfaces/{Security => Authentication}/IAuthenticateContext.cs (71%) rename src/Microsoft.AspNet.Http.Interfaces/{Security => Authentication}/IAuthenticationHandler.cs (100%) rename src/Microsoft.AspNet.Http.Interfaces/{Security => Authentication}/IChallengeContext.cs (100%) rename src/Microsoft.AspNet.Http.Interfaces/{Security => Authentication}/IDescribeSchemesContext.cs (100%) rename src/Microsoft.AspNet.Http.Interfaces/{Security => Authentication}/IHttpAuthenticationFeature.cs (99%) rename src/Microsoft.AspNet.Http.Interfaces/{Security => Authentication}/ISignInContext.cs (100%) rename src/Microsoft.AspNet.Http.Interfaces/{Security => Authentication}/ISignOutContext.cs (100%) diff --git a/src/Microsoft.AspNet.Http.Core/Authentication/AuthenticateContext.cs b/src/Microsoft.AspNet.Http.Core/Authentication/AuthenticateContext.cs index 1fa87a6417..ed0dd55eb7 100644 --- a/src/Microsoft.AspNet.Http.Core/Authentication/AuthenticateContext.cs +++ b/src/Microsoft.AspNet.Http.Core/Authentication/AuthenticateContext.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Security.Claims; using Microsoft.AspNet.Http.Authentication; @@ -10,24 +11,19 @@ namespace Microsoft.AspNet.Http.Core.Authentication { public class AuthenticateContext : IAuthenticateContext { - private List _results; - private List _accepted; + private AuthenticationResult _result; + private bool _accepted; - public AuthenticateContext([NotNull] IEnumerable authenticationSchemes) + public AuthenticateContext([NotNull] string authenticationScheme) { - AuthenticationSchemes = authenticationSchemes; - _results = new List(); - _accepted = new List(); + AuthenticationScheme = authenticationScheme; } - public IEnumerable AuthenticationSchemes { get; private set; } + public string AuthenticationScheme { get; private set; } - public IEnumerable Results - { - get { return _results; } - } + public AuthenticationResult Result { get; set; } - public IEnumerable Accepted + public bool Accepted { get { return _accepted; } } @@ -35,13 +31,13 @@ namespace Microsoft.AspNet.Http.Core.Authentication public void Authenticated(ClaimsPrincipal principal, IDictionary properties, IDictionary description) { var descrip = new AuthenticationDescription(description); - _accepted.Add(descrip.AuthenticationScheme); // may not match identity.AuthType - _results.Add(new AuthenticationResult(principal, new AuthenticationProperties(properties), descrip)); + _accepted = true; + Result = new AuthenticationResult(principal, new AuthenticationProperties(properties), descrip); } - public void NotAuthenticated(string authenticationScheme, IDictionary properties, IDictionary description) + public void NotAuthenticated() { - _accepted.Add(authenticationScheme); + _accepted = true; } } } diff --git a/src/Microsoft.AspNet.Http.Core/DefaultHttpContext.cs b/src/Microsoft.AspNet.Http.Core/DefaultHttpContext.cs index 90dff4c588..5ff30f1fc5 100644 --- a/src/Microsoft.AspNet.Http.Core/DefaultHttpContext.cs +++ b/src/Microsoft.AspNet.Http.Core/DefaultHttpContext.cs @@ -212,44 +212,41 @@ namespace Microsoft.AspNet.Http.Core return describeContext.Results; } - public override IEnumerable Authenticate([NotNull] IEnumerable authenticationSchemes) + public override AuthenticationResult Authenticate([NotNull] string authenticationScheme) { var handler = HttpAuthenticationFeature.Handler; - var authenticateContext = new AuthenticateContext(authenticationSchemes); + var authenticateContext = new AuthenticateContext(authenticationScheme); if (handler != null) { handler.Authenticate(authenticateContext); } - // Verify all types ack'd - IEnumerable leftovers = authenticationSchemes.Except(authenticateContext.Accepted); - if (leftovers.Any()) + if (!authenticateContext.Accepted) { - throw new InvalidOperationException("The following authentication schemes were not accepted: " + string.Join(", ", leftovers)); + throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme); } - return authenticateContext.Results; + return authenticateContext.Result; } - public override async Task> AuthenticateAsync([NotNull] IEnumerable authenticationSchemes) + public override async Task AuthenticateAsync([NotNull] string authenticationScheme) { var handler = HttpAuthenticationFeature.Handler; - var authenticateContext = new AuthenticateContext(authenticationSchemes); + var authenticateContext = new AuthenticateContext(authenticationScheme); if (handler != null) { await handler.AuthenticateAsync(authenticateContext); } // Verify all types ack'd - IEnumerable leftovers = authenticationSchemes.Except(authenticateContext.Accepted); - if (leftovers.Any()) + if (!authenticateContext.Accepted) { - throw new InvalidOperationException("The following authentication schemes were not accepted: " + string.Join(", ", leftovers)); + throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme); } - return authenticateContext.Results; + return authenticateContext.Result; } public override Task AcceptWebSocketAsync(string subProtocol) diff --git a/src/Microsoft.AspNet.Http.Interfaces/Security/IAuthenticateContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticateContext.cs similarity index 71% rename from src/Microsoft.AspNet.Http.Interfaces/Security/IAuthenticateContext.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticateContext.cs index 71c2b97a30..ecf8b56788 100644 --- a/src/Microsoft.AspNet.Http.Interfaces/Security/IAuthenticateContext.cs +++ b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticateContext.cs @@ -8,10 +8,10 @@ namespace Microsoft.AspNet.Http.Authentication { public interface IAuthenticateContext { - IEnumerable AuthenticationSchemes { get; } + string AuthenticationScheme { get; } void Authenticated(ClaimsPrincipal principal, IDictionary properties, IDictionary description); - void NotAuthenticated(string authenticationScheme, IDictionary properties, IDictionary description); + void NotAuthenticated(); } } diff --git a/src/Microsoft.AspNet.Http.Interfaces/Security/IAuthenticationHandler.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticationHandler.cs similarity index 100% rename from src/Microsoft.AspNet.Http.Interfaces/Security/IAuthenticationHandler.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticationHandler.cs diff --git a/src/Microsoft.AspNet.Http.Interfaces/Security/IChallengeContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IChallengeContext.cs similarity index 100% rename from src/Microsoft.AspNet.Http.Interfaces/Security/IChallengeContext.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/IChallengeContext.cs diff --git a/src/Microsoft.AspNet.Http.Interfaces/Security/IDescribeSchemesContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IDescribeSchemesContext.cs similarity index 100% rename from src/Microsoft.AspNet.Http.Interfaces/Security/IDescribeSchemesContext.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/IDescribeSchemesContext.cs diff --git a/src/Microsoft.AspNet.Http.Interfaces/Security/IHttpAuthenticationFeature.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IHttpAuthenticationFeature.cs similarity index 99% rename from src/Microsoft.AspNet.Http.Interfaces/Security/IHttpAuthenticationFeature.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/IHttpAuthenticationFeature.cs index 053f5d8e12..fb14959790 100644 --- a/src/Microsoft.AspNet.Http.Interfaces/Security/IHttpAuthenticationFeature.cs +++ b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IHttpAuthenticationFeature.cs @@ -8,6 +8,7 @@ namespace Microsoft.AspNet.Http.Authentication public interface IHttpAuthenticationFeature { ClaimsPrincipal User { get; set; } + IAuthenticationHandler Handler { get; set; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http.Interfaces/Security/ISignInContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignInContext.cs similarity index 100% rename from src/Microsoft.AspNet.Http.Interfaces/Security/ISignInContext.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignInContext.cs diff --git a/src/Microsoft.AspNet.Http.Interfaces/Security/ISignOutContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignOutContext.cs similarity index 100% rename from src/Microsoft.AspNet.Http.Interfaces/Security/ISignOutContext.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignOutContext.cs diff --git a/src/Microsoft.AspNet.Http/HttpContext.cs b/src/Microsoft.AspNet.Http/HttpContext.cs index c76019744e..c69f882f91 100644 --- a/src/Microsoft.AspNet.Http/HttpContext.cs +++ b/src/Microsoft.AspNet.Http/HttpContext.cs @@ -54,19 +54,9 @@ namespace Microsoft.AspNet.Http public abstract IEnumerable GetAuthenticationSchemes(); - public virtual AuthenticationResult Authenticate(string authenticationScheme) - { - return Authenticate(new[] { authenticationScheme }).SingleOrDefault(); - } + public abstract AuthenticationResult Authenticate(string authenticationScheme); - public abstract IEnumerable Authenticate(IEnumerable authenticationSchemes); - - public virtual async Task AuthenticateAsync(string authenticationScheme) - { - return (await AuthenticateAsync(new[] { authenticationScheme })).SingleOrDefault(); - } - - public abstract Task> AuthenticateAsync(IEnumerable authenticationSchemes); + public abstract Task AuthenticateAsync(string authenticationScheme); public virtual Task AcceptWebSocketAsync() {