mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-27 16:09:07 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RadicadeServer.DB;
|
||||
|
||||
namespace RadicadeServer.Utils
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static class TokenRevocation
|
||||
{
|
||||
private static readonly ConcurrentDictionary<long, int> Versions = new();
|
||||
|
||||
|
||||
|
||||
|
||||
public static async Task<bool> IsValidAsync(IServiceProvider services, long accountId, int tokenVersion)
|
||||
{
|
||||
if (Versions.TryGetValue(accountId, out var cached))
|
||||
return cached == tokenVersion;
|
||||
|
||||
var factory = services.GetRequiredService<IDbContextFactory<RecDBContext>>();
|
||||
await using var db = await factory.CreateDbContextAsync();
|
||||
var current = await db.Players
|
||||
.AsNoTracking()
|
||||
.Where(p => p.AccountId == accountId)
|
||||
.Select(p => (int?)p.TokenVersion)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
var stored = current ?? 0;
|
||||
Versions[accountId] = stored;
|
||||
return stored == tokenVersion;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void Bump(long accountId, int newVersion)
|
||||
{
|
||||
Versions.TryRemove(accountId, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user