Skip to content

Commit ff4d050

Browse files
committed
Fixed policy cache to handle metadata
1 parent 17c96a8 commit ff4d050

File tree

3 files changed

+3
-150
lines changed

3 files changed

+3
-150
lines changed

src/HotChocolate/AspNetCore/src/AspNetCore.Authorization/AuthorizationPolicyCache.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@ namespace HotChocolate.AspNetCore.Authorization;
66

77
internal sealed class AuthorizationPolicyCache
88
{
9-
private readonly ConcurrentDictionary<string, AuthorizationPolicy> _cache = new();
9+
private readonly ConcurrentDictionary<AuthorizeDirective, AuthorizationPolicy> _cache = new();
1010

1111
public AuthorizationPolicy? LookupPolicy(AuthorizeDirective directive)
1212
{
13-
var cacheKey = directive.GetPolicyCacheKey();
14-
15-
return _cache.GetValueOrDefault(cacheKey);
13+
return _cache.GetValueOrDefault(directive);
1614
}
1715

1816
public void CachePolicy(AuthorizeDirective directive, AuthorizationPolicy policy)
1917
{
20-
var cacheKey = directive.GetPolicyCacheKey();
21-
22-
_cache.TryAdd(cacheKey, policy);
18+
_cache.TryAdd(directive, policy);
2319
}
2420
}

src/HotChocolate/AspNetCore/test/AspNetCore.Authorization.Tests/AuthorizeDirectiveTests.cs

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -80,105 +80,6 @@ public void CreateInstance_Roles_RolesHasItems()
8080
t => Assert.Equal("b", t));
8181
}
8282

83-
[Fact]
84-
public void CacheKey_Policy_NoRoles()
85-
{
86-
// arrange
87-
var authorizeDirective = new AuthorizeDirective(
88-
policy: "policy");
89-
90-
// act
91-
var cacheKey = authorizeDirective.GetPolicyCacheKey();
92-
93-
// assert
94-
Assert.Equal("policy;", cacheKey);
95-
}
96-
97-
[Fact]
98-
public void CacheKey_NoPolicy_Roles()
99-
{
100-
// arrange
101-
var authorizeDirective = new AuthorizeDirective(
102-
policy: null,
103-
roles: ["a", "b"]);
104-
105-
// act
106-
var cacheKey = authorizeDirective.GetPolicyCacheKey();
107-
108-
// assert
109-
Assert.Equal(";a,b", cacheKey);
110-
}
111-
112-
[Fact]
113-
public void CacheKey_Policy_And_Roles()
114-
{
115-
// arrange
116-
var authorizeDirective = new AuthorizeDirective(
117-
policy: "policy",
118-
roles: ["a", "b"]);
119-
120-
// act
121-
var cacheKey = authorizeDirective.GetPolicyCacheKey();
122-
123-
// assert
124-
Assert.Equal("policy;a,b", cacheKey);
125-
}
126-
127-
[Fact]
128-
public void CacheKey_NoPolicy_NoRoles()
129-
{
130-
// arrange
131-
var authorizeDirective = new AuthorizeDirective(
132-
policy: null,
133-
roles: null);
134-
135-
// act
136-
var cacheKey = authorizeDirective.GetPolicyCacheKey();
137-
138-
// assert
139-
Assert.Equal("", cacheKey);
140-
}
141-
142-
[Fact]
143-
public void CacheKey_Policy_And_Role_Naming_Does_Not_Conflict()
144-
{
145-
// arrange
146-
var authorizeDirective1 = new AuthorizeDirective(
147-
policy: "policy",
148-
roles: null);
149-
150-
var authorizeDirective2 = new AuthorizeDirective(
151-
policy: null,
152-
roles: ["policy"]);
153-
154-
// act
155-
var cacheKey1 = authorizeDirective1.GetPolicyCacheKey();
156-
var cacheKey2 = authorizeDirective2.GetPolicyCacheKey();
157-
158-
// assert
159-
Assert.NotEqual(cacheKey1, cacheKey2);
160-
}
161-
162-
[Fact]
163-
public void CacheKey_Same_Roles_Albeit_Sorted_Differently_Have_Same_Cache_Key()
164-
{
165-
// arrange
166-
var authorizeDirective1 = new AuthorizeDirective(
167-
policy: null,
168-
roles: ["a", "c", "b"]);
169-
170-
var authorizeDirective2 = new AuthorizeDirective(
171-
policy: null,
172-
roles: ["c", "b", "a"]);
173-
174-
// act
175-
var cacheKey1 = authorizeDirective1.GetPolicyCacheKey();
176-
var cacheKey2 = authorizeDirective2.GetPolicyCacheKey();
177-
178-
// assert
179-
Assert.Equal(cacheKey1, cacheKey2);
180-
}
181-
18283
[Fact]
18384
public void TypeAuth_DefaultPolicy()
18485
{

src/HotChocolate/Core/src/Authorization/AuthorizeDirective.cs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
using System.Text;
2-
31
namespace HotChocolate.Authorization;
42

53
/// <summary>
64
/// The authorize directive.
75
/// </summary>
86
public sealed class AuthorizeDirective
97
{
10-
private readonly string _cacheKey;
11-
128
/// <summary>
139
/// Initializes a new instance of <see cref="AuthorizeDirective"/>.
1410
/// </summary>
@@ -57,8 +53,6 @@ public AuthorizeDirective(
5753
Roles = roles?.OrderBy(r => r).ToList();
5854
Apply = apply;
5955
Metadata = metadata;
60-
61-
_cacheKey = BuildCacheKey(Policy, Roles);
6256
}
6357

6458
/// <summary>
@@ -88,42 +82,4 @@ public AuthorizeDirective(
8882
/// Gets the metadata that can be used for authorization.
8983
/// </summary>
9084
public IReadOnlyList<object>? Metadata { get; }
91-
92-
/// <summary>
93-
/// Gets a cache key that uniquely identifies the combined authorization policy,
94-
/// of the specified <see cref="Roles"/> and <see cref="Policy"/>.
95-
/// </summary>
96-
internal string GetPolicyCacheKey() => _cacheKey;
97-
98-
private static string BuildCacheKey(string? policy, IReadOnlyList<string>? roles)
99-
{
100-
if (string.IsNullOrEmpty(policy) && roles is null)
101-
{
102-
return string.Empty;
103-
}
104-
105-
var sb = new StringBuilder();
106-
107-
if (!string.IsNullOrEmpty(policy))
108-
{
109-
sb.Append(policy);
110-
}
111-
112-
sb.Append(";");
113-
114-
if (roles is not null)
115-
{
116-
for (var i = 0; i < roles.Count; i++)
117-
{
118-
sb.Append(roles[i]);
119-
120-
if (i < roles.Count - 1)
121-
{
122-
sb.Append(",");
123-
}
124-
}
125-
}
126-
127-
return sb.ToString();
128-
}
12985
}

0 commit comments

Comments
 (0)