mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-12-05 16:16:34 +00:00
45 lines
1.9 KiB
C#
45 lines
1.9 KiB
C#
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.OpenApi;
|
|
using Microsoft.OpenApi;
|
|
|
|
namespace Milimoe.FunGame.WebAPI.Architecture
|
|
{
|
|
public class SecurityDocumentTransformer(IAuthenticationSchemeProvider authenticationSchemeProvider) : IOpenApiDocumentTransformer
|
|
{
|
|
public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
|
|
{
|
|
IEnumerable<AuthenticationScheme> authenticationSchemes = await authenticationSchemeProvider.GetAllSchemesAsync();
|
|
if (authenticationSchemes.Any(authScheme => authScheme.Name == "Bearer"))
|
|
{
|
|
Dictionary<string, IOpenApiSecurityScheme> securitySchemes = new()
|
|
{
|
|
["Bearer"] = new OpenApiSecurityScheme
|
|
{
|
|
Type = SecuritySchemeType.Http,
|
|
Scheme = "bearer",
|
|
BearerFormat = "JWT",
|
|
In = ParameterLocation.Header
|
|
}
|
|
};
|
|
document.Components ??= new OpenApiComponents();
|
|
document.Components.SecuritySchemes = securitySchemes;
|
|
|
|
foreach (KeyValuePair<HttpMethod, OpenApiOperation> operation in document.Paths.Values.SelectMany(path => path.Operations!))
|
|
{
|
|
operation.Value.Security ??= [];
|
|
operation.Value.Security.Add(new OpenApiSecurityRequirement
|
|
{
|
|
[new OpenApiSecuritySchemeReference("Bearer", document)] = []
|
|
});
|
|
}
|
|
}
|
|
document.Info = new()
|
|
{
|
|
Title = "FunGame Web API",
|
|
Version = "v1",
|
|
Description = "Welcome to FunGame Web API document."
|
|
};
|
|
}
|
|
}
|
|
}
|