Skip to content

Commit

Permalink
Updated how we handle auth
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Apr 6, 2020
1 parent c37c8bd commit 2e8b7b2
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Server/AspNetCore.Authorization/AuthorizeMiddleware.cs
Expand Up @@ -29,8 +29,7 @@ public AuthorizeMiddleware(FieldDelegate next)

public async Task InvokeAsync(IDirectiveContext context)
{
AuthorizeDirective directive = context.Directive
.ToObject<AuthorizeDirective>();
AuthorizeDirective directive = context.Directive.ToObject<AuthorizeDirective>();

ClaimsPrincipal principal = null;
var allowed = false;
Expand All @@ -41,8 +40,19 @@ public async Task InvokeAsync(IDirectiveContext context)
&& o is ClaimsPrincipal p)
{
principal = p;
authenticated = allowed =
p.Identities.Any(t => t.IsAuthenticated);

#if !ASPNETCLASSIC
if (NeedsPolicyValidation(directive))
{
authenticated = allowed = true;
}
else
{
authenticated = allowed = p.Identities.Any(t => t.IsAuthenticated);
}
#else
authenticated = allowed = p.Identities.Any(t => t.IsAuthenticated);
#endif
}

allowed = allowed && IsInAnyRole(principal, directive.Roles);
Expand Down Expand Up @@ -94,11 +104,9 @@ public async Task InvokeAsync(IDirectiveContext context)
}
#if !ASPNETCLASSIC

private static bool NeedsPolicyValidation(
AuthorizeDirective directive)
private static bool NeedsPolicyValidation(AuthorizeDirective directive)
{
return directive.Roles.Count == 0
|| !string.IsNullOrEmpty(directive.Policy);
return directive.Roles.Count == 0 || !string.IsNullOrEmpty(directive.Policy);
}

private static async Task<bool> AuthorizeWithPolicyAsync(
Expand Down

0 comments on commit 2e8b7b2

Please sign in to comment.