2020-12-20 22:31:53 +02:00
using Microsoft.AspNetCore.Builder ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.OpenApi.Models ;
using NDB.Extensions.Swagger.Constants ;
using NDB.Extensions.Swagger.Filters ;
using NDB.Extensions.Swagger.ReverseProxy ;
using Swashbuckle.AspNetCore.SwaggerGen ;
using System ;
using System.Collections.Generic ;
namespace NDB.Extensions.Swagger
{
public static class SwaggerExtensions
{
public static IServiceCollection AddSwagger ( this IServiceCollection services , string title , AuthorizationType authorizationType = AuthorizationType . Basic )
2021-06-26 12:49:10 +03:00
{
services . AddSwaggerGen ( c = >
{
c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = title , Version = "v1" } ) ;
c . SetAuthorization ( authorizationType ) ;
2022-03-11 18:26:49 +02:00
c . CustomSchemaIds ( type = > type . ToString ( ) ) ;
2021-06-26 12:49:10 +03:00
} ) ;
return services ;
}
public static IServiceCollection AddSwaggerWithFilters ( this IServiceCollection services , string title , AuthorizationType authorizationType = AuthorizationType . Basic )
2020-12-20 22:31:53 +02:00
{
services . AddSwaggerGen ( c = >
{
c . SwaggerDoc ( "v1" ,
new OpenApiInfo
{
Title = title ,
Version = "v1"
} ) ;
c . SetAuthorization ( authorizationType ) ;
c . OperationFilter < PathParamsOperationFilter > ( ) ;
c . SchemaFilter < DtoSchemaFilter > ( ) ;
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 :
2021-06-26 21:55:04 +03:00
options . SetAuthorization ( authorizationType . ToString ( ) , "Basic" ) ;
break ;
2020-12-20 22:31:53 +02:00
2021-06-26 21:55:04 +03:00
case AuthorizationType . InhouseIdentity :
options . SetAuthorization ( authorizationType . ToString ( ) , "Identity" ) ;
2020-12-20 22:31:53 +02:00
break ;
default :
throw new NotImplementedException ( $"Swagger extensions: Authorization type '{authorizationType}' is not implemented." ) ;
}
}
2021-06-26 21:55:04 +03:00
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 < string > ( )
}
} ) ;
}
2020-12-20 22:31:53 +02:00
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 < OpenApiServer >
{
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 ;
}
}
}