-
-
Notifications
You must be signed in to change notification settings - Fork 803
[Fusion] Add support for merging @authorize directives
#9378
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
19 changes: 19 additions & 0 deletions
19
...ocolate/Fusion/src/Fusion.Composition/Definitions/ApplyPolicyMutableEnumTypeDefinition.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,19 @@ | ||
| using HotChocolate.Types.Mutable; | ||
|
|
||
| namespace HotChocolate.Fusion.Definitions; | ||
|
|
||
| internal sealed class ApplyPolicyMutableEnumTypeDefinition : MutableEnumTypeDefinition | ||
| { | ||
| public ApplyPolicyMutableEnumTypeDefinition() | ||
| : base(WellKnownTypeNames.ApplyPolicy) | ||
| { | ||
| Values.Add(new MutableEnumValue("BEFORE_RESOLVER")); | ||
| Values.Add(new MutableEnumValue("AFTER_RESOLVER")); | ||
| Values.Add(new MutableEnumValue("VALIDATION")); | ||
| } | ||
|
|
||
| public static ApplyPolicyMutableEnumTypeDefinition Create() | ||
| { | ||
| return new ApplyPolicyMutableEnumTypeDefinition(); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
...hocolate/Fusion/src/Fusion.Composition/Definitions/AuthorizeMutableDirectiveDefinition.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,47 @@ | ||
| using HotChocolate.Language; | ||
| using HotChocolate.Types; | ||
| using HotChocolate.Types.Mutable; | ||
| using DirectiveLocation = HotChocolate.Types.DirectiveLocation; | ||
|
|
||
| namespace HotChocolate.Fusion.Definitions; | ||
|
|
||
| internal sealed class AuthorizeMutableDirectiveDefinition : MutableDirectiveDefinition | ||
| { | ||
| public AuthorizeMutableDirectiveDefinition( | ||
| MutableScalarTypeDefinition stringType, | ||
| MutableEnumTypeDefinition applyPolicyType) | ||
| : base(WellKnownDirectiveNames.Authorize) | ||
| { | ||
| Arguments.Add(new MutableInputFieldDefinition(WellKnownArgumentNames.Policy, stringType)); | ||
| Arguments.Add( | ||
| new MutableInputFieldDefinition( | ||
| WellKnownArgumentNames.Roles, | ||
| new ListType(new NonNullType(stringType)))); | ||
| Arguments.Add( | ||
| new MutableInputFieldDefinition(WellKnownArgumentNames.Apply, new NonNullType(applyPolicyType)) | ||
| { | ||
| DefaultValue = new EnumValueNode("BEFORE_RESOLVER") | ||
| }); | ||
| IsRepeatable = true; | ||
| Locations = DirectiveLocation.FieldDefinition | DirectiveLocation.Object; | ||
| } | ||
|
|
||
| public static AuthorizeMutableDirectiveDefinition Create(ISchemaDefinition schema) | ||
| { | ||
| if (!schema.Types.TryGetType<MutableScalarTypeDefinition>( | ||
| SpecScalarNames.String.Name, | ||
| out var stringType)) | ||
| { | ||
| stringType = BuiltIns.String.Create(); | ||
| } | ||
|
|
||
| if (!schema.Types.TryGetType<MutableEnumTypeDefinition>( | ||
| WellKnownTypeNames.ApplyPolicy, | ||
| out var applyPolicyType)) | ||
| { | ||
| applyPolicyType = ApplyPolicyMutableEnumTypeDefinition.Create(); | ||
| } | ||
|
|
||
| return new AuthorizeMutableDirectiveDefinition(stringType, applyPolicyType); | ||
| } | ||
| } |
136 changes: 136 additions & 0 deletions
136
src/HotChocolate/Fusion/src/Fusion.Composition/DirectiveMergers/AuthorizeDirectiveMerger.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,136 @@ | ||
| using System.Collections.Immutable; | ||
| using HotChocolate.Fusion.Definitions; | ||
| using HotChocolate.Fusion.Directives; | ||
| using HotChocolate.Fusion.Extensions; | ||
| using HotChocolate.Fusion.Info; | ||
| using HotChocolate.Fusion.Options; | ||
| using HotChocolate.Language; | ||
| using HotChocolate.Types; | ||
| using HotChocolate.Types.Mutable; | ||
| using ArgumentNames = HotChocolate.Fusion.WellKnownArgumentNames; | ||
| using DirectiveNames = HotChocolate.Fusion.WellKnownDirectiveNames; | ||
|
|
||
| namespace HotChocolate.Fusion.DirectiveMergers; | ||
|
|
||
| internal class AuthorizeDirectiveMerger(DirectiveMergeBehavior mergeBehavior) | ||
| : DirectiveMergerBase(mergeBehavior) | ||
| { | ||
| public override string DirectiveName => DirectiveNames.Authorize; | ||
|
|
||
| public override MutableDirectiveDefinition GetCanonicalDirectiveDefinition(MutableSchemaDefinition schema) | ||
| { | ||
| return AuthorizeMutableDirectiveDefinition.Create(schema); | ||
| } | ||
|
|
||
| public override void MergeDirectives( | ||
| IDirectivesProvider mergedMember, | ||
| ImmutableArray<DirectivesProviderInfo> memberDefinitions, | ||
| MutableSchemaDefinition mergedSchema) | ||
| { | ||
| if (!mergedSchema.DirectiveDefinitions.TryGetDirective(DirectiveName, out var directiveDefinition)) | ||
| { | ||
| // Merged definition not found. | ||
| return; | ||
| } | ||
|
|
||
| var uniqueAuthorizeDirectives = | ||
| memberDefinitions | ||
| .SelectMany(d => d.Member.Directives.Where(dir => dir.Name == DirectiveNames.Authorize)) | ||
| .Select(AuthorizeDirective.From) | ||
| .Distinct(AuthorizeDirectiveEqualityComparer.Instance); | ||
|
|
||
| foreach (var authorizeDirective in uniqueAuthorizeDirectives) | ||
| { | ||
| var arguments = new List<ArgumentAssignment>(); | ||
|
|
||
| if (authorizeDirective.Policy is not null) | ||
| { | ||
| arguments.Add( | ||
| new ArgumentAssignment(ArgumentNames.Policy, authorizeDirective.Policy)); | ||
| } | ||
|
|
||
| if (authorizeDirective.Roles is not null) | ||
| { | ||
| arguments.Add( | ||
| new ArgumentAssignment( | ||
| ArgumentNames.Roles, | ||
| new ListValueNode( | ||
| authorizeDirective.Roles | ||
| .Order(StringComparer.Ordinal) | ||
| .Select(r => new StringValueNode(r)) | ||
| .ToList()))); | ||
| } | ||
|
|
||
| if (authorizeDirective.Apply is { } apply and not ApplyPolicy.BeforeResolver) | ||
| { | ||
| arguments.Add( | ||
| new ArgumentAssignment( | ||
| ArgumentNames.Apply, | ||
| new EnumValueNode( | ||
| apply == ApplyPolicy.AfterResolver ? "AFTER_RESOLVER" : "VALIDATION"))); | ||
| } | ||
|
|
||
| mergedMember.AddDirective(new Directive(directiveDefinition, arguments)); | ||
| } | ||
| } | ||
|
|
||
| private sealed class AuthorizeDirectiveEqualityComparer | ||
| : IEqualityComparer<AuthorizeDirective> | ||
| { | ||
| public static readonly AuthorizeDirectiveEqualityComparer Instance = new(); | ||
|
|
||
| public bool Equals(AuthorizeDirective? x, AuthorizeDirective? y) | ||
| { | ||
| if (ReferenceEquals(x, y)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (x is null || y is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return x.Policy == y.Policy | ||
| && (x.Apply ?? ApplyPolicy.BeforeResolver) == (y.Apply ?? ApplyPolicy.BeforeResolver) | ||
| && RolesEqual(x.Roles, y.Roles); | ||
| } | ||
|
|
||
| public int GetHashCode(AuthorizeDirective obj) | ||
| { | ||
| var hash = new HashCode(); | ||
| hash.Add(obj.Policy); | ||
| hash.Add(obj.Apply ?? ApplyPolicy.BeforeResolver); | ||
|
|
||
| if (obj.Roles is not null) | ||
| { | ||
| foreach (var role in obj.Roles.Order(StringComparer.Ordinal)) | ||
| { | ||
| hash.Add(role); | ||
| } | ||
| } | ||
|
|
||
| return hash.ToHashCode(); | ||
| } | ||
|
|
||
| private static bool RolesEqual(List<string>? a, List<string>? b) | ||
| { | ||
| if (a is null && b is null) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (a is null || b is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (a.Count != b.Count) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return a.Order(StringComparer.Ordinal).SequenceEqual(b.Order(StringComparer.Ordinal)); | ||
| } | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
src/HotChocolate/Fusion/src/Fusion.Composition/Directives/ApplyPolicy.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,8 @@ | ||
| namespace HotChocolate.Fusion.Directives; | ||
|
|
||
| internal enum ApplyPolicy | ||
| { | ||
| BeforeResolver = 0, | ||
| AfterResolver = 1, | ||
| Validation = 2 | ||
| } |
69 changes: 69 additions & 0 deletions
69
src/HotChocolate/Fusion/src/Fusion.Composition/Directives/AuthorizeDirective.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,69 @@ | ||
| using HotChocolate.Language; | ||
| using HotChocolate.Types; | ||
| using static HotChocolate.Fusion.Properties.CompositionResources; | ||
| using ArgumentNames = HotChocolate.Fusion.WellKnownArgumentNames; | ||
|
|
||
| namespace HotChocolate.Fusion.Directives; | ||
|
|
||
| internal sealed class AuthorizeDirective( | ||
| string? policy, | ||
| List<string>? roles, | ||
| ApplyPolicy? apply = null) | ||
| { | ||
| public string? Policy { get; } = policy; | ||
|
|
||
| public List<string>? Roles { get; } = roles; | ||
|
|
||
| public ApplyPolicy? Apply { get; } = apply; | ||
|
|
||
| public static AuthorizeDirective From(IDirective directive) | ||
| { | ||
| string? policy = null; | ||
| List<string>? roles = null; | ||
| ApplyPolicy? applyPolicy = null; | ||
|
|
||
| if (directive.Arguments.TryGetValue(ArgumentNames.Policy, out var policyArg)) | ||
| { | ||
| policy = policyArg switch | ||
| { | ||
| StringValueNode stringValueNode => stringValueNode.Value, | ||
| NullValueNode => null, | ||
| _ => throw new InvalidOperationException(AuthorizeDirective_PolicyArgument_Invalid) | ||
| }; | ||
| } | ||
|
|
||
| if (directive.Arguments.TryGetValue(ArgumentNames.Roles, out var rolesArg)) | ||
| { | ||
| roles = rolesArg switch | ||
| { | ||
| ListValueNode listValueNode when listValueNode.Items.All(v => v is StringValueNode) | ||
| => listValueNode.Items.Cast<StringValueNode>().Select(v => v.Value).ToList(), | ||
| NullValueNode => null, | ||
| _ => throw new InvalidOperationException(AuthorizeDirective_RolesArgument_Invalid) | ||
| }; | ||
| } | ||
|
|
||
| if (directive.Arguments.TryGetValue(ArgumentNames.Apply, out var applyArg)) | ||
| { | ||
| applyPolicy = applyArg switch | ||
| { | ||
| EnumValueNode enumValueNode => GetApplyPolicy(enumValueNode.Value), | ||
| _ => throw new InvalidOperationException(AuthorizeDirective_ApplyArgument_Invalid) | ||
| }; | ||
| } | ||
|
|
||
| return new AuthorizeDirective(policy, roles, applyPolicy); | ||
|
glen-84 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static ApplyPolicy GetApplyPolicy(string applyPolicyValue) | ||
| { | ||
| return applyPolicyValue switch | ||
| { | ||
| "BEFORE_RESOLVER" => ApplyPolicy.BeforeResolver, | ||
| "AFTER_RESOLVER" => ApplyPolicy.AfterResolver, | ||
| "VALIDATION" => ApplyPolicy.Validation, | ||
| _ => throw new InvalidOperationException( | ||
| string.Format(AuthorizeDirective_ApplyArgument_InvalidEnumValue, applyPolicyValue)) | ||
| }; | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
src/HotChocolate/Fusion/src/Fusion.Composition/Properties/CompositionResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.
Uh oh!
There was an error while loading. Please reload this page.