feat(APIKeyAuthHeaderOpFilter): Implementierung der SwaggerGen.IOperationFilter-Schnittstelle, um das API-Schlüsselfeld hinzuzufügen.
- Eigenschaft swagger-description hinzugefügt
This commit is contained in:
parent
67a62d7311
commit
6ea053be36
27
WorkFlow.API/Filters/APIKeyAuthHeaderOpFilter.cs
Normal file
27
WorkFlow.API/Filters/APIKeyAuthHeaderOpFilter.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using WorkFlow.API.Models;
|
||||
|
||||
namespace WorkFlow.API.Filters
|
||||
{
|
||||
public class APIKeyAuthHeaderOpFilter(IOptions<APIKeyAuthOptions> options) : IOperationFilter
|
||||
{
|
||||
private readonly APIKeyAuthOptions apiKeyAuthOptions = options.Value;
|
||||
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
var param = new OpenApiParameter
|
||||
{
|
||||
Name = apiKeyAuthOptions.HeaderName,
|
||||
In = ParameterLocation.Header,
|
||||
Required = true
|
||||
};
|
||||
|
||||
if(apiKeyAuthOptions.SwaggerDescription is not null)
|
||||
param.Description = apiKeyAuthOptions.SwaggerDescription;
|
||||
|
||||
operation.Parameters = [param];
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,5 +5,7 @@
|
||||
public required string Key { get; init; }
|
||||
|
||||
public string HeaderName { get; init; } = "X-API-Key";
|
||||
|
||||
public string? SwaggerDescription { get; init; } = null;
|
||||
}
|
||||
}
|
||||
@ -12,6 +12,7 @@ using NLog;
|
||||
using NLog.Web;
|
||||
using WorkFlow.API.Extensions;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using WorkFlow.API.Filters;
|
||||
|
||||
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
||||
logger.Info("Logging initialized.");
|
||||
@ -37,8 +38,14 @@ try
|
||||
Claims = user.ToClaimList().ToDictionary(claim => claim.Type, claim => claim.Value as object)
|
||||
});
|
||||
|
||||
if (config.GetSection("APIKeyAuth").Get<APIKeyAuthOptions>() is APIKeyAuthOptions options)
|
||||
var apiKeyAuthSection = config.GetSection("APIKeyAuth");
|
||||
var useApiKey = false;
|
||||
if (apiKeyAuthSection.Get<APIKeyAuthOptions>() is APIKeyAuthOptions options)
|
||||
{
|
||||
builder.Services.Configure<APIKeyAuthOptions>(apiKeyAuthSection);
|
||||
builder.Services.AddAPIKeyAuth(options);
|
||||
useApiKey = true;
|
||||
}
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
@ -56,7 +63,11 @@ try
|
||||
});
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
if(useApiKey)
|
||||
c.OperationFilter<APIKeyAuthHeaderOpFilter>();
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@ -65,6 +65,7 @@
|
||||
},
|
||||
"APIKeyAuth": {
|
||||
"Key": "ULbcOUiAXAoCXPviyCGtObZUGnrCHNgDmtNbQNpq5MOhB0EFQn18dObdQ93INNy8xIcnOPMJfEHqOotllELVrJ2R5AjqOfQszT2j00w215GanD3UiJGwFhwmdoNFsmNj",
|
||||
"HeaderName": "X-API-Key"
|
||||
"HeaderName": "X-API-Key",
|
||||
"SwaggerDescription": "Required header for API key authentication. Enter a valid API key."
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user