fix(auth): use temp session store in tests

This commit is contained in:
2026-04-18 22:48:46 -04:00
parent 00a6cbc20e
commit b23678e95a
@@ -33,22 +33,33 @@ public class AdminAuthSessionService
public static readonly TimeSpan DefaultSessionLifetime = TimeSpan.FromHours(12); public static readonly TimeSpan DefaultSessionLifetime = TimeSpan.FromHours(12);
public static readonly TimeSpan PersistentSessionLifetime = TimeSpan.FromDays(30); public static readonly TimeSpan PersistentSessionLifetime = TimeSpan.FromDays(30);
private const string SessionStoreFilePath = "/app/cache/admin-auth/sessions.protected";
private readonly ConcurrentDictionary<string, AdminAuthSession> _sessions = new(); private readonly ConcurrentDictionary<string, AdminAuthSession> _sessions = new();
private readonly IDataProtector _protector; private readonly IDataProtector _protector;
private readonly ILogger<AdminAuthSessionService> _logger; private readonly ILogger<AdminAuthSessionService> _logger;
private readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); private readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
private readonly object _persistLock = new(); private readonly object _persistLock = new();
private readonly string _sessionStoreFilePath;
public AdminAuthSessionService( public AdminAuthSessionService(
IDataProtectionProvider dataProtectionProvider, IDataProtectionProvider dataProtectionProvider,
ILogger<AdminAuthSessionService> logger) ILogger<AdminAuthSessionService> logger)
: this(
dataProtectionProvider,
logger,
"/app/cache/admin-auth/sessions.protected")
{
}
private AdminAuthSessionService(
IDataProtectionProvider dataProtectionProvider,
ILogger<AdminAuthSessionService> logger,
string sessionStoreFilePath)
{ {
_protector = dataProtectionProvider.CreateProtector("allstarr.admin.auth.sessions.v1"); _protector = dataProtectionProvider.CreateProtector("allstarr.admin.auth.sessions.v1");
_logger = logger; _logger = logger;
_sessionStoreFilePath = sessionStoreFilePath;
var directory = Path.GetDirectoryName(SessionStoreFilePath); var directory = Path.GetDirectoryName(_sessionStoreFilePath);
if (!string.IsNullOrWhiteSpace(directory)) if (!string.IsNullOrWhiteSpace(directory))
{ {
Directory.CreateDirectory(directory); Directory.CreateDirectory(directory);
@@ -58,12 +69,18 @@ public class AdminAuthSessionService
} }
public AdminAuthSessionService(ILogger<AdminAuthSessionService> logger) public AdminAuthSessionService(ILogger<AdminAuthSessionService> logger)
: this(CreateFallbackDataProtectionProvider(), logger) : this(
CreateFallbackDataProtectionProvider(),
logger,
Path.Combine(Path.GetTempPath(), "allstarr-admin-auth", "sessions.protected"))
{ {
} }
public AdminAuthSessionService() public AdminAuthSessionService()
: this(CreateFallbackDataProtectionProvider(), NullLogger<AdminAuthSessionService>.Instance) : this(
CreateFallbackDataProtectionProvider(),
NullLogger<AdminAuthSessionService>.Instance,
Path.Combine(Path.GetTempPath(), "allstarr-admin-auth", "sessions.protected"))
{ {
} }
@@ -158,12 +175,12 @@ public class AdminAuthSessionService
{ {
try try
{ {
if (!File.Exists(SessionStoreFilePath)) if (!File.Exists(_sessionStoreFilePath))
{ {
return; return;
} }
var protectedPayload = File.ReadAllText(SessionStoreFilePath); var protectedPayload = File.ReadAllText(_sessionStoreFilePath);
if (string.IsNullOrWhiteSpace(protectedPayload)) if (string.IsNullOrWhiteSpace(protectedPayload))
{ {
return; return;
@@ -235,7 +252,7 @@ public class AdminAuthSessionService
var json = JsonSerializer.Serialize(activeSessions, _jsonOptions); var json = JsonSerializer.Serialize(activeSessions, _jsonOptions);
var protectedPayload = _protector.Protect(json); var protectedPayload = _protector.Protect(json);
File.WriteAllText(SessionStoreFilePath, protectedPayload); File.WriteAllText(_sessionStoreFilePath, protectedPayload);
} }
catch (Exception ex) catch (Exception ex)
{ {