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

Commit 30b84c0

Browse files
committed
#266 Consolidate authentication APIs.
1 parent f5267fc commit 30b84c0

File tree

8 files changed

+208
-174
lines changed

8 files changed

+208
-174
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Security.Claims;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.AspNet.Http.Authentication
9+
{
10+
public abstract class AuthenticationManager
11+
{
12+
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();
13+
14+
public abstract AuthenticationResult Authenticate(string authenticationScheme);
15+
16+
public abstract Task<AuthenticationResult> AuthenticateAsync(string authenticationScheme);
17+
18+
public virtual void Challenge()
19+
{
20+
Challenge(properties: null, authenticationScheme: null);
21+
}
22+
23+
public virtual void Challenge(AuthenticationProperties properties)
24+
{
25+
Challenge(properties, "");
26+
}
27+
28+
public virtual void Challenge(string authenticationScheme)
29+
{
30+
Challenge(properties: null, authenticationScheme: authenticationScheme);
31+
}
32+
33+
public abstract void Challenge(AuthenticationProperties properties, string authenticationScheme);
34+
35+
public abstract void SignIn(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties = null);
36+
37+
public virtual void SignOut()
38+
{
39+
SignOut(authenticationScheme: null, properties: null);
40+
}
41+
42+
public abstract void SignOut(string authenticationScheme);
43+
44+
public abstract void SignOut(string authenticationScheme, AuthenticationProperties properties);
45+
}
46+
}

src/Microsoft.AspNet.Http.Core/HttpContext.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using System.Net.WebSockets;
87
using System.Security.Claims;
98
using System.Threading;
@@ -20,6 +19,8 @@ public abstract class HttpContext : IDisposable
2019

2120
public abstract ConnectionInfo Connection { get; }
2221

22+
public abstract AuthenticationManager Authentication { get; }
23+
2324
public abstract ClaimsPrincipal User { get; set; }
2425

2526
public abstract IDictionary<object, object> Items { get; }
@@ -54,12 +55,6 @@ public virtual void SetFeature<T>(T instance)
5455
SetFeature(typeof(T), instance);
5556
}
5657

57-
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();
58-
59-
public abstract AuthenticationResult Authenticate(string authenticationScheme);
60-
61-
public abstract Task<AuthenticationResult> AuthenticateAsync(string authenticationScheme);
62-
6358
public virtual Task<WebSocket> AcceptWebSocketAsync()
6459
{
6560
return AcceptWebSocketAsync(subProtocol: null);

src/Microsoft.AspNet.Http.Core/HttpRequest.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections;
6-
using System.Collections.Generic;
74
using System.IO;
85
using System.Threading;
96
using System.Threading.Tasks;
@@ -12,8 +9,6 @@ namespace Microsoft.AspNet.Http
129
{
1310
public abstract class HttpRequest
1411
{
15-
// TODO - review IOwinRequest for properties
16-
1712
public abstract HttpContext HttpContext { get; }
1813

1914
/// <summary>

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

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.IO;
7-
using System.Security.Claims;
8-
using Microsoft.AspNet.Http.Authentication;
96

107
namespace Microsoft.AspNet.Http
118
{
129
public abstract class HttpResponse
1310
{
14-
// TODO - review IOwinResponse for completeness
15-
1611
public abstract HttpContext HttpContext { get; }
12+
1713
public abstract int StatusCode { get; set; }
14+
1815
public abstract IHeaderDictionary Headers { get; }
16+
1917
public abstract Stream Body { get; set; }
2018

2119
public abstract long? ContentLength { get; set; }
20+
2221
public abstract string ContentType { get; set; }
2322

2423
public abstract IResponseCookies Cookies { get; }
@@ -35,33 +34,5 @@ public virtual void Redirect(string location)
3534
}
3635

3736
public abstract void Redirect(string location, bool permanent);
38-
39-
public virtual void Challenge()
40-
{
41-
Challenge(properties: null, authenticationScheme: null);
42-
}
43-
44-
public virtual void Challenge(AuthenticationProperties properties)
45-
{
46-
Challenge(properties, "");
47-
}
48-
49-
public virtual void Challenge(string authenticationScheme)
50-
{
51-
Challenge(properties: null, authenticationScheme: authenticationScheme);
52-
}
53-
54-
public abstract void Challenge(AuthenticationProperties properties, string authenticationScheme);
55-
56-
public abstract void SignIn(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties = null);
57-
58-
public virtual void SignOut()
59-
{
60-
SignOut(authenticationScheme: null, properties: null);
61-
}
62-
63-
public abstract void SignOut(string authenticationScheme);
64-
65-
public abstract void SignOut(string authenticationScheme, AuthenticationProperties properties);
6637
}
6738
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Security.Claims;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNet.FeatureModel;
9+
using Microsoft.AspNet.Http.Infrastructure;
10+
using Microsoft.Framework.Internal;
11+
12+
namespace Microsoft.AspNet.Http.Authentication
13+
{
14+
public class DefaultAuthenticationManager : AuthenticationManager
15+
{
16+
private readonly IFeatureCollection _features;
17+
private FeatureReference<IHttpAuthenticationFeature> _authentication = FeatureReference<IHttpAuthenticationFeature>.Default;
18+
private FeatureReference<IHttpResponseFeature> _response = FeatureReference<IHttpResponseFeature>.Default;
19+
20+
public DefaultAuthenticationManager(IFeatureCollection features)
21+
{
22+
_features = features;
23+
}
24+
25+
private IHttpAuthenticationFeature HttpAuthenticationFeature
26+
{
27+
get { return _authentication.Fetch(_features) ?? _authentication.Update(_features, new HttpAuthenticationFeature()); }
28+
}
29+
30+
private IHttpResponseFeature HttpResponseFeature
31+
{
32+
get { return _response.Fetch(_features); }
33+
}
34+
35+
public override IEnumerable<AuthenticationDescription> GetAuthenticationSchemes()
36+
{
37+
var handler = HttpAuthenticationFeature.Handler;
38+
if (handler == null)
39+
{
40+
return new AuthenticationDescription[0];
41+
}
42+
43+
var describeContext = new DescribeSchemesContext();
44+
handler.GetDescriptions(describeContext);
45+
return describeContext.Results;
46+
}
47+
48+
public override AuthenticationResult Authenticate([NotNull] string authenticationScheme)
49+
{
50+
var handler = HttpAuthenticationFeature.Handler;
51+
52+
var authenticateContext = new AuthenticateContext(authenticationScheme);
53+
if (handler != null)
54+
{
55+
handler.Authenticate(authenticateContext);
56+
}
57+
58+
if (!authenticateContext.Accepted)
59+
{
60+
throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme);
61+
}
62+
63+
return authenticateContext.Result;
64+
}
65+
66+
public override async Task<AuthenticationResult> AuthenticateAsync([NotNull] string authenticationScheme)
67+
{
68+
var handler = HttpAuthenticationFeature.Handler;
69+
70+
var authenticateContext = new AuthenticateContext(authenticationScheme);
71+
if (handler != null)
72+
{
73+
await handler.AuthenticateAsync(authenticateContext);
74+
}
75+
76+
// Verify all types ack'd
77+
if (!authenticateContext.Accepted)
78+
{
79+
throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme);
80+
}
81+
82+
return authenticateContext.Result;
83+
}
84+
85+
public override void Challenge(AuthenticationProperties properties, string authenticationScheme)
86+
{
87+
HttpResponseFeature.StatusCode = 401;
88+
var handler = HttpAuthenticationFeature.Handler;
89+
90+
var challengeContext = new ChallengeContext(authenticationScheme, properties == null ? null : properties.Dictionary);
91+
if (handler != null)
92+
{
93+
handler.Challenge(challengeContext);
94+
}
95+
96+
if (!challengeContext.Accepted)
97+
{
98+
throw new InvalidOperationException("The following authentication type was not accepted: " + authenticationScheme);
99+
}
100+
}
101+
102+
public override void SignIn(string authenticationScheme, [NotNull] ClaimsPrincipal principal, AuthenticationProperties properties)
103+
{
104+
var handler = HttpAuthenticationFeature.Handler;
105+
106+
var signInContext = new SignInContext(authenticationScheme, principal, properties == null ? null : properties.Dictionary);
107+
if (handler != null)
108+
{
109+
handler.SignIn(signInContext);
110+
}
111+
112+
// Verify all types ack'd
113+
if (!signInContext.Accepted)
114+
{
115+
throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme);
116+
}
117+
}
118+
119+
public override void SignOut(string authenticationScheme, AuthenticationProperties properties)
120+
{
121+
var handler = HttpAuthenticationFeature.Handler;
122+
123+
var signOutContext = new SignOutContext(authenticationScheme, properties?.Dictionary);
124+
if (handler != null)
125+
{
126+
handler.SignOut(signOutContext);
127+
}
128+
129+
// Verify all types ack'd
130+
if (!string.IsNullOrWhiteSpace(authenticationScheme) && !signOutContext.Accepted)
131+
{
132+
throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme);
133+
}
134+
}
135+
136+
public override void SignOut(string authenticationScheme)
137+
{
138+
SignOut(authenticationScheme, properties: null);
139+
}
140+
}
141+
}

src/Microsoft.AspNet.Http/DefaultHttpContext.cs

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.AspNet.Http.Authentication;
1212
using Microsoft.AspNet.Http.Collections;
1313
using Microsoft.AspNet.Http.Infrastructure;
14-
using Microsoft.Framework.Internal;
1514
using Microsoft.Net.Http.Headers;
1615

1716
namespace Microsoft.AspNet.Http
@@ -23,6 +22,7 @@ public class DefaultHttpContext : HttpContext
2322
private readonly HttpRequest _request;
2423
private readonly HttpResponse _response;
2524
private readonly ConnectionInfo _connection;
25+
private readonly AuthenticationManager _authenticationManager;
2626

2727
private FeatureReference<IItemsFeature> _items;
2828
private FeatureReference<IServiceProvidersFeature> _serviceProviders;
@@ -45,6 +45,7 @@ public DefaultHttpContext(IFeatureCollection features)
4545
_request = new DefaultHttpRequest(this, features);
4646
_response = new DefaultHttpResponse(this, features);
4747
_connection = new DefaultConnectionInfo(features);
48+
_authenticationManager = new DefaultAuthenticationManager(features);
4849

4950
_items = FeatureReference<IItemsFeature>.Default;
5051
_serviceProviders = FeatureReference<IServiceProvidersFeature>.Default;
@@ -90,6 +91,8 @@ private ISessionFeature SessionFeature
9091

9192
public override ConnectionInfo Connection { get { return _connection; } }
9293

94+
public override AuthenticationManager Authentication { get { return _authenticationManager; } }
95+
9396
public override ClaimsPrincipal User
9497
{
9598
get
@@ -201,56 +204,6 @@ public override void SetFeature(Type type, object instance)
201204
_features[type] = instance;
202205
}
203206

204-
public override IEnumerable<AuthenticationDescription> GetAuthenticationSchemes()
205-
{
206-
var handler = HttpAuthenticationFeature.Handler;
207-
if (handler == null)
208-
{
209-
return new AuthenticationDescription[0];
210-
}
211-
212-
var describeContext = new DescribeSchemesContext();
213-
handler.GetDescriptions(describeContext);
214-
return describeContext.Results;
215-
}
216-
217-
public override AuthenticationResult Authenticate([NotNull] string authenticationScheme)
218-
{
219-
var handler = HttpAuthenticationFeature.Handler;
220-
221-
var authenticateContext = new AuthenticateContext(authenticationScheme);
222-
if (handler != null)
223-
{
224-
handler.Authenticate(authenticateContext);
225-
}
226-
227-
if (!authenticateContext.Accepted)
228-
{
229-
throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme);
230-
}
231-
232-
return authenticateContext.Result;
233-
}
234-
235-
public override async Task<AuthenticationResult> AuthenticateAsync([NotNull] string authenticationScheme)
236-
{
237-
var handler = HttpAuthenticationFeature.Handler;
238-
239-
var authenticateContext = new AuthenticateContext(authenticationScheme);
240-
if (handler != null)
241-
{
242-
await handler.AuthenticateAsync(authenticateContext);
243-
}
244-
245-
// Verify all types ack'd
246-
if (!authenticateContext.Accepted)
247-
{
248-
throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme);
249-
}
250-
251-
return authenticateContext.Result;
252-
}
253-
254207
public override Task<WebSocket> AcceptWebSocketAsync(string subProtocol)
255208
{
256209
var webSocketFeature = WebSocketFeature;

0 commit comments

Comments
 (0)