using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace SVSim.EmulatedEntrypoint.Infrastructure;
///
/// Attaches the security requirement to
/// Swagger operations whose action carries . The
/// matching is registered by Program.cs under the
/// same scheme id () so the Swagger UI shows an Authorize dialog and,
/// once populated, sends the header on gated endpoints only.
///
public sealed class AdminSecretOperationFilter : IOperationFilter
{
public const string SchemeId = "AdminSecret";
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var hasAttribute = context.MethodInfo.GetCustomAttributes(true)
.OfType().Any()
|| (context.MethodInfo.DeclaringType?.GetCustomAttributes(true)
.OfType().Any() ?? false);
if (!hasAttribute) return;
operation.Security ??= new List();
operation.Security.Add(new OpenApiSecurityRequirement
{
[new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = SchemeId,
}
}] = Array.Empty()
});
}
}