-
-
Notifications
You must be signed in to change notification settings - Fork 786
Support IAuthorizationRequirementData in the implementation-first approach #8303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sunghwan2789
wants to merge
10
commits into
main
Choose a base branch
from
sb/feat/authz-req-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
100ab61
Added metadata to support IAuthorizationRequirementData
sunghwan2789 73293bd
Add tests
sunghwan2789 17c96a8
Add more tests
sunghwan2789 ff4d050
Fixed policy cache to handle metadata
sunghwan2789 7db173c
add req-data instead of reqs
sunghwan2789 309ec80
Merge branch 'main' into sb/feat/authz-req-data
sunghwan2789 c08086a
Merge branch 'main' into sb/feat/authz-req-data
sunghwan2789 1efcdb7
Removed trailing commas
sunghwan2789 09237f5
Merge branch 'main' into sb/feat/authz-req-data
sunghwan2789 61f429a
Merge branch 'main' into sb/feat/authz-req-data
sunghwan2789 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
222 changes: 222 additions & 0 deletions
222
...olate/AspNetCore/test/AspNetCore.Authorization.Tests/AuthorizationRequirementDataTests.cs
This file contains hidden or 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,222 @@ | ||
using System.Net; | ||
using System.Reflection; | ||
using System.Security.Claims; | ||
using HotChocolate.AspNetCore.Tests.Utilities; | ||
using HotChocolate.Execution.Configuration; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Descriptors; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
public class AuthorizationRequirementDataTests(TestServerFactory serverFactory) : ServerTestBase(serverFactory) | ||
{ | ||
[Fact] | ||
public async Task Authorized() | ||
{ | ||
// arrange | ||
var server = CreateTestServer( | ||
builder => | ||
{ | ||
builder.Services.AddTransient<IAuthorizationHandler, CustomAuthorizationHandler>(); | ||
builder | ||
.AddQueryType<Query>() | ||
.AddAuthorization(); | ||
}, | ||
context => | ||
{ | ||
var identity = new ClaimsIdentity("testauth"); | ||
identity.AddClaim(new Claim( | ||
"foo", | ||
"bar")); | ||
context.User = new ClaimsPrincipal(identity); | ||
}); | ||
|
||
// act | ||
var result = await server.PostAsync(new ClientQueryRequest { Query = "{ foo }" }); | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, result.StatusCode); | ||
Assert.Null(result.Errors); | ||
} | ||
|
||
[Fact] | ||
public async Task NotAuthorized() | ||
{ | ||
// arrange | ||
var server = CreateTestServer( | ||
builder => | ||
{ | ||
builder.Services.AddTransient<IAuthorizationHandler, CustomAuthorizationHandler>(); | ||
builder | ||
.AddQueryType<Query>() | ||
.AddAuthorization(); | ||
}, | ||
context => | ||
{ | ||
var identity = new ClaimsIdentity("testauth"); | ||
identity.AddClaim(new Claim( | ||
"foo", | ||
"foo")); | ||
context.User = new ClaimsPrincipal(identity); | ||
}); | ||
|
||
// act | ||
var result = await server.PostAsync(new ClientQueryRequest { Query = "{ foo }" }); | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, result.StatusCode); | ||
result.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task Multiple_Authorized() | ||
{ | ||
// arrange | ||
var server = CreateTestServer( | ||
builder => | ||
{ | ||
builder.Services.AddTransient<IAuthorizationHandler, CustomAuthorizationHandler>(); | ||
builder | ||
.AddQueryType<Query>() | ||
.AddAuthorization(); | ||
}, | ||
context => | ||
{ | ||
var identity = new ClaimsIdentity("testauth"); | ||
identity.AddClaim(new Claim( | ||
"foo", | ||
"bar")); | ||
identity.AddClaim(new Claim( | ||
"bar", | ||
"baz")); | ||
context.User = new ClaimsPrincipal(identity); | ||
}); | ||
|
||
// act | ||
var result = await server.PostAsync(new ClientQueryRequest { Query = "{ fooMultiple }" }); | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, result.StatusCode); | ||
Assert.Null(result.Errors); | ||
} | ||
|
||
[Fact] | ||
public async Task Multiple_NotAuthorized() | ||
{ | ||
// arrange | ||
var server = CreateTestServer( | ||
builder => | ||
{ | ||
builder.Services.AddTransient<IAuthorizationHandler, CustomAuthorizationHandler>(); | ||
builder | ||
.AddQueryType<Query>() | ||
.AddAuthorization(); | ||
}, | ||
context => | ||
{ | ||
var identity = new ClaimsIdentity("testauth"); | ||
identity.AddClaim(new Claim( | ||
"foo", | ||
"bar")); | ||
identity.AddClaim(new Claim( | ||
"bar", | ||
"bar")); | ||
context.User = new ClaimsPrincipal(identity); | ||
}); | ||
|
||
// act | ||
var result = await server.PostAsync(new ClientQueryRequest { Query = "{ fooMultiple }" }); | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, result.StatusCode); | ||
result.MatchSnapshot(); | ||
} | ||
|
||
public class Query | ||
{ | ||
[CustomAuthorize("foo", "bar", Apply = HotChocolate.Authorization.ApplyPolicy.BeforeResolver)] | ||
public string? GetFoo() => "foo"; | ||
|
||
[CustomAuthorize("foo", "bar", Apply = HotChocolate.Authorization.ApplyPolicy.BeforeResolver)] | ||
[CustomAuthorize("bar", "baz", Apply = HotChocolate.Authorization.ApplyPolicy.BeforeResolver)] | ||
public string? GetFooMultiple() => "foo"; | ||
} | ||
|
||
private class CustomAuthorizeAttribute(string type, string value) | ||
: HotChocolate.Authorization.AuthorizeAttribute, | ||
IAuthorizationRequirement, | ||
IAuthorizationRequirementData | ||
{ | ||
public string Type => type; | ||
|
||
public string Value => value; | ||
|
||
public IEnumerable<IAuthorizationRequirement> GetRequirements() | ||
{ | ||
yield return this; | ||
} | ||
|
||
protected internal override void TryConfigure( | ||
IDescriptorContext context, | ||
IDescriptor descriptor, | ||
ICustomAttributeProvider element) | ||
{ | ||
if (descriptor is IObjectTypeDescriptor type) | ||
{ | ||
type.Directive(CreateDirective()); | ||
} | ||
else if (descriptor is IObjectFieldDescriptor field) | ||
{ | ||
field.Directive(CreateDirective()); | ||
} | ||
} | ||
|
||
private HotChocolate.Authorization.AuthorizeDirective CreateDirective() | ||
{ | ||
return new HotChocolate.Authorization.AuthorizeDirective(metadata: [this]); | ||
} | ||
} | ||
|
||
private class CustomAuthorizationHandler : AuthorizationHandler<CustomAuthorizeAttribute> | ||
{ | ||
protected override Task HandleRequirementAsync( | ||
AuthorizationHandlerContext context, | ||
CustomAuthorizeAttribute requirement) | ||
{ | ||
if (context.User.HasClaim(requirement.Type, requirement.Value)) | ||
{ | ||
context.Succeed(requirement); | ||
} | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
private TestServer CreateTestServer( | ||
Action<IRequestExecutorBuilder> build, | ||
Action<HttpContext> configureUser) | ||
{ | ||
return ServerFactory.Create( | ||
services => | ||
{ | ||
build(services | ||
.AddRouting() | ||
.AddGraphQLServer() | ||
.AddHttpRequestInterceptor( | ||
(context, requestExecutor, requestBuilder, cancellationToken) => | ||
{ | ||
configureUser(context); | ||
return default; | ||
})); | ||
}, | ||
app => | ||
{ | ||
app.UseRouting(); | ||
app.UseEndpoints(b => b.MapGraphQL()); | ||
}); | ||
} | ||
} |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using AuthorizeDirective as the dictionary key may lead to cache misses if two different instances have equivalent values. Consider overriding Equals and GetHashCode on AuthorizeDirective or reverting to a value-based cache key to ensure consistent behavior.
Copilot uses AI. Check for mistakes.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is okay because the instance is at least bound to a field or type, and has reference-equality across requests.
This is needed for a directive that have no policy, no roles, but metadata.
AuthorizationRequirementDataTests.Multiple_NotAuthorized
is a test...