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

Adding Auth to endpoints #241

Merged
merged 1 commit into from
Mar 21, 2017
Merged

Adding Auth to endpoints #241

merged 1 commit into from
Mar 21, 2017

Conversation

BrennanConroy
Copy link
Member

@BrennanConroy BrennanConroy commented Feb 26, 2017

This PR is mainly to see what the current Auth progress is and if it is heading in the right direction and also to get input on design. Built on top of #240. Try to ignore most of the weird stuff in the samples.

@moozzyk @davidfowl @anurse @mikaelm12

@dnfclas
Copy link

dnfclas commented Feb 26, 2017

@BrennanConroy,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by .NET Foundation. We will now review your pull request.
Thanks,
.NET Foundation Pull Request Bot

{
Context.Connection.Dispose();
}
//if (!Context.User.Identity.IsAuthenticated)
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove?

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace ChatSample
{
public class req : IAuthorizationRequirement
Copy link
Contributor

Choose a reason for hiding this comment

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

Change this class name

public class req : IAuthorizationRequirement
{
}
public class reqHandler : AuthorizationHandler<req>
Copy link
Contributor

Choose a reason for hiding this comment

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

This one too

@@ -54,8 +71,13 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<ISmsSender, AuthMessageSender>();

services.AddSignalR();
services.AddEndPoint<HubEndPoint<Chat>>(options =>
Copy link
Contributor

Choose a reason for hiding this comment

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

new line here?

using Microsoft.Extensions.Primitives;
using Microsoft.Extensions.Internal;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Sort this last using

Copy link
Contributor

@analogrelay analogrelay left a comment

Choose a reason for hiding this comment

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

Looking good so far. Bring @HaoK in to look at the authentication process. We're trying to mimic MVC here, but with EndPoints instead of Controllers/Actions as the authorized resource.

{
public class EndPointOptions<TEndPoint> where TEndPoint : EndPoint
{
public AuthorizationPolicy Policy { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to take the actual policy or a name to look up? I know AuthorizeAttribute uses a name, but then again it has to because it's an Attribute.

Copy link
Member

Choose a reason for hiding this comment

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

You probably want to take the name, as that would enable scenarios where the policy is resolved dynamically by name which would be blocked if you took the policy instance always.

@@ -48,6 +58,59 @@ public HttpConnectionDispatcher(ConnectionManager manager, ILoggerFactory logger
}
}

private async Task<bool> AuthorizeAsync<TEndPoint>(HttpContext context) where TEndPoint : EndPoint
Copy link
Member

Choose a reason for hiding this comment

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

Is this pretty much an exact copy of the AuthorizeFilter logic? As apart of Auth 2.0 I'll try to consolidate this logic somewhere that MVC + SignalR can consume rather than duplicate...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah its from AuthorizeFilter, a common spot for this would be great!

Copy link
Member

Choose a reason for hiding this comment

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

/cc @javiercn

Copy link
Member

Choose a reason for hiding this comment

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

Agree. We should have live somewhere where both MVC and Signal R can consume

Copy link
Member

Choose a reason for hiding this comment

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

So currently AuthZ doesn't depend on HttpAbstractions, assuming we want to keep that clean, we will need to add a new package for this helper in Security... maybe Microsoft.AspNetCore.Authorization.Http?

Copy link
Member

Choose a reason for hiding this comment

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

Or is this more appropriate as a shared common code i.e. AuthorizationHelper similar to SecurityHelper (or we could just add this as a new method in SecurityHelper too)...

Copy link
Member

Choose a reason for hiding this comment

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

I would make a package like Microsoft.AspNetCore.Authentication.Policy and I would offer a service
IPolicyAuthenticationProvider {
Task AuthenticateAsync(HttpContext context, string policyName);
}

It's implementation will do what the AuthorizeFilter does before setting the principal into the context. This has two benefits:

  • You can reuse this logic across implementations (MVC, SignalR, any other middleware).
  • You can get access to a principal that matches exactly what that policy defines at any point where you have access to the HttpContext.

Copy link
Member

Choose a reason for hiding this comment

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

Okay I'll start a prototype/design PR for the rough shape of something this week

Copy link
Member

Choose a reason for hiding this comment

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

Something like aspnet/Security#1148 look reasonable? It basically splits things into the authentication method, and authorization method (eventually this might have logic to distinguish between challenge/forbidden)

@BrennanConroy BrennanConroy force-pushed the brecon/endpoint_auth branch 3 times, most recently from faa3930 to c1f3845 Compare March 1, 2017 17:21
@davidfowl
Copy link
Member

@BrennanConroy What's the deal with this PR?

@BrennanConroy BrennanConroy force-pushed the brecon/endpoint_auth branch 2 times, most recently from f6bea88 to c86a3b4 Compare March 20, 2017 16:14
@BrennanConroy BrennanConroy changed the title [WIP] Adding Auth to endpoints Adding Auth to endpoints Mar 20, 2017
@davidfowl
Copy link
Member

@BrennanConroy rebsae

@@ -51,6 +60,58 @@ public HttpConnectionDispatcher(ConnectionManager manager, ILoggerFactory logger
}
}

private async Task<bool> AuthorizeAsync<TEndPoint>(HttpContext context) where TEndPoint : EndPoint
Copy link
Member

Choose a reason for hiding this comment

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

Move this code to a helper

@BrennanConroy
Copy link
Member Author

🆙 📅

private Task<bool> AuthorizeAsync<TEndPoint>(HttpContext context) where TEndPoint : EndPoint
{
// TODO: Authorize attribute on EndPoint
var options = context.RequestServices.GetRequiredService<IOptions<EndPointOptions<TEndPoint>>>();
Copy link
Member

Choose a reason for hiding this comment

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

Why are you resolving options twice?

Copy link
Contributor

@analogrelay analogrelay left a comment

Choose a reason for hiding this comment

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

Small notes and approval is subject to build passing

var options = context.RequestServices.GetRequiredService<IOptions<EndPointOptions<TEndPoint>>>().Value;
// TODO: Authorize attribute on EndPoint
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a bug for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

{
public static class AuthorizeHelper
{
public static async Task<bool> AuthorizeAsync(HttpContext context, AuthorizationPolicy policy)
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that we complete synchronously if policy is null, perhaps this is a place for ValueTask<bool>? C# 7 lets you drop it in place and continue using async. It's not a super high-performance code path, but it seems useful for the relatively common case of not having an auth policy.

@davidfowl thoughts on more ValueTasks? Premature optimization? Other unknown costs?

Copy link
Member

Choose a reason for hiding this comment

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

Do it in a follow up commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

If there's going to be a new commit to remove the jasmine crap, may as well throw it in here, it's a small change (unless ValueTask isn't available in our current package set, then I agree, just get it in).

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -0,0 +1,152 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this supposed to be checked in? If not, can we gitignore it?

Copy link
Member

Choose a reason for hiding this comment

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

It totally confused me.

@BrennanConroy BrennanConroy merged commit 8f9f6bc into dev Mar 21, 2017
@BrennanConroy BrennanConroy deleted the brecon/endpoint_auth branch March 21, 2017 17:25
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.

7 participants