Skip to content

Commit

Permalink
Made AddAuthorizeDirectiveType public for customizations. (#7836)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Dec 17, 2024
1 parent 3266bfb commit d94063a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,9 @@ public static class WellKnownContextData
/// The key to determine whether the request is a warmup request.
/// </summary>
public const string IsWarmupRequest = "HotChocolate.AspNetCore.Warmup.IsWarmupRequest";

/// <summary>
/// The key to determine whether the @authorize directive was already registered.
/// </summary>
public const string AreAuthorizeDirectivesRegistered = "HotChocolate.Authorization.AuthDirectivesRegistered";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,45 @@

namespace HotChocolate;

internal static class AuthorizeSchemaBuilderExtensions
/// <summary>
/// Provides extension methods for the schema builder.
/// </summary>
public static class AuthorizeSchemaBuilderExtensions
{
/// <summary>
/// Adds the authorize directive types to the schema.
/// </summary>
/// <param name="builder">
/// The schema builder.
/// </param>
/// <returns>
/// Returns the schema builder for configuration chaining.
/// </returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="builder"/> is <c>null</c>.
/// </exception>
public static ISchemaBuilder AddAuthorizeDirectiveType(this ISchemaBuilder builder)
{
if (builder is null)
{
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<AuthorizationTypeInterceptor>();

builder.SetContextData(WellKnownContextData.AreAuthorizeDirectivesRegistered, true);
}

return builder
.AddDirectiveType(authorize)
.AddDirectiveType(allowAnonymous)
.TryAddSchemaDirective(authorize)
.TryAddSchemaDirective(allowAnonymous)
.TryAddTypeInterceptor<AuthorizationTypeInterceptor>();
return builder;
}
}

0 comments on commit d94063a

Please sign in to comment.