feat(APIKeyAuthOptions): Schlüsselattribut wird löschbar gemacht.
- isValidKey-Eintrag wird löschbar gemacht. - wenn der Schlüssel null ist und der X-API-Schlüssel nicht existiert, wird die Anfrage authirezred.
This commit is contained in:
parent
2c1abaaf32
commit
cbdd6ee295
@ -5,7 +5,7 @@ namespace WorkFlow.API.Extensions
|
||||
{
|
||||
public static class DIExtensions
|
||||
{
|
||||
public static IServiceCollection AddAPIKeyAuth(this IServiceCollection services, Func<string, bool> isValidKey, string headerName = "X-API-Key")
|
||||
public static IServiceCollection AddAPIKeyAuth(this IServiceCollection services, Func<string?, bool> isValidKey, string headerName = "X-API-Key")
|
||||
=> services.AddSingleton<APIKeyAuthFilter>(provider => new(isValidKey: isValidKey, headerName: headerName));
|
||||
|
||||
public static IServiceCollection AddAPIKeyAuth(this IServiceCollection services, APIKeyAuthOptions options)
|
||||
|
||||
@ -3,16 +3,12 @@ using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WorkFlow.API.Filters
|
||||
{
|
||||
public class APIKeyAuthFilter(Func<string, bool> isValidKey, string headerName = "X-API-Key") : IAuthorizationFilter
|
||||
public class APIKeyAuthFilter(Func<string?, bool> isValidKey, string headerName = "X-API-Key") : IAuthorizationFilter
|
||||
{
|
||||
public void OnAuthorization(AuthorizationFilterContext context)
|
||||
{
|
||||
string? apiKey = context.HttpContext.Request.Headers[headerName];
|
||||
|
||||
if (apiKey is null || !isValidKey(apiKey))
|
||||
{
|
||||
if (!isValidKey(context.HttpContext.Request.Headers[headerName]))
|
||||
context.Result = new UnauthorizedResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
{
|
||||
public class APIKeyAuthOptions
|
||||
{
|
||||
public required string Key { get; init; }
|
||||
public string? Key { get; init; } = null;
|
||||
|
||||
public string HeaderName { get; init; } = "X-API-Key";
|
||||
|
||||
|
||||
@ -39,11 +39,11 @@ try
|
||||
});
|
||||
|
||||
bool disableAPIKeyAuth = config.GetValue<bool>("DisableAPIKeyAuth") && builder.IsDevOrDiP();
|
||||
if (!disableAPIKeyAuth)
|
||||
if (disableAPIKeyAuth)
|
||||
builder.Services.AddAPIKeyAuth(new());
|
||||
else
|
||||
if (config.GetSection("APIKeyAuth").Get<APIKeyAuthOptions>() is APIKeyAuthOptions options)
|
||||
{
|
||||
builder.Services.AddAPIKeyAuth(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.");
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user