mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-04 23:29:00 +08:00
Initial commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.OpenApi;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
namespace Foxel;
|
||||
|
||||
public sealed class BearerSecuritySchemeTransformer(IAuthenticationSchemeProvider authenticationSchemeProvider)
|
||||
: IOpenApiDocumentTransformer
|
||||
{
|
||||
public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var authenticationSchemes = await authenticationSchemeProvider.GetAllSchemesAsync();
|
||||
if (authenticationSchemes.Any(authScheme => authScheme.Name == "Bearer"))
|
||||
{
|
||||
var requirements = new Dictionary<string,
|
||||
OpenApiSecurityScheme>
|
||||
{
|
||||
["Bearer"] = new()
|
||||
{
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = "bearer",
|
||||
In = ParameterLocation.Header,
|
||||
BearerFormat = "Json Web Token"
|
||||
}
|
||||
};
|
||||
document.Components ??= new OpenApiComponents();
|
||||
document.Components.SecuritySchemes = requirements;
|
||||
|
||||
foreach (var operation in document.Paths.Values.SelectMany(path => path.Operations))
|
||||
{
|
||||
operation.Value.Security.Add(new OpenApiSecurityRequirement
|
||||
{
|
||||
[new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Id = "Bearer", Type = ReferenceType.SecurityScheme
|
||||
}
|
||||
}] = []
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user