Skip to content
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

Feature/improve authentication for minimal api #204

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<IResult> {item.Name}(");
sb.AppendLine(8, $"[FromServices] {item.InterfaceName} handler,");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()");
Expand Down Expand Up @@ -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, "{");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void GenerateServiceCollectionEndpointHandlerExtensions()

var classParameters = new ClassParameters(
codeGeneratorContentHeader,
Namespace: projectName,
Namespace: $"{projectName}.Extensions",
DocumentationTags: null,
Attributes: [codeGeneratorAttribute],
AccessModifiers.PublicStaticClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}]"
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading