Skip to content

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
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

sunghwan2789
Copy link
Collaborator

No description provided.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces support for IAuthorizationRequirementData in the annotation based schema by extending the AuthorizeDirective with a metadata parameter. Key changes include adding a metadata parameter/property to AuthorizeDirective, updating the construction of authorization policies to include metadata requirements, and modifying the caching mechanism to use the AuthorizeDirective directly as a key.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/HotChocolate/Core/src/Authorization/AuthorizeDirective.cs Removed cache key generation and added the metadata parameter and property.
src/HotChocolate/AspNetCore/test/AspNetCore.Authorization.Tests/snapshots/* Updated snapshot files to reflect authorization error responses.
src/HotChocolate/AspNetCore/test/AspNetCore.Authorization.Tests/AuthorizeDirectiveTests.cs Removed tests relying on cache key retrieval from AuthorizeDirective.
src/HotChocolate/AspNetCore/test/AspNetCore.Authorization.Tests/AuthorizationRequirementDataTests.cs Added tests covering authorization logic with metadata requirements.
src/HotChocolate/AspNetCore/src/AspNetCore.Authorization/DefaultAuthorizationHandler.cs Updated BuildAuthorizationPolicy to incorporate metadata.
src/HotChocolate/AspNetCore/src/AspNetCore.Authorization/AuthorizationPolicyCache.cs Changed cache keys from string values to AuthorizeDirective instances.

@@ -6,19 +6,15 @@ namespace HotChocolate.AspNetCore.Authorization;

internal sealed class AuthorizationPolicyCache
{
private readonly ConcurrentDictionary<string, AuthorizationPolicy> _cache = new();
private readonly ConcurrentDictionary<AuthorizeDirective, AuthorizationPolicy> _cache = new();
Copy link
Preview

Copilot AI May 22, 2025

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.

Copy link
Collaborator Author

@sunghwan2789 sunghwan2789 May 22, 2025

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...

@sunghwan2789 sunghwan2789 changed the title Support IAuthorizationRequirementData in annotation based schema Support IAuthorizationRequirementData in the implementation-first approach May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant