From 10c0593120d73ab35043c542ecb7da63ea185475 Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 28 Feb 2026 21:52:36 -0500 Subject: [PATCH] Single-session login policy + fix deck editor card search Auth (single-session policy): - AuthGrpcHandler.Login now calls RevokeAllSessionsForUserAsync before CreateSessionAsync. When a user logs in on a new device, all existing sessions are revoked in Redis (token keys deleted) and marked RevokedAt in Postgres. The old device will fail auth.hello on its next reconnect and return to the login screen automatically. Admin (PrebuiltDeckEditor): - SearchCardsAsync and SearchArchSpiritsAsync were using .Contains() which compiles to a case-sensitive PostgreSQL LIKE. Changed to EF.Functions.ILike so card names can be found regardless of capitalisation. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../Pages/Bazaar/PrebuiltDeckEditor.razor | 6 ++-- .../src/SOR.Auth/Handlers/AuthGrpcHandler.cs | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Server/src/SOR.Admin/Components/Pages/Bazaar/PrebuiltDeckEditor.razor b/Server/src/SOR.Admin/Components/Pages/Bazaar/PrebuiltDeckEditor.razor index 183b54d..8816f6e 100644 --- a/Server/src/SOR.Admin/Components/Pages/Bazaar/PrebuiltDeckEditor.razor +++ b/Server/src/SOR.Admin/Components/Pages/Bazaar/PrebuiltDeckEditor.razor @@ -294,8 +294,9 @@ else private async Task SearchCardsAsync() { if (string.IsNullOrWhiteSpace(_cardSearch)) return; + var term = _cardSearch.Trim(); _cardResults = await CatalogDb.CardDefinitions - .Where(c => c.Name.Contains(_cardSearch)) + .Where(c => EF.Functions.ILike(c.Name, $"%{term}%")) .OrderBy(c => c.Name) .Take(10) .ToListAsync(); @@ -304,8 +305,9 @@ else private async Task SearchArchSpiritsAsync() { if (string.IsNullOrWhiteSpace(_archSearch)) return; + var term = _archSearch.Trim(); _archResults = await CatalogDb.CardDefinitions - .Where(c => c.CardType == CardType.ArchSpirit && c.Name.Contains(_archSearch)) + .Where(c => c.CardType == CardType.ArchSpirit && EF.Functions.ILike(c.Name, $"%{term}%")) .OrderBy(c => c.Name) .Take(10) .ToListAsync(); diff --git a/Server/src/SOR.Auth/Handlers/AuthGrpcHandler.cs b/Server/src/SOR.Auth/Handlers/AuthGrpcHandler.cs index 7297334..fc58d87 100644 --- a/Server/src/SOR.Auth/Handlers/AuthGrpcHandler.cs +++ b/Server/src/SOR.Auth/Handlers/AuthGrpcHandler.cs @@ -72,6 +72,11 @@ public sealed class AuthGrpcHandler : AuthService.AuthServiceBase user.LastLoginAt = DateTimeOffset.UtcNow; await _userManager.UpdateAsync(user); + // Single-session policy: revoke all active sessions before issuing a new one. + // This ensures only one device is logged in at a time. The old device will + // receive an auth failure on its next auth.hello and return to the login screen. + await RevokeAllSessionsForUserAsync(user.Id); + var token = await CreateSessionAsync(user.Id, context); _logger.LogInformation("Login succeeded for account {AccountId}", user.Id); return new LoginResponse @@ -348,6 +353,37 @@ public sealed class AuthGrpcHandler : AuthService.AuthServiceBase // ── Helpers ─────────────────────────────────────────────────────────────── + /// + /// Revokes all active sessions for the given account in both Redis and Postgres. + /// Called before issuing a new session token so only one device is ever logged in. + /// + private async Task RevokeAllSessionsForUserAsync(Guid accountId) + { + var now = DateTimeOffset.UtcNow; + var cache = _redis.GetDatabase(); + var userKey = RedisKeys.UserSessions(accountId.ToString()); + + var members = await cache.SetMembersAsync(userKey); + foreach (var member in members) + await cache.KeyDeleteAsync(RedisKeys.Session(member!)); + await cache.KeyDeleteAsync(userKey); + + var sessions = await _db.AccountSessions + .Where(s => s.AccountId == accountId && s.RevokedAt == null && s.ExpiresAt > now) + .ToListAsync(); + + foreach (var s in sessions) + s.RevokedAt = now; + + if (sessions.Count > 0) + { + await _db.SaveChangesAsync(); + _logger.LogInformation( + "Revoked {Count} existing session(s) for account {AccountId} on new login", + sessions.Count, accountId); + } + } + /// /// Generates a cryptographically random session token, persists the hash to Postgres, /// and caches the raw token → accountId mapping in Redis.