diff --git a/src/Microsoft.AspNet.Http.Interfaces/Authentication/AuthenticateContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/AuthenticateContext.cs new file mode 100644 index 0000000000..e76a501d73 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Interfaces/Authentication/AuthenticateContext.cs @@ -0,0 +1,40 @@ +// 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.Collections.Generic; +using System.Security.Claims; +using Microsoft.Framework.Internal; + +namespace Microsoft.AspNet.Http.Authentication +{ + public class AuthenticateContext + { + public AuthenticateContext([NotNull] string authenticationScheme) + { + AuthenticationScheme = authenticationScheme; + } + + public string AuthenticationScheme { get; } + + public bool Accepted { get; private set; } + + public ClaimsPrincipal Principal { get; private set; } + + public IDictionary Properties { get; private set; } + + public IDictionary Description { get; private set; } + + public virtual void Authenticated(ClaimsPrincipal principal, IDictionary properties, IDictionary description) + { + Accepted = true; + Principal = principal; + Properties = properties; + Description = description; + } + + public virtual void NotAuthenticated() + { + Accepted = true; + } + } +} diff --git a/src/Microsoft.AspNet.Http/Authentication/ChallengeContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/ChallengeContext.cs similarity index 54% rename from src/Microsoft.AspNet.Http/Authentication/ChallengeContext.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/ChallengeContext.cs index 0a137bb645..e1b6bf8c04 100644 --- a/src/Microsoft.AspNet.Http/Authentication/ChallengeContext.cs +++ b/src/Microsoft.AspNet.Http.Interfaces/Authentication/ChallengeContext.cs @@ -3,35 +3,26 @@ using System; using System.Collections.Generic; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Http.Authentication { - public class ChallengeContext : IChallengeContext + public class ChallengeContext { - private bool _accepted; - public ChallengeContext(string authenticationScheme, IDictionary properties) { AuthenticationScheme = authenticationScheme; Properties = properties ?? new Dictionary(StringComparer.Ordinal); - - // The default Challenge with no scheme is always accepted - _accepted = string.IsNullOrEmpty(authenticationScheme); } - public string AuthenticationScheme { get; private set; } + public string AuthenticationScheme { get; } - public IDictionary Properties { get; private set; } + public IDictionary Properties { get; } - public bool Accepted - { - get { return _accepted; } - } + public bool Accepted { get; private set; } public void Accept() { - _accepted = true; + Accepted = true; } } -} +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http/Authentication/DescribeSchemesContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/DescribeSchemesContext.cs similarity index 60% rename from src/Microsoft.AspNet.Http/Authentication/DescribeSchemesContext.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/DescribeSchemesContext.cs index 6e4907949d..38daa0a856 100644 --- a/src/Microsoft.AspNet.Http/Authentication/DescribeSchemesContext.cs +++ b/src/Microsoft.AspNet.Http.Interfaces/Authentication/DescribeSchemesContext.cs @@ -5,23 +5,23 @@ using System.Collections.Generic; namespace Microsoft.AspNet.Http.Authentication { - public class DescribeSchemesContext : IDescribeSchemesContext + public class DescribeSchemesContext { - private List _results; + private List> _results; public DescribeSchemesContext() { - _results = new List(); + _results = new List>(); } - public IEnumerable Results + public IEnumerable> Results { get { return _results; } } public void Accept(IDictionary description) { - _results.Add(new AuthenticationDescription(description)); + _results.Add(description); } } -} +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticateContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticateContext.cs deleted file mode 100644 index ecf8b56788..0000000000 --- a/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticateContext.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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.Collections.Generic; -using System.Security.Claims; - -namespace Microsoft.AspNet.Http.Authentication -{ - public interface IAuthenticateContext - { - string AuthenticationScheme { get; } - - void Authenticated(ClaimsPrincipal principal, IDictionary properties, IDictionary description); - - void NotAuthenticated(); - } -} diff --git a/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticationHandler.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticationHandler.cs index 76b3e8cb1b..04a7039a80 100644 --- a/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticationHandler.cs +++ b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticationHandler.cs @@ -7,13 +7,16 @@ namespace Microsoft.AspNet.Http.Authentication { public interface IAuthenticationHandler { - void GetDescriptions(IDescribeSchemesContext context); + void GetDescriptions(DescribeSchemesContext context); - void Authenticate(IAuthenticateContext context); - Task AuthenticateAsync(IAuthenticateContext context); + void Authenticate(AuthenticateContext context); - void Challenge(IChallengeContext context); - void SignIn(ISignInContext context); - void SignOut(ISignOutContext context); + Task AuthenticateAsync(AuthenticateContext context); + + void Challenge(ChallengeContext context); + + void SignIn(SignInContext context); + + void SignOut(SignOutContext context); } } diff --git a/src/Microsoft.AspNet.Http.Interfaces/Authentication/IChallengeContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IChallengeContext.cs deleted file mode 100644 index 26a90fefb2..0000000000 --- a/src/Microsoft.AspNet.Http.Interfaces/Authentication/IChallengeContext.cs +++ /dev/null @@ -1,15 +0,0 @@ -// 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.Collections.Generic; - -namespace Microsoft.AspNet.Http.Authentication -{ - public interface IChallengeContext - { - string AuthenticationScheme { get; } - IDictionary Properties { get; } - - void Accept(); - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http.Interfaces/Authentication/IDescribeSchemesContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/IDescribeSchemesContext.cs deleted file mode 100644 index 96f395cb37..0000000000 --- a/src/Microsoft.AspNet.Http.Interfaces/Authentication/IDescribeSchemesContext.cs +++ /dev/null @@ -1,12 +0,0 @@ -// 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.Collections.Generic; - -namespace Microsoft.AspNet.Http.Authentication -{ - public interface IDescribeSchemesContext - { - void Accept(IDictionary description); - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignInContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignInContext.cs deleted file mode 100644 index c79860279d..0000000000 --- a/src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignInContext.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.Collections.Generic; -using System.Security.Claims; - -namespace Microsoft.AspNet.Http.Authentication -{ - public interface ISignInContext - { - //IEnumerable Principals { get; } - ClaimsPrincipal Principal { get; } - IDictionary Properties { get; } - string AuthenticationScheme { get; } - - void Accept(IDictionary description); - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignOutContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignOutContext.cs deleted file mode 100644 index 2bb7036026..0000000000 --- a/src/Microsoft.AspNet.Http.Interfaces/Authentication/ISignOutContext.cs +++ /dev/null @@ -1,16 +0,0 @@ -// 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.Collections.Generic; - -namespace Microsoft.AspNet.Http.Authentication -{ - public interface ISignOutContext - { - string AuthenticationScheme { get; } - - IDictionary Properties { get; } - - void Accept(); - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http/Authentication/SignInContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/SignInContext.cs similarity index 70% rename from src/Microsoft.AspNet.Http/Authentication/SignInContext.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/SignInContext.cs index 1a77db412b..d5d74c3186 100644 --- a/src/Microsoft.AspNet.Http/Authentication/SignInContext.cs +++ b/src/Microsoft.AspNet.Http.Interfaces/Authentication/SignInContext.cs @@ -8,31 +8,26 @@ using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Http.Authentication { - public class SignInContext : ISignInContext + public class SignInContext { - private bool _accepted; - - public SignInContext([NotNull] string authenticationScheme, [NotNull] ClaimsPrincipal principal, IDictionary dictionary) + public SignInContext([NotNull] string authenticationScheme, [NotNull] ClaimsPrincipal principal, IDictionary properties) { AuthenticationScheme = authenticationScheme; Principal = principal; - Properties = dictionary ?? new Dictionary(StringComparer.Ordinal); + Properties = properties ?? new Dictionary(StringComparer.Ordinal); } + public string AuthenticationScheme { get; } + public ClaimsPrincipal Principal { get; } public IDictionary Properties { get; } - public string AuthenticationScheme { get; } + public bool Accepted { get; private set; } - public bool Accepted + public void Accept() { - get { return _accepted; } - } - - public void Accept(IDictionary description) - { - _accepted = true; + Accepted = true; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http/Authentication/SignOutContext.cs b/src/Microsoft.AspNet.Http.Interfaces/Authentication/SignOutContext.cs similarity index 79% rename from src/Microsoft.AspNet.Http/Authentication/SignOutContext.cs rename to src/Microsoft.AspNet.Http.Interfaces/Authentication/SignOutContext.cs index d78820980e..27a7d2941b 100644 --- a/src/Microsoft.AspNet.Http/Authentication/SignOutContext.cs +++ b/src/Microsoft.AspNet.Http.Interfaces/Authentication/SignOutContext.cs @@ -7,10 +7,8 @@ using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Http.Authentication { - public class SignOutContext : ISignOutContext + public class SignOutContext { - private bool _accepted; - public SignOutContext([NotNull] string authenticationScheme, IDictionary properties) { AuthenticationScheme = authenticationScheme; @@ -21,14 +19,11 @@ namespace Microsoft.AspNet.Http.Authentication public IDictionary Properties { get; } - public bool Accepted - { - get { return _accepted; } - } + public bool Accepted { get; private set; } public void Accept() { - _accepted = true; + Accepted = true; } } -} +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http.Interfaces/project.json b/src/Microsoft.AspNet.Http.Interfaces/project.json index 3af7b3bdfb..d4d113923c 100644 --- a/src/Microsoft.AspNet.Http.Interfaces/project.json +++ b/src/Microsoft.AspNet.Http.Interfaces/project.json @@ -1,16 +1,21 @@ { - "version": "1.0.0-*", - "description": "ASP.NET 5 HTTP feature interface definitions.", - "frameworks": { - "dnx451": {}, - "dnxcore50": { - "dependencies": { - "System.Net.Primitives": "4.0.10-beta-*", - "System.Net.WebSockets" : "4.0.0-beta-*", - "System.Security.Claims": "4.0.0-beta-*", - "System.Security.Cryptography.X509Certificates": "4.0.0-beta-*", - "System.Security.Principal": "4.0.0-beta-*" - } + "version": "1.0.0-*", + "description": "ASP.NET 5 HTTP feature interface definitions.", + "dependencies": { + "Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" } + }, + "frameworks": { + "dnx451": { }, + "dnxcore50": { + "dependencies": { + "System.Collections": "4.0.10-beta-*", + "System.Net.Primitives": "4.0.10-beta-*", + "System.Net.WebSockets": "4.0.0-beta-*", + "System.Runtime.Extensions": "4.0.10-beta-*", + "System.Security.Claims": "4.0.0-beta-*", + "System.Security.Cryptography.X509Certificates": "4.0.0-beta-*", + "System.Security.Principal": "4.0.0-beta-*" + } + } } - } } diff --git a/src/Microsoft.AspNet.Http/Authentication/AuthenticateContext.cs b/src/Microsoft.AspNet.Http/Authentication/AuthenticateContext.cs deleted file mode 100644 index 91d530dfad..0000000000 --- a/src/Microsoft.AspNet.Http/Authentication/AuthenticateContext.cs +++ /dev/null @@ -1,41 +0,0 @@ -// 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.Collections.Generic; -using System.Security.Claims; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Http.Authentication -{ - public class AuthenticateContext : IAuthenticateContext - { - private AuthenticationResult _result; - private bool _accepted; - - public AuthenticateContext([NotNull] string authenticationScheme) - { - AuthenticationScheme = authenticationScheme; - } - - public string AuthenticationScheme { get; private set; } - - public AuthenticationResult Result { get; set; } - - public bool Accepted - { - get { return _accepted; } - } - - public void Authenticated(ClaimsPrincipal principal, IDictionary properties, IDictionary description) - { - var descrip = new AuthenticationDescription(description); - _accepted = true; - Result = new AuthenticationResult(principal, new AuthenticationProperties(properties), descrip); - } - - public void NotAuthenticated() - { - _accepted = true; - } - } -} diff --git a/src/Microsoft.AspNet.Http/Authentication/DefaultAuthenticationManager.cs b/src/Microsoft.AspNet.Http/Authentication/DefaultAuthenticationManager.cs index 9fccca6116..e21bea9634 100644 --- a/src/Microsoft.AspNet.Http/Authentication/DefaultAuthenticationManager.cs +++ b/src/Microsoft.AspNet.Http/Authentication/DefaultAuthenticationManager.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.FeatureModel; @@ -42,7 +43,7 @@ namespace Microsoft.AspNet.Http.Authentication var describeContext = new DescribeSchemesContext(); handler.GetDescriptions(describeContext); - return describeContext.Results; + return describeContext.Results.Select(description => new AuthenticationDescription(description)); } public override AuthenticationResult Authenticate([NotNull] string authenticationScheme) @@ -57,10 +58,12 @@ namespace Microsoft.AspNet.Http.Authentication if (!authenticateContext.Accepted) { - throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme); + throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}"); } - return authenticateContext.Result; + return new AuthenticationResult(authenticateContext.Principal, + new AuthenticationProperties(authenticateContext.Properties), + new AuthenticationDescription(authenticateContext.Description)); } public override async Task AuthenticateAsync([NotNull] string authenticationScheme) @@ -76,10 +79,12 @@ namespace Microsoft.AspNet.Http.Authentication // Verify all types ack'd if (!authenticateContext.Accepted) { - throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme); + throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}"); } - return authenticateContext.Result; + return new AuthenticationResult(authenticateContext.Principal, + new AuthenticationProperties(authenticateContext.Properties), + new AuthenticationDescription(authenticateContext.Description)); } public override void Challenge(AuthenticationProperties properties, string authenticationScheme) @@ -87,23 +92,24 @@ namespace Microsoft.AspNet.Http.Authentication HttpResponseFeature.StatusCode = 401; var handler = HttpAuthenticationFeature.Handler; - var challengeContext = new ChallengeContext(authenticationScheme, properties == null ? null : properties.Items); + var challengeContext = new ChallengeContext(authenticationScheme, properties?.Items); if (handler != null) { handler.Challenge(challengeContext); } - if (!challengeContext.Accepted) + // The default Challenge with no scheme is always accepted + if (!challengeContext.Accepted && !string.IsNullOrEmpty(authenticationScheme)) { - throw new InvalidOperationException("The following authentication type was not accepted: " + authenticationScheme); + throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}"); } } - public override void SignIn(string authenticationScheme, [NotNull] ClaimsPrincipal principal, AuthenticationProperties properties) + public override void SignIn([NotNull] string authenticationScheme, [NotNull] ClaimsPrincipal principal, AuthenticationProperties properties) { var handler = HttpAuthenticationFeature.Handler; - var signInContext = new SignInContext(authenticationScheme, principal, properties == null ? null : properties.Items); + var signInContext = new SignInContext(authenticationScheme, principal, properties?.Items); if (handler != null) { handler.SignIn(signInContext); @@ -112,7 +118,7 @@ namespace Microsoft.AspNet.Http.Authentication // Verify all types ack'd if (!signInContext.Accepted) { - throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme); + throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}"); } } @@ -129,7 +135,7 @@ namespace Microsoft.AspNet.Http.Authentication // Verify all types ack'd if (!string.IsNullOrWhiteSpace(authenticationScheme) && !signOutContext.Accepted) { - throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme); + throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}"); } } diff --git a/test/Microsoft.AspNet.Http.Tests/DefaultHttpContextTests.cs b/test/Microsoft.AspNet.Http.Tests/DefaultHttpContextTests.cs index c53c3c73c2..ea887b711b 100644 --- a/test/Microsoft.AspNet.Http.Tests/DefaultHttpContextTests.cs +++ b/test/Microsoft.AspNet.Http.Tests/DefaultHttpContextTests.cs @@ -97,33 +97,33 @@ namespace Microsoft.AspNet.Http { public bool SignedIn { get; set; } - public void Authenticate(IAuthenticateContext context) + public void Authenticate(AuthenticateContext context) { throw new NotImplementedException(); } - public Task AuthenticateAsync(IAuthenticateContext context) + public Task AuthenticateAsync(AuthenticateContext context) { throw new NotImplementedException(); } - public void Challenge(IChallengeContext context) + public void Challenge(ChallengeContext context) { throw new NotImplementedException(); } - public void GetDescriptions(IDescribeSchemesContext context) + public void GetDescriptions(DescribeSchemesContext context) { throw new NotImplementedException(); } - public void SignIn(ISignInContext context) + public void SignIn(SignInContext context) { SignedIn = true; - context.Accept(new Dictionary()); + context.Accept(); } - public void SignOut(ISignOutContext context) + public void SignOut(SignOutContext context) { SignedIn = false; context.Accept();