-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor/Extract OpenApi bootstrapping from Program.cs (#589)
- Loading branch information
1 parent
ec703df
commit 922e2fa
Showing
2 changed files
with
65 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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()) | ||
{ | ||
|
@@ -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()) | ||
{ | ||
|