mirror of
https://github.com/Kareadita/Kavita.git
synced 2026-06-06 06:45:18 -04:00
More Polish (#4336)
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com>
This commit is contained in:
@@ -8,9 +8,12 @@ using System.Threading.Tasks;
|
||||
using API.Constants;
|
||||
using API.Data;
|
||||
using API.Entities.Progress;
|
||||
using API.Services;
|
||||
using Hangfire;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
@@ -26,6 +29,7 @@ public class AuthKeyAuthenticationHandler : AuthenticationHandler<AuthKeyAuthent
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly HybridCache _cache;
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
|
||||
private static readonly HybridCacheEntryOptions CacheOptions = new()
|
||||
{
|
||||
@@ -33,16 +37,24 @@ private readonly IUnitOfWork _unitOfWork;
|
||||
LocalCacheExpiration = TimeSpan.FromMinutes(15)
|
||||
};
|
||||
|
||||
private static readonly MemoryCacheEntryOptions AuthKeyCacheOptions = new MemoryCacheEntryOptions()
|
||||
{
|
||||
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5),
|
||||
Size = 1
|
||||
};
|
||||
|
||||
public AuthKeyAuthenticationHandler(
|
||||
IOptionsMonitor<AuthKeyAuthenticationOptions> options,
|
||||
ILoggerFactory logger,
|
||||
UrlEncoder encoder,
|
||||
IUnitOfWork unitOfWork,
|
||||
HybridCache cache)
|
||||
HybridCache cache,
|
||||
IMemoryCache memoryCache)
|
||||
: base(options, logger, encoder)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_cache = cache;
|
||||
_memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
@@ -86,6 +98,9 @@ private readonly IUnitOfWork _unitOfWork;
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
var ticket = new AuthenticationTicket(principal, Scheme.Name);
|
||||
|
||||
// Mark the auth key as last accessed
|
||||
EnqueueAccessUpdateIfNeeded(apiKey);
|
||||
|
||||
return AuthenticateResult.Success(ticket);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -122,4 +137,19 @@ private readonly IUnitOfWork _unitOfWork;
|
||||
{
|
||||
return $"authKey_{keyValue}";
|
||||
}
|
||||
|
||||
private void EnqueueAccessUpdateIfNeeded(string apiKey)
|
||||
{
|
||||
var throttleKey = $"authkey_access_{apiKey}";
|
||||
|
||||
if (_memoryCache.TryGetValue(throttleKey, out _))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Mark as recently queued
|
||||
_memoryCache.Set(throttleKey, true, AuthKeyCacheOptions);
|
||||
|
||||
BackgroundJob.Enqueue<IAuthKeyService>(s => s.UpdateLastAccessedAsync(apiKey));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user