mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
27 lines
813 B
C#
27 lines
813 B
C#
using SharpbinV3.Server.Extensions;
|
|
using SharpbinV3.Server.Services;
|
|
|
|
namespace SharpbinV3.Server.Middleware
|
|
{
|
|
public sealed class ApiKeyAuthenticationMiddleware(RequestDelegate next)
|
|
{
|
|
private readonly RequestDelegate _next = next;
|
|
|
|
public async Task InvokeAsync(HttpContext context, ApiKeyService apiKeyService)
|
|
{
|
|
var apiKeyString = context.GetApiKey();
|
|
if (!string.IsNullOrEmpty(apiKeyString))
|
|
{
|
|
var apiKey = await apiKeyService.GetByKeyAsync(apiKeyString);
|
|
if (apiKey != null)
|
|
{
|
|
context.SetApiKeyContext(apiKey);
|
|
await apiKeyService.UpdateLastUsedAsync(apiKey.UUID);
|
|
}
|
|
}
|
|
|
|
await _next(context);
|
|
}
|
|
}
|
|
}
|