Skip to content

Commit

Permalink
Refactor/Extract OpenApi bootstrapping from Program.cs (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashendrickx authored May 17, 2024
1 parent ec703df commit 922e2fa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 45 deletions.
62 changes: 62 additions & 0 deletions src/Api/OpenApi/OpenApiBootstrap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.OpenApi.Models;
using Passwordless.Api.Authorization;
using Passwordless.Api.OpenApi.Filters;

namespace Passwordless.Api.OpenApi;

public static class OpenApiBootstrap
{
public static void AddOpenApi(this IServiceCollection services)
{
services.AddSwaggerGen(swagger =>
{
swagger.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "Passwordless.Api.xml"), true);
swagger.DocInclusionPredicate((docName, apiDesc) =>
{
var policy = (AuthorizationPolicy?)apiDesc.ActionDescriptor.EndpointMetadata.FirstOrDefault(x => x.GetType() == typeof(AuthorizationPolicy));
if (policy == null)
{
return false;
}
return !policy.AuthenticationSchemes.Contains(Constants.ManagementKeyAuthenticationScheme);
});
swagger.OperationFilter<AuthorizationOperationFilter>();
swagger.OperationFilter<ExtendedStatusDescriptionsOperationFilter>();
swagger.OperationFilter<ExternalDocsOperationFilter>();
swagger.SupportNonNullableReferenceTypes();
swagger.SwaggerDoc("v4", new OpenApiInfo
{
Version = "v4",
Title = "Passwordless.dev API",
TermsOfService = new Uri("https://bitwarden.com/terms/"),
Contact = new OpenApiContact
{
Email = "[email protected]",
Name = "Support",
Url = new Uri("https://bitwarden.com/contact/")
}
});
swagger.SwaggerGeneratorOptions.IgnoreObsoleteActions = true;
});
}

public static void UseOpenApi(this IApplicationBuilder app)
{
app.UseSwagger(c => c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
{
httpReq.HttpContext.Response.Headers.Append("Access-Control-Allow-Origin", "*");
}));

app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v4/swagger.json", "v4");
c.ConfigObject.ShowExtensions = true;
c.ConfigObject.ShowCommonExtensions = true;
c.DefaultModelsExpandDepth(-1);
c.IndexStream = () => typeof(Program).Assembly.GetManifestResourceStream("Passwordless.Api.OpenApi.swagger.html");
c.InjectStylesheet("/openapi.css");
c.SupportedSubmitMethods();
});
}
}
48 changes: 3 additions & 45 deletions src/Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Passwordless.Api.HealthChecks;
using Passwordless.Api.Helpers;
using Passwordless.Api.Middleware;
using Passwordless.Api.OpenApi;
using Passwordless.Api.OpenApi.Filters;
using Passwordless.Api.RateLimiting;
using Passwordless.Api.Reporting.Background;
Expand All @@ -28,36 +29,7 @@
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(swagger =>
{
swagger.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "Passwordless.Api.xml"), true);
swagger.DocInclusionPredicate((docName, apiDesc) =>
{
var policy = (AuthorizationPolicy?)apiDesc.ActionDescriptor.EndpointMetadata.FirstOrDefault(x => x.GetType() == typeof(AuthorizationPolicy));
if (policy == null)
{
return false;
}
return !policy.AuthenticationSchemes.Contains(Constants.ManagementKeyAuthenticationScheme);
});
swagger.OperationFilter<AuthorizationOperationFilter>();
swagger.OperationFilter<ExtendedStatusDescriptionsOperationFilter>();
swagger.OperationFilter<ExternalDocsOperationFilter>();
swagger.SupportNonNullableReferenceTypes();
swagger.SwaggerDoc("v4", new OpenApiInfo
{
Version = "v4",
Title = "Passwordless.dev API",
TermsOfService = new Uri("https://bitwarden.com/terms/"),
Contact = new OpenApiContact
{
Email = "[email protected]",
Name = "Support",
Url = new Uri("https://bitwarden.com/contact/")
}
});
swagger.SwaggerGeneratorOptions.IgnoreObsoleteActions = true;
});
builder.Services.AddOpenApi();

if (builder.Configuration.IsSelfHosted())
{
Expand Down Expand Up @@ -147,21 +119,7 @@
"Hey, this place is for computers. Check out our human documentation instead: https://docs.passwordless.dev");
}

app.UseSwagger(c => c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
{
httpReq.HttpContext.Response.Headers.Append("Access-Control-Allow-Origin", "*");
}));

app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v4/swagger.json", "v4");
c.ConfigObject.ShowExtensions = true;
c.ConfigObject.ShowCommonExtensions = true;
c.DefaultModelsExpandDepth(-1);
c.IndexStream = () => typeof(Program).Assembly.GetManifestResourceStream("Passwordless.Api.OpenApi.swagger.html");
c.InjectStylesheet("/openapi.css");
c.SupportedSubmitMethods();
});
app.UseOpenApi();

if (builder.Configuration.IsSelfHosted())
{
Expand Down

0 comments on commit 922e2fa

Please sign in to comment.