From 040ee3cd9681e95b91d0e3d0ea3d2ea08fc94021 Mon Sep 17 00:00:00 2001 From: David Kallesen Date: Tue, 13 Aug 2024 11:01:44 +0200 Subject: [PATCH 1/3] feat: move AppendMethodContentAuthorizationIfNeeded to a helper and use in in mimimal-api --- .../ContentGeneratorServerEndpoints.cs | 5 ++ .../ContentGeneratorServerController.cs | 65 ++---------------- .../Helpers/StringBuilderEndpointHelper.cs | 67 +++++++++++++++++++ 3 files changed, 76 insertions(+), 61 deletions(-) create mode 100644 src/Atc.Rest.ApiGenerator.Framework/Helpers/StringBuilderEndpointHelper.cs diff --git a/src/Atc.Rest.ApiGenerator.Framework.Minimal/ContentGenerators/ContentGeneratorServerEndpoints.cs b/src/Atc.Rest.ApiGenerator.Framework.Minimal/ContentGenerators/ContentGeneratorServerEndpoints.cs index 3852163a..60e5d244 100644 --- a/src/Atc.Rest.ApiGenerator.Framework.Minimal/ContentGenerators/ContentGeneratorServerEndpoints.cs +++ b/src/Atc.Rest.ApiGenerator.Framework.Minimal/ContentGenerators/ContentGeneratorServerEndpoints.cs @@ -155,6 +155,11 @@ private void AppendRouteHandlers( { var item = parameters.MethodParameters[i]; + StringBuilderEndpointHelper.AppendMethodContentAuthorizationIfNeeded( + sb, + parameters.Authorization, + item.Authorization); + sb.AppendLine(4, $"internal async Task {item.Name}("); sb.AppendLine(8, $"[FromServices] {item.InterfaceName} handler,"); diff --git a/src/Atc.Rest.ApiGenerator.Framework.Mvc/ContentGenerators/ContentGeneratorServerController.cs b/src/Atc.Rest.ApiGenerator.Framework.Mvc/ContentGenerators/ContentGeneratorServerController.cs index 91891795..f752b4e6 100644 --- a/src/Atc.Rest.ApiGenerator.Framework.Mvc/ContentGenerators/ContentGeneratorServerController.cs +++ b/src/Atc.Rest.ApiGenerator.Framework.Mvc/ContentGenerators/ContentGeneratorServerController.cs @@ -66,7 +66,10 @@ private void AppendContent( sb.Append(codeDocumentationTagsGenerator.GenerateTags(4, item.DocumentationTags)); } - AppendMethodContentAuthorizationIfNeeded(sb, item); + StringBuilderEndpointHelper.AppendMethodContentAuthorizationIfNeeded( + sb, + parameters.Authorization, + item.Authorization); sb.AppendLine(4, string.IsNullOrEmpty(item.RouteSuffix) ? $"[Http{item.OperationTypeRepresentation}]" @@ -109,66 +112,6 @@ private void AppendContent( } } - private void AppendMethodContentAuthorizationIfNeeded( - StringBuilder sb, - ContentGeneratorServerEndpointMethodParameters item) - { - if (item.Authorization is null) - { - return; - } - - if (item.Authorization.UseAllowAnonymous) - { - if (parameters.Authorization is not null && - parameters.Authorization.UseAllowAnonymous) - { - return; - } - - sb.AppendLine(4, "[AllowAnonymous]"); - return; - } - - if (parameters.Authorization is not null && - !parameters.Authorization.UseAllowAnonymous && - (item.Authorization.Roles is null || item.Authorization.Roles.Count == 0) && - (item.Authorization.AuthenticationSchemes is null || item.Authorization.AuthenticationSchemes.Count == 0)) - { - return; - } - - var authorizeLineBuilder = new StringBuilder(); - var authRoles = item.Authorization.Roles is null - ? null - : string.Join(',', item.Authorization.Roles); - var authSchemes = item.Authorization.AuthenticationSchemes is null - ? null - : string.Join(',', item.Authorization.AuthenticationSchemes); - - authorizeLineBuilder.Append(4, "[Authorize"); - - if (!string.IsNullOrEmpty(authRoles)) - { - authorizeLineBuilder.Append($"(Roles = \"{authRoles}\""); - } - - if (!string.IsNullOrEmpty(authSchemes)) - { - authorizeLineBuilder.Append(string.IsNullOrEmpty(authRoles) - ? $"(AuthenticationSchemes = \"{authSchemes}\"" - : $", AuthenticationSchemes = \"{authSchemes}\""); - } - - if (!string.IsNullOrEmpty(authRoles) || !string.IsNullOrEmpty(authSchemes)) - { - authorizeLineBuilder.Append(')'); - } - - authorizeLineBuilder.Append(']'); - sb.AppendLine(authorizeLineBuilder.ToString()); - } - private static void AppendProducesWithProblemDetails( StringBuilder sb, ContentGeneratorServerEndpointMethodParameters item) diff --git a/src/Atc.Rest.ApiGenerator.Framework/Helpers/StringBuilderEndpointHelper.cs b/src/Atc.Rest.ApiGenerator.Framework/Helpers/StringBuilderEndpointHelper.cs new file mode 100644 index 00000000..04008887 --- /dev/null +++ b/src/Atc.Rest.ApiGenerator.Framework/Helpers/StringBuilderEndpointHelper.cs @@ -0,0 +1,67 @@ +namespace Atc.Rest.ApiGenerator.Framework.Helpers; + +public static class StringBuilderEndpointHelper +{ + public static void AppendMethodContentAuthorizationIfNeeded( + StringBuilder sb, + ApiAuthorizeModel? authorizationForController, + ApiAuthorizeModel? authorizationForEndpoint) + { + ArgumentNullException.ThrowIfNull(sb); + + if (authorizationForEndpoint is null) + { + return; + } + + if (authorizationForEndpoint.UseAllowAnonymous) + { + if (authorizationForController is not null && + authorizationForController.UseAllowAnonymous) + { + return; + } + + sb.AppendLine(4, "[AllowAnonymous]"); + return; + } + + if (authorizationForController is not null && + !authorizationForController.UseAllowAnonymous && + (authorizationForEndpoint.Roles is null || authorizationForEndpoint.Roles.Count == 0) && + (authorizationForEndpoint.AuthenticationSchemes is null || authorizationForEndpoint.AuthenticationSchemes.Count == 0)) + { + return; + } + + var authorizeLineBuilder = new StringBuilder(); + var authRoles = authorizationForEndpoint.Roles is null + ? null + : string.Join(',', authorizationForEndpoint.Roles); + var authSchemes = authorizationForEndpoint.AuthenticationSchemes is null + ? null + : string.Join(',', authorizationForEndpoint.AuthenticationSchemes); + + authorizeLineBuilder.Append(4, "[Authorize"); + + if (!string.IsNullOrEmpty(authRoles)) + { + authorizeLineBuilder.Append($"(Roles = \"{authRoles}\""); + } + + if (!string.IsNullOrEmpty(authSchemes)) + { + authorizeLineBuilder.Append(string.IsNullOrEmpty(authRoles) + ? $"(AuthenticationSchemes = \"{authSchemes}\"" + : $", AuthenticationSchemes = \"{authSchemes}\""); + } + + if (!string.IsNullOrEmpty(authRoles) || !string.IsNullOrEmpty(authSchemes)) + { + authorizeLineBuilder.Append(')'); + } + + authorizeLineBuilder.Append(']'); + sb.AppendLine(authorizeLineBuilder.ToString()); + } +} \ No newline at end of file From 987921c2c3205a9947d9637fa9a8a89929b701b0 Mon Sep 17 00:00:00 2001 From: David Kallesen Date: Tue, 13 Aug 2024 11:02:17 +0200 Subject: [PATCH 2/3] feat: add Extensions to the namespace for ServiceCollectionEndpointHandlerExtensions --- .../ProjectGenerator/ServerDomainGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Atc.Rest.ApiGenerator.Framework.Minimal/ProjectGenerator/ServerDomainGenerator.cs b/src/Atc.Rest.ApiGenerator.Framework.Minimal/ProjectGenerator/ServerDomainGenerator.cs index 6c364806..cf081dff 100644 --- a/src/Atc.Rest.ApiGenerator.Framework.Minimal/ProjectGenerator/ServerDomainGenerator.cs +++ b/src/Atc.Rest.ApiGenerator.Framework.Minimal/ProjectGenerator/ServerDomainGenerator.cs @@ -219,7 +219,7 @@ public void GenerateServiceCollectionEndpointHandlerExtensions() var classParameters = new ClassParameters( codeGeneratorContentHeader, - Namespace: projectName, + Namespace: $"{projectName}.Extensions", DocumentationTags: null, Attributes: [codeGeneratorAttribute], AccessModifiers.PublicStaticClass, From 7b0b944dfdee6981912a6bb8231decafc88b2d60 Mon Sep 17 00:00:00 2001 From: David Kallesen Date: Tue, 13 Aug 2024 11:02:38 +0200 Subject: [PATCH 3/3] feat: rename DemoCorsPolicy to CorsPolicy --- .../ContentGenerators/ContentGeneratorServerProgram.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Atc.Rest.ApiGenerator.Framework.Minimal/ContentGenerators/ContentGeneratorServerProgram.cs b/src/Atc.Rest.ApiGenerator.Framework.Minimal/ContentGenerators/ContentGeneratorServerProgram.cs index 010f6b20..9bda3dc6 100644 --- a/src/Atc.Rest.ApiGenerator.Framework.Minimal/ContentGenerators/ContentGeneratorServerProgram.cs +++ b/src/Atc.Rest.ApiGenerator.Framework.Minimal/ContentGenerators/ContentGeneratorServerProgram.cs @@ -48,7 +48,7 @@ public string Generate() sb.AppendLine(8, "services.AddCors(corsOptions =>"); sb.AppendLine(8, "{"); - sb.AppendLine(12, "corsOptions.AddPolicy(\"DemoCorsPolicy\", configurePolicy =>"); + sb.AppendLine(12, "corsOptions.AddPolicy(\"CorsPolicy\", configurePolicy =>"); sb.AppendLine(12, "{"); sb.AppendLine(16, "configurePolicy"); sb.AppendLine(20, ".AllowAnyOrigin()"); @@ -88,7 +88,7 @@ public string Generate() sb.AppendLine(8, "app.UseHttpsRedirection();"); sb.AppendLine(8, "app.UseHsts();"); sb.AppendLine(); - sb.AppendLine(8, "app.UseCors(\"DemoCorsPolicy\");"); + sb.AppendLine(8, "app.UseCors(\"CorsPolicy\");"); sb.AppendLine(); sb.AppendLine(8, "if (!app.Environment.IsDevelopment())"); sb.AppendLine(8, "{");