-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Align authorization behavior to regular ASP.NET Core (#7408)
- Loading branch information
1 parent
e98b218
commit 4003717
Showing
18 changed files
with
532 additions
and
116 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/HotChocolate/AspNetCore/src/AspNetCore.Authorization/AuthorizationPolicyCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Collections.Concurrent; | ||
using HotChocolate.Authorization; | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
internal sealed class AuthorizationPolicyCache(IAuthorizationPolicyProvider policyProvider) | ||
{ | ||
private readonly ConcurrentDictionary<string, Task<AuthorizationPolicy>> _cache = new(); | ||
|
||
public Task<AuthorizationPolicy> GetOrCreatePolicyAsync(AuthorizeDirective directive) | ||
{ | ||
var cacheKey = directive.GetPolicyCacheKey(); | ||
|
||
return _cache.GetOrAdd(cacheKey, _ => BuildAuthorizationPolicy(directive.Policy, directive.Roles)); | ||
} | ||
|
||
private async Task<AuthorizationPolicy> BuildAuthorizationPolicy( | ||
string? policyName, | ||
IReadOnlyList<string>? roles) | ||
{ | ||
var policyBuilder = new AuthorizationPolicyBuilder(); | ||
|
||
if (!string.IsNullOrWhiteSpace(policyName)) | ||
{ | ||
var policy = await policyProvider.GetPolicyAsync(policyName).ConfigureAwait(false); | ||
|
||
if (policy is not null) | ||
{ | ||
policyBuilder = policyBuilder.Combine(policy); | ||
} | ||
else | ||
{ | ||
throw new MissingAuthorizationPolicyException(policyName); | ||
} | ||
} | ||
else | ||
{ | ||
var defaultPolicy = await policyProvider.GetDefaultPolicyAsync().ConfigureAwait(false); | ||
|
||
policyBuilder = policyBuilder.Combine(defaultPolicy); | ||
} | ||
|
||
if (roles is not null) | ||
{ | ||
policyBuilder = policyBuilder.RequireRole(roles); | ||
} | ||
|
||
return policyBuilder.Build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...tChocolate/AspNetCore/src/AspNetCore.Authorization/MissingAuthorizationPolicyException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
internal sealed class MissingAuthorizationPolicyException(string policyName) | ||
: Exception($"The policy `{policyName}` does not exist.") | ||
{ | ||
public string PolicyName { get; } = policyName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.