Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add route and request parameters logging along with API Key #97

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading