feat(DisableAPIKeyAuth): Optionen als bool zu appsettings hinzugefügt. Konfiguriert mit app

This commit is contained in:
Developer 02 2024-10-29 11:43:25 +01:00
parent 8038ff74dd
commit 2c1abaaf32
2 changed files with 10 additions and 10 deletions

View File

@ -11,7 +11,6 @@ using WorkFlow.API.Models;
using NLog;
using NLog.Web;
using WorkFlow.API.Extensions;
using Microsoft.Extensions.Configuration;
using WorkFlow.API.Filters;
using Microsoft.OpenApi.Models;
@ -39,14 +38,14 @@ try
Claims = user.ToClaimList().ToDictionary(claim => claim.Type, claim => claim.Value as object)
});
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;
}
bool disableAPIKeyAuth = config.GetValue<bool>("DisableAPIKeyAuth") && builder.IsDevOrDiP();
if (!disableAPIKeyAuth)
if (config.GetSection("APIKeyAuth").Get<APIKeyAuthOptions>() is APIKeyAuthOptions options)
{
builder.Services.AddAPIKeyAuth(options);
}
else
throw new("The API Key Authorization configuration is not available in the app settings, even though the app is not in development or DiP mode and API Key Authorization is not disabled.");
builder.Services.AddControllers();
@ -66,7 +65,7 @@ try
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(setupAct =>
{
if(useApiKey)
if(!disableAPIKeyAuth)
setupAct.OperationFilter<APIKeyAuthHeaderOpFilter>();
if(config.GetSection("OpenApiInfo").Get<OpenApiInfo>() is OpenApiInfo openApiInfo)

View File

@ -1,6 +1,7 @@
{
"DiPMode": true,
"EnableSwagger": true,
"DisableAPIKeyAuth": false,
"Logging": {
"LogLevel": {
"Default": "Information",