Skip to content
This repository has been archived by the owner on Mar 19, 2019. It is now read-only.

HttpSys => Auth 2.0 #354

Closed
wants to merge 8 commits into from
Closed

HttpSys => Auth 2.0 #354

wants to merge 8 commits into from

Conversation

HaoK
Copy link
Member

@HaoK HaoK commented Apr 26, 2017

@Tratcher replaces: #325

Something weird is still going on where the challenge functional tests only fail on core, but pass on 46.

@HaoK HaoK mentioned this pull request Apr 26, 2017
var auth = authentication.FirstOrDefault();
if (auth == null)
{
throw new InvalidOperationException("AddAuthentication() is required to use Authentication.");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this no-op instead of throw?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting one. we know auth is on, and we have the user set on HttpContext.User. You don't really need the AuthN service unless you're mixing windows and anonymous access. How about throwing if AuthenticationSchemes != None && AllowAnonymous? That way you don't need the AuthN service for an app that handles AuthZ at the server level.

get { return _authHandler; }
set { _authHandler = value; }
}
[Obsolete("See https://go.microsoft.com/fwlink/?linkid=845470")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The obsolete is a bit overkill for an internal property that's implementing an obsolete interface property.

var auth = authentication.FirstOrDefault();
if (auth == null)
{
throw new InvalidOperationException("AddAuthentication() is required to use Authentication.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting one. we know auth is on, and we have the user set on HttpContext.User. You don't really need the AuthN service unless you're mixing windows and anonymous access. How about throwing if AuthenticationSchemes != None && AllowAnonymous? That way you don't need the AuthN service for an app that handles AuthZ at the server level.

@@ -26,8 +26,7 @@ public class AuthenticationTests
[InlineData(AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic)]
public async Task AuthTypes_AllowAnonymous_NoChallenge(AuthenticationSchemes authType)
{
string address;
using (Utilities.CreateHttpAuthServer(authType, AllowAnoymous, out address, httpContext =>
using (var server = Utilities.CreateDynamicHost(string.Empty, authType, AllowAnoymous, out var address, out var baseAddress, httpContext =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an overload that doesn't return baseAddress? You're not using it in many (any?) of these tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, removed, also hardcoded the string.Empty basePath in this new overload since it wasn't being set anywhere either.

@@ -316,20 +243,16 @@ public class AuthenticationTests
// [InlineData(AuthenticationSchemes.Digest)]
[InlineData(AuthenticationSchemes.Basic)]
[InlineData(AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic)]
[FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")]
//[FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

{
context.NotAuthenticated();
return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(_requestContext.User, properties: null, authenticationScheme: _scheme.Name)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This says the scheme is "Negotiate", but you've registered "Windows". With a combine handler you can skip ListEnabledAuthSchemes and just always return the authenticated user.

var identity = (ClaimsIdentity)_requestContext.User?.Identity;
if (identity != null && identity.IsAuthenticated
&& (automaticChallenge || string.Equals(identity.AuthenticationType, context.AuthenticationScheme, StringComparison.Ordinal)))
_customChallenges |= scheme;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_customChallenges = _authSchemes, you don't have any granularity now.

var identity = (ClaimsIdentity)_requestContext.User?.Identity;
if (identity != null && identity.IsAuthenticated)
{
foreach (var scheme in ListEnabledAuthSchemes())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this, it will always find a match.

else
{
_requestContext.Response.StatusCode = 401;
foreach (var scheme in ListEnabledAuthSchemes())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

{ "AuthenticationScheme", authenticationScheme },
};
}

private IEnumerable<AuthenticationSchemes> ListEnabledAuthSchemes()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need ListEnabledAuthSchemes anywhere now that you only have one handler for all schemes.

@HaoK
Copy link
Member Author

HaoK commented May 1, 2017

The auth handler is actually pretty clean now

context.NotAuthenticated();
}
}
return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(_requestContext.User, properties: null, authenticationScheme: _scheme.Name)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since HttpSys pre-sets HttpContext.User, will AuthZ try to merge this Principal with itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging only happens when multiple schemes are requested for a policy, it doesn't actually merge with anything that already is on context.User either.

@@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Core" Version="$(AspNetCoreVersion)" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Core and not Abstractions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moot now that we are going to always AddAuthenticationCore

@HaoK
Copy link
Member Author

HaoK commented May 15, 2017

Updated, tests are passing (but quite a few are skipped due to the HttpHandler issue

Copy link
Member

@Tratcher Tratcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests need a little cleanup.

@@ -258,23 +195,14 @@ public class AuthenticationTests
[InlineData(AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic)]
public async Task AuthTypes_AuthenticateWithUser_OneResult(AuthenticationSchemes authType)
{
string address;
var authTypeList = authType.ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

authTypeList is no longer used

@@ -314,17 +241,13 @@ public class AuthenticationTests
[InlineData(AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic)]
public async Task AuthTypes_ChallengeWithAllAuthTypes_AllChallengesSent(AuthenticationSchemes authType)
{
string address;
var authTypeList = authType.ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

authTypeList is no longer used

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one we still use to check the count.

@@ -340,45 +263,44 @@ public class AuthenticationTests
[InlineData(AuthenticationSchemes.Basic)]
public async Task AuthTypes_ChallengeOneAuthType_OneChallengeSent(AuthenticationSchemes authType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input param is no longer used

@@ -19,7 +20,7 @@ public void OverridingDirectConfigurationWithIServerAddressesFeatureSucceeds()
var serverAddress = "http://localhost:11001/";
var overrideAddress = "http://localhost:11002/";

using (var server = new MessagePump(Options.Create(new HttpSysOptions()), new LoggerFactory()))
using (var server = new MessagePump(Options.Create(new HttpSysOptions()), new LoggerFactory(), new AuthenticationSchemeProvider(Options.Create(new AuthenticationOptions()))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factor out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, moved into Utilities.CreatePump static

@HaoK
Copy link
Member Author

HaoK commented May 17, 2017

bff13c7

@HaoK HaoK closed this May 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants