#273 - Use POCOs for auth context objects.

This commit is contained in:
Chris Ross 2015-04-20 10:35:46 -07:00
parent a174bb299e
commit cc1a24b949
15 changed files with 116 additions and 200 deletions

View File

@ -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<string, string> Properties { get; private set; }
public IDictionary<string, object> Description { get; private set; }
public virtual void Authenticated(ClaimsPrincipal principal, IDictionary<string, string> properties, IDictionary<string, object> description)
{
Accepted = true;
Principal = principal;
Properties = properties;
Description = description;
}
public virtual void NotAuthenticated()
{
Accepted = true;
}
}
}

View File

@ -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<string, string> properties)
{
AuthenticationScheme = authenticationScheme;
Properties = properties ?? new Dictionary<string, string>(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<string, string> Properties { get; private set; }
public IDictionary<string, string> Properties { get; }
public bool Accepted
{
get { return _accepted; }
}
public bool Accepted { get; private set; }
public void Accept()
{
_accepted = true;
Accepted = true;
}
}
}
}

View File

@ -5,23 +5,23 @@ using System.Collections.Generic;
namespace Microsoft.AspNet.Http.Authentication
{
public class DescribeSchemesContext : IDescribeSchemesContext
public class DescribeSchemesContext
{
private List<AuthenticationDescription> _results;
private List<IDictionary<string, object>> _results;
public DescribeSchemesContext()
{
_results = new List<AuthenticationDescription>();
_results = new List<IDictionary<string, object>>();
}
public IEnumerable<AuthenticationDescription> Results
public IEnumerable<IDictionary<string, object>> Results
{
get { return _results; }
}
public void Accept(IDictionary<string, object> description)
{
_results.Add(new AuthenticationDescription(description));
_results.Add(description);
}
}
}
}

View File

@ -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<string, string> properties, IDictionary<string, object> description);
void NotAuthenticated();
}
}

View File

@ -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);
}
}

View File

@ -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<string, string> Properties { get; }
void Accept();
}
}

View File

@ -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<string,object> description);
}
}

View File

@ -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<ClaimsPrincipal> Principals { get; }
ClaimsPrincipal Principal { get; }
IDictionary<string, string> Properties { get; }
string AuthenticationScheme { get; }
void Accept(IDictionary<string, object> description);
}
}

View File

@ -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<string, string> Properties { get; }
void Accept();
}
}

View File

@ -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<string, string> dictionary)
public SignInContext([NotNull] string authenticationScheme, [NotNull] ClaimsPrincipal principal, IDictionary<string, string> properties)
{
AuthenticationScheme = authenticationScheme;
Principal = principal;
Properties = dictionary ?? new Dictionary<string, string>(StringComparer.Ordinal);
Properties = properties ?? new Dictionary<string, string>(StringComparer.Ordinal);
}
public string AuthenticationScheme { get; }
public ClaimsPrincipal Principal { get; }
public IDictionary<string, string> 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<string, object> description)
{
_accepted = true;
Accepted = true;
}
}
}

View File

@ -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<string, string> properties)
{
AuthenticationScheme = authenticationScheme;
@ -21,14 +19,11 @@ namespace Microsoft.AspNet.Http.Authentication
public IDictionary<string, string> Properties { get; }
public bool Accepted
{
get { return _accepted; }
}
public bool Accepted { get; private set; }
public void Accept()
{
_accepted = true;
Accepted = true;
}
}
}
}

View File

@ -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-*"
}
}
}
}
}

View File

@ -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<string, string> properties, IDictionary<string, object> description)
{
var descrip = new AuthenticationDescription(description);
_accepted = true;
Result = new AuthenticationResult(principal, new AuthenticationProperties(properties), descrip);
}
public void NotAuthenticated()
{
_accepted = true;
}
}
}

View File

@ -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<AuthenticationResult> 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}");
}
}

View File

@ -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<string, object>());
context.Accept();
}
public void SignOut(ISignOutContext context)
public void SignOut(SignOutContext context)
{
SignedIn = false;
context.Accept();