using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using Netmash.Extensions.Swagger.Constants; using Netmash.Extensions.Swagger.Filters; using Netmash.Extensions.Swagger.ReverseProxy; using Swashbuckle.AspNetCore.SwaggerGen; using System; using System.Collections.Generic; namespace Netmash.Extensions.Swagger { public static class SwaggerExtensions { public static IServiceCollection AddSwagger(this IServiceCollection services, string title, AuthorizationType authorizationType = AuthorizationType.Basic) { services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = title, Version = "v1" }); c.SetAuthorization(authorizationType); c.CustomSchemaIds(type => type.ToString()); }); return services; } public static IServiceCollection AddSwaggerWithFilters(this IServiceCollection services, string title, AuthorizationType authorizationType = AuthorizationType.Basic) { services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = title, Version = "v1" }); c.SetAuthorization(authorizationType); c.OperationFilter(); c.SchemaFilter(); c.CustomSchemaIds(type => type.ToString()); }); return services; } private static void SetAuthorization(this SwaggerGenOptions options, AuthorizationType authorizationType) { switch (authorizationType) { case AuthorizationType.None: return; case AuthorizationType.Basic: options.SetAuthorization(authorizationType.ToString(), "Basic"); break; case AuthorizationType.InhouseIdentity: options.SetAuthorization(authorizationType.ToString(), "Identity"); break; default: throw new NotImplementedException($"Swagger extensions: Authorization type '{authorizationType}' is not implemented."); } } private static void SetAuthorization(this SwaggerGenOptions options, string schemeName, string schemeKey) { options.AddSecurityDefinition(schemeName, new OpenApiSecurityScheme { In = ParameterLocation.Header, Description = $@"JWT Authorization header using the {schemeName} scheme. Enter '{schemeKey}' [space] and then your token in the text input below. Example: '{schemeKey} 12345abcdef'", Name = "Authorization", Scheme = schemeName, Type = SecuritySchemeType.ApiKey }); options.AddSecurityRequirement(new OpenApiSecurityRequirement() { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = schemeName }, Scheme = schemeName, Name = "Authorization", In = ParameterLocation.Header }, new List() } }); } public static IApplicationBuilder ConfigureSwagger(this IApplicationBuilder applicationBuilder, string endpointName) { applicationBuilder.UseSwagger(c => { c.PreSerializeFilters.Add((swagger, httpRequest) => { var (host, basePath, scheme) = ReverseProxyHelper.GetUrlComponents(httpRequest); swagger.Servers = new List { new OpenApiServer {Url = $"{scheme}://{host}{basePath}"} }; }); c.RouteTemplate = "swagger/{documentName}/swagger.json"; }); applicationBuilder.UseSwaggerUI(c => { c.SwaggerEndpoint("v1/swagger.json", endpointName); c.RoutePrefix = $"swagger"; }); return applicationBuilder; } } }