mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat(backend): implement api keys; rename token -> verificationToken in paste creation api
api keys can bypass authentication and captchas/verification
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user