2020-12-20 03:22:27 +02:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace IdentityServer.Api.Swagger
|
|
|
|
|
{
|
|
|
|
|
public static class SwaggerExtensions
|
|
|
|
|
{
|
|
|
|
|
public static IServiceCollection AddSwagger(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddSwaggerGen(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerDoc("v1",
|
|
|
|
|
new OpenApiInfo
|
|
|
|
|
{
|
2020-12-20 11:28:25 +02:00
|
|
|
|
Title = "Identity Server API",
|
2020-12-20 03:22:27 +02:00
|
|
|
|
Version = "v1"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
c.OperationFilter<PathParamsOperationFilter>();
|
|
|
|
|
c.SchemaFilter<DtoSchemaFilter>();
|
|
|
|
|
c.CustomSchemaIds(type => type.ToString());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IApplicationBuilder ConfigureSwagger(this IApplicationBuilder applicationBuilder)
|
|
|
|
|
{
|
|
|
|
|
applicationBuilder.UseSwagger(c =>
|
|
|
|
|
{
|
|
|
|
|
c.PreSerializeFilters.Add((swagger, httpRequest) =>
|
|
|
|
|
{
|
|
|
|
|
var (host, basePath, scheme) = GetUrlComponents(httpRequest);
|
|
|
|
|
|
|
|
|
|
swagger.Servers = new List<OpenApiServer>
|
|
|
|
|
{
|
|
|
|
|
new OpenApiServer {Url = $"{scheme}://{host}{basePath}"}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
c.RouteTemplate = "swagger/{documentName}/swagger.json";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
applicationBuilder.UseSwaggerUI(c =>
|
|
|
|
|
{
|
2020-12-20 11:28:25 +02:00
|
|
|
|
c.SwaggerEndpoint("v1/swagger.json", "IdentityServer API");
|
2020-12-20 03:22:27 +02:00
|
|
|
|
c.RoutePrefix = $"swagger";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return applicationBuilder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static (string host, string basePath, string scheme) GetUrlComponents(HttpRequest request)
|
|
|
|
|
{
|
|
|
|
|
var host = ExtractHost(request);
|
|
|
|
|
var basePath = ExtractBasePath(request);
|
|
|
|
|
var scheme = ExtractScheme(request);
|
|
|
|
|
|
|
|
|
|
return (host, basePath, scheme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ExtractHost(HttpRequest request)
|
|
|
|
|
{
|
|
|
|
|
if (request.Headers.ContainsKey("X-Forwarded-Host"))
|
|
|
|
|
return request.Headers["X-Forwarded-Host"].First();
|
|
|
|
|
|
|
|
|
|
return request.Host.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ExtractBasePath(HttpRequest request)
|
|
|
|
|
{
|
|
|
|
|
if (request.Headers.ContainsKey("X-Forwarded-PathBase"))
|
|
|
|
|
return request.Headers["X-Forwarded-PathBase"].First();
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ExtractScheme(HttpRequest request)
|
|
|
|
|
{
|
|
|
|
|
return request.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? request.Scheme;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|