Skip to content

Commit

Permalink
Add route and parameters logging to ApiKeyAttribute (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
3Mydlo3 authored Sep 14, 2024
1 parent 660d59a commit 37a2361
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -37,12 +38,20 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
}

var apiKeySha256 = GetApiKeySha256(extractedApiKey);

var actionDetails = GetActionDetails(context.ActionDescriptor, context.ActionArguments);

GetLogger(context).LogInformation("Valid API Key {Key} used for action {Action}", apiKeySha256, "TODO");
GetLogger(context).LogInformation("Valid API Key {Key} used for route {Route}, executing action {Action}", apiKeySha256, context.ActionDescriptor.AttributeRouteInfo?.Template, actionDetails);

await next();
}

private static string GetActionDetails(ActionDescriptor actionDescriptor, IDictionary<string, object?> actionArguments)
{
var parameters = string.Join(", ", actionArguments.Select(x => $"{x.Key}={x.Value}"));
return $"{actionDescriptor.AttributeRouteInfo?.Name}({parameters})";
}

private static IEnumerable<string> GetApiKeys(ActionContext context)
=> context.HttpContext.RequestServices
.GetRequiredService<IApiKeyProvider>()
Expand Down

0 comments on commit 37a2361

Please sign in to comment.