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

Commit 08ddbe8

Browse files
committed
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)
1 parent 93deb0b commit 08ddbe8

File tree

10 files changed

+27
-43
lines changed

10 files changed

+27
-43
lines changed
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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;
45
using System.Collections.Generic;
56
using System.Security.Claims;
67
using Microsoft.AspNet.Http.Authentication;
@@ -10,38 +11,33 @@ namespace Microsoft.AspNet.Http.Core.Authentication
1011
{
1112
public class AuthenticateContext : IAuthenticateContext
1213
{
13-
private List<AuthenticationResult> _results;
14-
private List<string> _accepted;
14+
private AuthenticationResult _result;
15+
private bool _accepted;
1516

16-
public AuthenticateContext([NotNull] IEnumerable<string> authenticationSchemes)
17+
public AuthenticateContext([NotNull] string authenticationScheme)
1718
{
18-
AuthenticationSchemes = authenticationSchemes;
19-
_results = new List<AuthenticationResult>();
20-
_accepted = new List<string>();
19+
AuthenticationScheme = authenticationScheme;
2120
}
2221

23-
public IEnumerable<string> AuthenticationSchemes { get; private set; }
22+
public string AuthenticationScheme { get; private set; }
2423

25-
public IEnumerable<AuthenticationResult> Results
26-
{
27-
get { return _results; }
28-
}
24+
public AuthenticationResult Result { get; set; }
2925

30-
public IEnumerable<string> Accepted
26+
public bool Accepted
3127
{
3228
get { return _accepted; }
3329
}
3430

3531
public void Authenticated(ClaimsPrincipal principal, IDictionary<string, string> properties, IDictionary<string, object> description)
3632
{
3733
var descrip = new AuthenticationDescription(description);
38-
_accepted.Add(descrip.AuthenticationScheme); // may not match identity.AuthType
39-
_results.Add(new AuthenticationResult(principal, new AuthenticationProperties(properties), descrip));
34+
_accepted = true;
35+
Result = new AuthenticationResult(principal, new AuthenticationProperties(properties), descrip);
4036
}
4137

42-
public void NotAuthenticated(string authenticationScheme, IDictionary<string, string> properties, IDictionary<string, object> description)
38+
public void NotAuthenticated()
4339
{
44-
_accepted.Add(authenticationScheme);
40+
_accepted = true;
4541
}
4642
}
4743
}

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,44 +212,41 @@ public override IEnumerable<AuthenticationDescription> GetAuthenticationSchemes(
212212
return describeContext.Results;
213213
}
214214

215-
public override IEnumerable<AuthenticationResult> Authenticate([NotNull] IEnumerable<string> authenticationSchemes)
215+
public override AuthenticationResult Authenticate([NotNull] string authenticationScheme)
216216
{
217217
var handler = HttpAuthenticationFeature.Handler;
218218

219-
var authenticateContext = new AuthenticateContext(authenticationSchemes);
219+
var authenticateContext = new AuthenticateContext(authenticationScheme);
220220
if (handler != null)
221221
{
222222
handler.Authenticate(authenticateContext);
223223
}
224224

225-
// Verify all types ack'd
226-
IEnumerable<string> leftovers = authenticationSchemes.Except(authenticateContext.Accepted);
227-
if (leftovers.Any())
225+
if (!authenticateContext.Accepted)
228226
{
229-
throw new InvalidOperationException("The following authentication schemes were not accepted: " + string.Join(", ", leftovers));
227+
throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme);
230228
}
231229

232-
return authenticateContext.Results;
230+
return authenticateContext.Result;
233231
}
234232

235-
public override async Task<IEnumerable<AuthenticationResult>> AuthenticateAsync([NotNull] IEnumerable<string> authenticationSchemes)
233+
public override async Task<AuthenticationResult> AuthenticateAsync([NotNull] string authenticationScheme)
236234
{
237235
var handler = HttpAuthenticationFeature.Handler;
238236

239-
var authenticateContext = new AuthenticateContext(authenticationSchemes);
237+
var authenticateContext = new AuthenticateContext(authenticationScheme);
240238
if (handler != null)
241239
{
242240
await handler.AuthenticateAsync(authenticateContext);
243241
}
244242

245243
// Verify all types ack'd
246-
IEnumerable<string> leftovers = authenticationSchemes.Except(authenticateContext.Accepted);
247-
if (leftovers.Any())
244+
if (!authenticateContext.Accepted)
248245
{
249-
throw new InvalidOperationException("The following authentication schemes were not accepted: " + string.Join(", ", leftovers));
246+
throw new InvalidOperationException("The following authentication scheme was not accepted: " + authenticationScheme);
250247
}
251248

252-
return authenticateContext.Results;
249+
return authenticateContext.Result;
253250
}
254251

255252
public override Task<WebSocket> AcceptWebSocketAsync(string subProtocol)

src/Microsoft.AspNet.Http.Interfaces/Security/IAuthenticateContext.cs renamed to src/Microsoft.AspNet.Http.Interfaces/Authentication/IAuthenticateContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace Microsoft.AspNet.Http.Authentication
88
{
99
public interface IAuthenticateContext
1010
{
11-
IEnumerable<string> AuthenticationSchemes { get; }
11+
string AuthenticationScheme { get; }
1212

1313
void Authenticated(ClaimsPrincipal principal, IDictionary<string, string> properties, IDictionary<string, object> description);
1414

15-
void NotAuthenticated(string authenticationScheme, IDictionary<string, string> properties, IDictionary<string, object> description);
15+
void NotAuthenticated();
1616
}
1717
}

src/Microsoft.AspNet.Http.Interfaces/Security/IHttpAuthenticationFeature.cs renamed to src/Microsoft.AspNet.Http.Interfaces/Authentication/IHttpAuthenticationFeature.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Microsoft.AspNet.Http.Authentication
88
public interface IHttpAuthenticationFeature
99
{
1010
ClaimsPrincipal User { get; set; }
11+
1112
IAuthenticationHandler Handler { get; set; }
1213
}
1314
}

src/Microsoft.AspNet.Http/HttpContext.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,9 @@ public virtual void SetFeature<T>(T instance)
5454

5555
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();
5656

57-
public virtual AuthenticationResult Authenticate(string authenticationScheme)
58-
{
59-
return Authenticate(new[] { authenticationScheme }).SingleOrDefault();
60-
}
61-
62-
public abstract IEnumerable<AuthenticationResult> Authenticate(IEnumerable<string> authenticationSchemes);
63-
64-
public virtual async Task<AuthenticationResult> AuthenticateAsync(string authenticationScheme)
65-
{
66-
return (await AuthenticateAsync(new[] { authenticationScheme })).SingleOrDefault();
67-
}
57+
public abstract AuthenticationResult Authenticate(string authenticationScheme);
6858

69-
public abstract Task<IEnumerable<AuthenticationResult>> AuthenticateAsync(IEnumerable<string> authenticationSchemes);
59+
public abstract Task<AuthenticationResult> AuthenticateAsync(string authenticationScheme);
7060

7161
public virtual Task<WebSocket> AcceptWebSocketAsync()
7262
{

0 commit comments

Comments
 (0)