From 7b38662f8f0265ae7e15a59ad88851acb009a237 Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Thu, 12 Feb 2026 09:59:47 +0100 Subject: [PATCH] Added cache key format for api key validation --- .../Implementations/ApiKeyScheme/ApiKeySchemeHandler.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Moonlight.Api/Implementations/ApiKeyScheme/ApiKeySchemeHandler.cs b/Moonlight.Api/Implementations/ApiKeyScheme/ApiKeySchemeHandler.cs index a721be70..dcc9a483 100644 --- a/Moonlight.Api/Implementations/ApiKeyScheme/ApiKeySchemeHandler.cs +++ b/Moonlight.Api/Implementations/ApiKeyScheme/ApiKeySchemeHandler.cs @@ -16,6 +16,8 @@ public class ApiKeySchemeHandler : AuthenticationHandler private readonly DatabaseRepository ApiKeyRepository; private readonly IMemoryCache MemoryCache; + private const string CacheKeyFormat = $"Moonlight.Api.{nameof(ApiKeySchemeHandler)}.{{0}}"; + public ApiKeySchemeHandler( IOptionsMonitor options, ILoggerFactory logger, @@ -38,7 +40,9 @@ public class ApiKeySchemeHandler : AuthenticationHandler if (authHeaderValue.Length > 32) return AuthenticateResult.Fail("Invalid api key specified"); - if (!MemoryCache.TryGetValue(authHeaderValue, out var apiKey)) + var cacheKey = string.Format(CacheKeyFormat, authHeaderValue); + + if (!MemoryCache.TryGetValue(cacheKey, out var apiKey)) { apiKey = await ApiKeyRepository .Query() @@ -49,7 +53,7 @@ public class ApiKeySchemeHandler : AuthenticationHandler if (apiKey == null) return AuthenticateResult.Fail("Invalid api key specified"); - MemoryCache.Set(authHeaderValue, apiKey, Options.LookupCacheTime); + MemoryCache.Set(cacheKey, apiKey, Options.LookupCacheTime); } else {