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

Commit 5fe8037

Browse files
committed
Auth API changes (Async, ChallengeBehavior)
1 parent 641a7fb commit 5fe8037

File tree

13 files changed

+136
-166
lines changed

13 files changed

+136
-166
lines changed

src/Microsoft.AspNet.Http.Abstractions/Authentication/AuthenticateResult.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Microsoft.AspNet.Http.Abstractions/Authentication/AuthenticationManager.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,70 @@
44
using System.Collections.Generic;
55
using System.Security.Claims;
66
using System.Threading.Tasks;
7+
using Microsoft.AspNet.Http.Features.Authentication;
8+
using Microsoft.Framework.Internal;
79

810
namespace Microsoft.AspNet.Http.Authentication
911
{
1012
public abstract class AuthenticationManager
1113
{
1214
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();
1315

14-
public abstract AuthenticationResult Authenticate(string authenticationScheme);
16+
public abstract Task AuthenticateAsync([NotNull] AuthenticateContext context);
1517

16-
public abstract Task<AuthenticationResult> AuthenticateAsync(string authenticationScheme);
18+
public virtual async Task<ClaimsPrincipal> AuthenticateAsync([NotNull] string authenticationScheme)
19+
{
20+
var context = new AuthenticateContext(authenticationScheme);
21+
await AuthenticateAsync(context);
22+
return context.Principal;
23+
}
1724

18-
public virtual void Challenge()
25+
public virtual Task ChallengeAsync()
1926
{
20-
Challenge(authenticationScheme: null, properties: null);
27+
return ChallengeAsync(properties: null);
2128
}
2229

23-
public virtual void Challenge(AuthenticationProperties properties)
30+
public virtual Task ChallengeAsync(AuthenticationProperties properties)
2431
{
25-
Challenge(authenticationScheme: null, properties: properties);
32+
return ChallengeAsync(authenticationScheme: string.Empty, properties: properties);
2633
}
2734

28-
public virtual void Challenge(string authenticationScheme)
35+
public virtual Task ChallengeAsync([NotNull] string authenticationScheme)
2936
{
30-
Challenge(authenticationScheme: authenticationScheme, properties: null);
37+
return ChallengeAsync(authenticationScheme: authenticationScheme, properties: null);
3138
}
3239

33-
public abstract void Challenge(string authenticationScheme, AuthenticationProperties properties);
40+
// Leave it up to authentication handler to do the right thing for the challenge
41+
public virtual Task ChallengeAsync([NotNull] string authenticationScheme, AuthenticationProperties properties)
42+
{
43+
return ChallengeAsync(authenticationScheme, properties, ChallengeBehavior.Automatic);
44+
}
3445

35-
public void SignIn(string authenticationScheme, ClaimsPrincipal principal)
46+
public virtual Task SignInAsync([NotNull] string authenticationScheme, [NotNull] ClaimsPrincipal principal)
3647
{
37-
SignIn(authenticationScheme, principal, properties: null);
48+
return SignInAsync(authenticationScheme, principal, properties: null);
3849
}
3950

40-
public abstract void SignIn(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties);
51+
public virtual Task ForbidAsync([NotNull] string authenticationScheme)
52+
{
53+
return ForbidAsync(authenticationScheme, properties: null);
54+
}
4155

42-
public virtual void SignOut()
56+
// Deny access (typically a 403)
57+
public virtual Task ForbidAsync([NotNull] string authenticationScheme, AuthenticationProperties properties)
4358
{
44-
SignOut(authenticationScheme: null, properties: null);
59+
return ChallengeAsync(authenticationScheme, properties, ChallengeBehavior.Forbidden);
4560
}
4661

47-
public abstract void SignOut(string authenticationScheme);
62+
public abstract Task ChallengeAsync([NotNull] string authenticationScheme, AuthenticationProperties properties, ChallengeBehavior behavior);
63+
64+
public abstract Task SignInAsync([NotNull] string authenticationScheme, [NotNull] ClaimsPrincipal principal, AuthenticationProperties properties);
65+
66+
public virtual Task SignOutAsync([NotNull] string authenticationScheme)
67+
{
68+
return SignOutAsync(authenticationScheme, properties: null);
69+
}
4870

49-
public abstract void SignOut(string authenticationScheme, AuthenticationProperties properties);
71+
public abstract Task SignOutAsync([NotNull] string authenticationScheme, AuthenticationProperties properties);
5072
}
5173
}

src/Microsoft.AspNet.Http.Abstractions/HttpResponse.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33

44
using System;
55
using System.IO;
6+
using System.Threading.Tasks;
7+
using Microsoft.Framework.Internal;
68

79
namespace Microsoft.AspNet.Http
810
{
911
public abstract class HttpResponse
1012
{
13+
private static readonly Func<object, Task> _callbackDelegate = callback => ((Func<Task>)callback)();
14+
private static readonly Func<object, Task> _disposeDelegate = disposable =>
15+
{
16+
((IDisposable)disposable).Dispose();
17+
return Task.FromResult(0);
18+
};
19+
1120
public abstract HttpContext HttpContext { get; }
1221

1322
public abstract int StatusCode { get; set; }
@@ -24,14 +33,17 @@ public abstract class HttpResponse
2433

2534
public abstract bool HasStarted { get; }
2635

27-
public abstract void OnResponseStarting(Action<object> callback, object state);
36+
public abstract void OnStarting([NotNull] Func<object, Task> callback, object state);
2837

29-
public abstract void OnResponseCompleted(Action<object> callback, object state);
38+
public virtual void OnStarting([NotNull] Func<Task> callback) => OnStarting(_callbackDelegate, callback);
3039

31-
public virtual void Redirect(string location)
32-
{
33-
Redirect(location, permanent: false);
34-
}
40+
public abstract void OnCompleted([NotNull] Func<object, Task> callback, object state);
41+
42+
public virtual void OnCompletedDispose([NotNull] IDisposable disposable) => OnCompleted(_disposeDelegate, disposable);
43+
44+
public virtual void OnCompleted([NotNull] Func<Task> callback) => OnCompleted(_callbackDelegate, callback);
45+
46+
public virtual void Redirect(string location) => Redirect(location, permanent: false);
3547

3648
public abstract void Redirect(string location, bool permanent);
3749
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
namespace Microsoft.AspNet.Http.Features.Authentication
5+
{
6+
public enum ChallengeBehavior
7+
{
8+
Automatic,
9+
Unauthorized,
10+
Forbidden
11+
}
12+
}

src/Microsoft.AspNet.Http.Features/Authentication/ChallengeContext.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.Framework.Internal;
67

78
namespace Microsoft.AspNet.Http.Features.Authentication
89
{
910
public class ChallengeContext
1011
{
11-
public ChallengeContext(string authenticationScheme, IDictionary<string, string> properties)
12+
public ChallengeContext([NotNull] string authenticationScheme) : this(authenticationScheme, properties: null, behavior: ChallengeBehavior.Automatic)
13+
{
14+
}
15+
16+
public ChallengeContext([NotNull] string authenticationScheme, IDictionary<string, string> properties, ChallengeBehavior behavior)
1217
{
1318
AuthenticationScheme = authenticationScheme;
1419
Properties = properties ?? new Dictionary<string, string>(StringComparer.Ordinal);
20+
Behavior = behavior;
1521
}
1622

1723
public string AuthenticationScheme { get; }
1824

25+
public ChallengeBehavior Behavior { get; }
26+
1927
public IDictionary<string, string> Properties { get; }
2028

2129
public bool Accepted { get; private set; }

src/Microsoft.AspNet.Http.Features/Authentication/IAuthenticationHandler.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ public interface IAuthenticationHandler
99
{
1010
void GetDescriptions(DescribeSchemesContext context);
1111

12-
void Authenticate(AuthenticateContext context);
13-
1412
Task AuthenticateAsync(AuthenticateContext context);
1513

16-
void Challenge(ChallengeContext context);
14+
Task ChallengeAsync(ChallengeContext context);
1715

18-
void SignIn(SignInContext context);
16+
Task SignInAsync(SignInContext context);
1917

20-
void SignOut(SignOutContext context);
18+
Task SignOutAsync(SignOutContext context);
2119
}
2220
}

src/Microsoft.AspNet.Http.Features/IHttpResponseFeature.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Threading.Tasks;
78

89
namespace Microsoft.AspNet.Http.Features
910
{
@@ -14,7 +15,7 @@ public interface IHttpResponseFeature
1415
IDictionary<string, string[]> Headers { get; set; }
1516
Stream Body { get; set; }
1617
bool HasStarted { get; }
17-
void OnResponseStarting(Action<object> callback, object state);
18-
void OnResponseCompleted(Action<object> callback, object state);
18+
void OnStarting(Func<object, Task> callback, object state);
19+
void OnCompleted(Func<object, Task> callback, object state);
1920
}
2021
}

src/Microsoft.AspNet.Http/Authentication/DefaultAuthenticationManager.cs

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -48,66 +48,29 @@ public override IEnumerable<AuthenticationDescription> GetAuthenticationSchemes(
4848
return describeContext.Results.Select(description => new AuthenticationDescription(description));
4949
}
5050

51-
public override AuthenticationResult Authenticate([NotNull] string authenticationScheme)
51+
public override async Task AuthenticateAsync([NotNull] AuthenticateContext context)
5252
{
5353
var handler = HttpAuthenticationFeature.Handler;
5454

55-
var authenticateContext = new AuthenticateContext(authenticationScheme);
5655
if (handler != null)
5756
{
58-
handler.Authenticate(authenticateContext);
57+
await handler.AuthenticateAsync(context);
5958
}
6059

61-
if (!authenticateContext.Accepted)
60+
if (!context.Accepted)
6261
{
63-
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}");
64-
}
65-
66-
if (authenticateContext.Principal == null)
67-
{
68-
return null;
69-
}
70-
71-
return new AuthenticationResult(authenticateContext.Principal,
72-
new AuthenticationProperties(authenticateContext.Properties),
73-
new AuthenticationDescription(authenticateContext.Description));
74-
}
75-
76-
public override async Task<AuthenticationResult> AuthenticateAsync([NotNull] string authenticationScheme)
77-
{
78-
var handler = HttpAuthenticationFeature.Handler;
79-
80-
var authenticateContext = new AuthenticateContext(authenticationScheme);
81-
if (handler != null)
82-
{
83-
await handler.AuthenticateAsync(authenticateContext);
84-
}
85-
86-
// Verify all types ack'd
87-
if (!authenticateContext.Accepted)
88-
{
89-
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}");
90-
}
91-
92-
if (authenticateContext.Principal == null)
93-
{
94-
return null;
62+
throw new InvalidOperationException($"The following authentication scheme was not accepted: {context.AuthenticationScheme}");
9563
}
96-
97-
return new AuthenticationResult(authenticateContext.Principal,
98-
new AuthenticationProperties(authenticateContext.Properties),
99-
new AuthenticationDescription(authenticateContext.Description));
10064
}
10165

102-
public override void Challenge(string authenticationScheme, AuthenticationProperties properties)
66+
public override async Task ChallengeAsync([NotNull] string authenticationScheme, AuthenticationProperties properties, ChallengeBehavior behavior)
10367
{
104-
HttpResponseFeature.StatusCode = 401;
10568
var handler = HttpAuthenticationFeature.Handler;
10669

107-
var challengeContext = new ChallengeContext(authenticationScheme, properties?.Items);
70+
var challengeContext = new ChallengeContext(authenticationScheme, properties?.Items, behavior);
10871
if (handler != null)
10972
{
110-
handler.Challenge(challengeContext);
73+
await handler.ChallengeAsync(challengeContext);
11174
}
11275

11376
// The default Challenge with no scheme is always accepted
@@ -117,43 +80,36 @@ public override void Challenge(string authenticationScheme, AuthenticationProper
11780
}
11881
}
11982

120-
public override void SignIn([NotNull] string authenticationScheme, [NotNull] ClaimsPrincipal principal, AuthenticationProperties properties)
83+
public override async Task SignInAsync([NotNull] string authenticationScheme, [NotNull] ClaimsPrincipal principal, AuthenticationProperties properties)
12184
{
12285
var handler = HttpAuthenticationFeature.Handler;
12386

12487
var signInContext = new SignInContext(authenticationScheme, principal, properties?.Items);
12588
if (handler != null)
12689
{
127-
handler.SignIn(signInContext);
90+
await handler.SignInAsync(signInContext);
12891
}
12992

130-
// Verify all types ack'd
13193
if (!signInContext.Accepted)
13294
{
13395
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}");
13496
}
13597
}
13698

137-
public override void SignOut(string authenticationScheme, AuthenticationProperties properties)
99+
public override async Task SignOutAsync([NotNull] string authenticationScheme, AuthenticationProperties properties)
138100
{
139101
var handler = HttpAuthenticationFeature.Handler;
140102

141103
var signOutContext = new SignOutContext(authenticationScheme, properties?.Items);
142104
if (handler != null)
143105
{
144-
handler.SignOut(signOutContext);
106+
await handler.SignOutAsync(signOutContext);
145107
}
146108

147-
// Verify all types ack'd
148109
if (!string.IsNullOrWhiteSpace(authenticationScheme) && !signOutContext.Accepted)
149110
{
150111
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}");
151112
}
152113
}
153-
154-
public override void SignOut(string authenticationScheme)
155-
{
156-
SignOut(authenticationScheme, properties: null);
157-
}
158114
}
159115
}

0 commit comments

Comments
 (0)