From d94063a11c5443ed0e8aa130fdf2768c3eb552d8 Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Tue, 17 Dec 2024 10:26:04 +0100 Subject: [PATCH] Made AddAuthorizeDirectiveType public for customizations. (#7836) --- .../src/Abstractions/WellKnownContextData.cs | 5 +++ .../AuthorizeSchemaBuilderExtensions.cs | 40 ++++++++++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/HotChocolate/Core/src/Abstractions/WellKnownContextData.cs b/src/HotChocolate/Core/src/Abstractions/WellKnownContextData.cs index 53b633819ac..788484c4c4c 100644 --- a/src/HotChocolate/Core/src/Abstractions/WellKnownContextData.cs +++ b/src/HotChocolate/Core/src/Abstractions/WellKnownContextData.cs @@ -338,4 +338,9 @@ public static class WellKnownContextData /// The key to determine whether the request is a warmup request. /// public const string IsWarmupRequest = "HotChocolate.AspNetCore.Warmup.IsWarmupRequest"; + + /// + /// The key to determine whether the @authorize directive was already registered. + /// + public const string AreAuthorizeDirectivesRegistered = "HotChocolate.Authorization.AuthDirectivesRegistered"; } diff --git a/src/HotChocolate/Core/src/Authorization/Extensions/AuthorizeSchemaBuilderExtensions.cs b/src/HotChocolate/Core/src/Authorization/Extensions/AuthorizeSchemaBuilderExtensions.cs index e0f0ca023e1..e8abcaac2e6 100644 --- a/src/HotChocolate/Core/src/Authorization/Extensions/AuthorizeSchemaBuilderExtensions.cs +++ b/src/HotChocolate/Core/src/Authorization/Extensions/AuthorizeSchemaBuilderExtensions.cs @@ -2,8 +2,23 @@ namespace HotChocolate; -internal static class AuthorizeSchemaBuilderExtensions +/// +/// Provides extension methods for the schema builder. +/// +public static class AuthorizeSchemaBuilderExtensions { + /// + /// Adds the authorize directive types to the schema. + /// + /// + /// The schema builder. + /// + /// + /// Returns the schema builder for configuration chaining. + /// + /// + /// The is null. + /// public static ISchemaBuilder AddAuthorizeDirectiveType(this ISchemaBuilder builder) { if (builder is null) @@ -11,14 +26,21 @@ public static ISchemaBuilder AddAuthorizeDirectiveType(this ISchemaBuilder build throw new ArgumentNullException(nameof(builder)); } - var authorize = new AuthorizeDirectiveType(); - var allowAnonymous = new AllowAnonymousDirectiveType(); + if (!builder.ContextData.ContainsKey(WellKnownContextData.AreAuthorizeDirectivesRegistered)) + { + var authorize = new AuthorizeDirectiveType(); + var allowAnonymous = new AllowAnonymousDirectiveType(); + + builder + .AddDirectiveType(authorize) + .AddDirectiveType(allowAnonymous) + .TryAddSchemaDirective(authorize) + .TryAddSchemaDirective(allowAnonymous) + .TryAddTypeInterceptor(); + + builder.SetContextData(WellKnownContextData.AreAuthorizeDirectivesRegistered, true); + } - return builder - .AddDirectiveType(authorize) - .AddDirectiveType(allowAnonymous) - .TryAddSchemaDirective(authorize) - .TryAddSchemaDirective(allowAnonymous) - .TryAddTypeInterceptor(); + return builder; } }