insight/src/Api/Insight.Api/Extensions/ServiceExtensions.cs

58 lines
1.9 KiB
C#
Raw Normal View History

2023-09-21 18:58:32 +02:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.OpenApi.Models;
using System.Reflection;
2023-09-22 22:16:56 +02:00
namespace Insight.Api.Hosting;
public static class ServiceExtensions
2023-09-21 18:58:32 +02:00
{
2023-09-22 22:16:56 +02:00
internal static IServiceCollection AddSwaggerServices(this IServiceCollection services, IConfiguration configuration)
2023-09-21 18:58:32 +02:00
{
2023-09-22 22:16:56 +02:00
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(options =>
2023-09-21 18:58:32 +02:00
{
2023-09-22 22:16:56 +02:00
options.SwaggerDoc("v1", new OpenApiInfo
2023-09-21 18:58:32 +02:00
{
2023-09-22 22:16:56 +02:00
Title = "Insight API",
Version = "v1"
});
2023-09-21 18:58:32 +02:00
2023-09-22 22:16:56 +02:00
options.AddSecurityDefinition(name: "Bearer", securityScheme: new OpenApiSecurityScheme
{
Name = "Authorization",
Description = "Enter the Bearer Authorization string as following: `Bearer Generated-JWT-Token`",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer",
BearerFormat = "JWT",
2023-09-21 18:58:32 +02:00
2023-09-22 22:16:56 +02:00
Reference = new OpenApiReference
{
Id = JwtBearerDefaults.AuthenticationScheme,
Type = ReferenceType.SecurityScheme
}
});
2023-09-21 18:58:32 +02:00
2023-09-22 22:16:56 +02:00
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
2023-09-21 18:58:32 +02:00
{
2023-09-22 22:16:56 +02:00
new OpenApiSecurityScheme
2023-09-21 18:58:32 +02:00
{
2023-09-22 22:16:56 +02:00
In = ParameterLocation.Header,
Reference = new OpenApiReference
2023-09-21 18:58:32 +02:00
{
2023-09-22 22:16:56 +02:00
Id = "Bearer",
Type = ReferenceType.SecurityScheme
}
},
new List<string>()
}
2023-09-21 18:58:32 +02:00
});
2023-09-22 22:16:56 +02:00
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
return services;
2023-09-21 18:58:32 +02:00
}
}