Security icon indicating copy to clipboard operation
Security copied to clipboard

AuthorizationOptions.DefaultPolicy

Open XplMarcel opened this issue 11 years ago • 29 comments

I'm trying to replace the DefaultPolicy with my own Policy like this:

services.AddMvcCore()
        .AddAuthorization(options => {
           options.DefaultPolicy = new AuthorizationPolicyBuilder(OAuthBearerAuthenticationDefaults.AuthenticationScheme).RequireAuthenticatedUser()
          .AddRequirements(new TenantRequirement()).Build();
        })
        .AddFormatterMappings()
        .AddJsonFormatters()
        .AddViews();

To my opinion the Authorize attribute should only use the requirements from that policy. (Correct me if i'm wrong). The TenantRequirement is used to make sure the a user has a claim for a route which uses a TenantId.

If i change the Authorize attribute to [Authorize(Roles = "Administrator")] it looks like the RolesAuthorizationRequirement is used (i get a 403 - Forbidden), which is strange considering the DefaultPolicy shouldn't have that requirement.

If i make sure the user has that specific role however, the TenantRequirement doesn't execute at all.

Any ideas ?

XplMarcel avatar Sep 29 '15 14:09 XplMarcel

I am not sure about overriding the default policy. There could be an easier way. Does your authorize handler have complex logic.

Example sample code here and highlighted on SO: https://github.com/aspnet/MusicStore/blob/master/src/MusicStore/Startup.cs#L118-L121

dcarr42 avatar Sep 29 '15 15:09 dcarr42

@dvdcrr77 I know how to add Requirements by name and use them in an Authorize attribute like [Authorize(policy = "tenant"). What i'm basicly looking for is a way to add a policy requirement with parameters, which i don't think is currently supported. For example: A user can have different roles for each tenant he belongs to. I add these tenants to the claims of the user (tenantId, name, roles). When a request comes in for a certain tenantId i have to check if the user has a tenant claim with the tenantId (from the routedata.values) with the right roles for that tenant.

So my Authorize attribute would possibly look like this: [Authorize(policy = { name: "tenant", roles: "administrator "})]

That way i could check within the given policy for the correct tenantId and tenantroles.

I hope this is clear.

XplMarcel avatar Sep 29 '15 17:09 XplMarcel

You may have to derive from https://github.com/aspnet/Security/blob/3a8ea672eae590785a78d74777abb76bb9664130/src/Microsoft.AspNet.Authorization/AuthorizeAttribute.cs

or ,maybe look at some of the examples provided by https://github.com/blowdart

dcarr42 avatar Sep 29 '15 18:09 dcarr42

@dvdcrr77 I think in this case that would be the only possible solution yeah. Maybe it would be a good idea for the future to support parameters on policies in Authorize attributes instead of only a policy name. Then you don't have to introduce a custom authorize attribute and Authorization policies can simply be configured on startup.

XplMarcel avatar Sep 29 '15 18:09 XplMarcel

@XplMarcel Your right of course regards your original question. It promptly ignores the default policy under certain conditions and rightly so :-).

https://github.com/aspnet/Security/blob/3294de14f43108f11af682b4d30090a131be0d71/src/Microsoft.AspNet.Authorization/AuthorizationPolicy.cs#L73-L76

dcarr42 avatar Sep 29 '15 19:09 dcarr42

Personally i think this is a bug. In my opinion a developer should be able to define his own Authorization policy (DefaultPolicy) using the Requirements he wants. All those requirements should be handled unless 1 of them fails. I removed the DenyAnonymousAuthorizationRequirement, but when an Authorize attribute has Roles the rest of the requirements or simply ignored (even when the role requirement succeeds).

XplMarcel avatar Sep 30 '15 07:09 XplMarcel

Yes this could be construed as a bug. But you can combine policies so there should be a work around.

dcarr42 avatar Sep 30 '15 10:09 dcarr42

Eventually i solved the problem by deriving (like you proposed) from AuthorizeAttribute (basicly reintroducing it within my own namespace.) That way i can simply use the Authorize attribute with extended properties and still take advantage of the AuthorizationPolicies.

The only problem was to get the custom properties from the (new) Authorize attribute, but an Extensionmethod on the AuthorizationContext solved that pretty easily.

So now i can simply use [Authorize(TenantRoles = "Administrator")] on my controllers and my TenantRequirement gets handled automatically.

XplMarcel avatar Sep 30 '15 13:09 XplMarcel

@XplMarcel Very good but doesn't feel quite right. You should label this issue it might get picked up by someone.

dcarr42 avatar Sep 30 '15 13:09 dcarr42

@dvdcrr77 I totally agree, but i didn't want to reinvent/rewrite the Authorization Middleware to suit my needs. ps.. i have no idea how to label an issue here.

XplMarcel avatar Sep 30 '15 13:09 XplMarcel

So can you summarize the issue? Is it that you expect the default policy to be applied even when specifying roles? The current logic basically only applies the default policy if neither the Roles nor the Policy name is specified.

HaoK avatar Sep 30 '15 23:09 HaoK

@HaoK I was under the assumption that i could override the DefaultPolicy with the Requirements i need.

  services.AddMvcCore()
    .AddAuthorization(options =>
    {
      options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().AddRequirements(new TenantRequirement()).Build();
    })
    .AddFormatterMappings()
    .AddJsonFormatters()
    .AddViews();

In this case i would expect the Authorize attribute to only handle the DenyAnonymousAuthorizationRequirement and my TenantRequirement. As soon as i use Roles = in an Authorize attribute the DefaultAuthorizationPolicy adds the RoleRequirement automatically which isn't what i would expect since i didn't tell the PolicyBuilder to add the RoleRequirement. If the RoleRequirement is added, my TenantRequirement never gets handled.

On top of that i would like to add a custom RoleRequirement, so Roles = would be handled by my custom RoleRequirement instead of the default one.

A user in our system can have permissions on 1 or multiple tenants with different roles per tenant. So the ClaimsIdentity has a Claim for those Tenants with the TenantId and Roles.

When a user does a request to our Api for a specific Tenant i have to check if the user has permissions on that tenant and has the correct roles for that Tenant.

I hope this is clear enough.

XplMarcel avatar Oct 01 '15 10:10 XplMarcel

@blowdart thoughts?

HaoK avatar Oct 02 '15 23:10 HaoK

Interesting.

If you override the default policy would users expect to be applied everywhere, as it's the default? Even where there's no authorize attribute?

I wonder if by exposing the request in policy you could do the tenant check (assuming the tenant is in the path)

blowdart avatar Oct 02 '15 23:10 blowdart

Yeah so the current behavior is more of a fallback policy (where if nothing else is specified, we use the default policy)

HaoK avatar Oct 02 '15 23:10 HaoK

@blowdart That's what i was thinking with regards to the default policy, but i don't think requirements added to the default policy should be hard-coded in case developers want to use their own requirements. Or make it at least make possible to override the default policy during startup. If (options.DefaultPolicy == null) { AddDefaultPolicy(); } would be the easiest fix i guess.

I can already do the tenant check using the AuthorizationContext in the Requirement.Handle

public static string GetTenantIdFromRequest(this AuthorizationContext context)
{
  var controllerContext = context.Resource as Microsoft.AspNet.Mvc.AuthorizationContext;
  return controllerContext?.RouteData?.Values["tenantId"]?.ToString();
}

XplMarcel avatar Oct 05 '15 07:10 XplMarcel

@blowdart Are we happy with the current behavior or are we considering any changes?

HaoK avatar Oct 15 '15 23:10 HaoK

I have no opinion either way, but judging by your workload, this should probably go on the backlog to be reexamined after rtm :)

blowdart avatar Oct 15 '15 23:10 blowdart

Sold! Moving to backlog for now

HaoK avatar Oct 15 '15 23:10 HaoK

So I've been assuming that when I set a default policy that it would be used when I didn't have any authorize attributes used... But This doesn't seem to be this case is this correct? I've been having lots of authorization issues.


                options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireClaim(ClaimTypes.Role, AuthorizationRoles.User).Build();
                options.AddPolicy(AuthorizationRoles.ClientPolicy, policy => policy.RequireClaim(ClaimTypes.Role, AuthorizationRoles.Client));
                options.AddPolicy(AuthorizationRoles.UserPolicy, policy => policy.RequireClaim(ClaimTypes.Role, AuthorizationRoles.User));
                options.AddPolicy(AuthorizationRoles.GlobalAdminPolicy, policy => policy.RequireClaim(ClaimTypes.Role, AuthorizationRoles.GlobalAdmin));

Basically was understanding that I could set user role requirement for every end point and then I could override each one specifically (much cleaner). @blowdart any way to make this happen or am I doing something wrong? https://github.com/exceptionless/exceptionless/tree/feature/core

niemyjski avatar Nov 07 '17 14:11 niemyjski