Authentication Service
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Server.Services.Auth;
|
||||
|
||||
public sealed class SessionRegistry : ISessionRegistry
|
||||
{
|
||||
private readonly ConcurrentDictionary<Guid, (ulong accountId, string token)> _byConn = new();
|
||||
|
||||
public void Bind(Guid connectionId, ulong accountId, string token)
|
||||
=> _byConn[connectionId] = (accountId, token);
|
||||
|
||||
public void Unbind(Guid connectionId)
|
||||
=> _byConn.TryRemove(connectionId, out _);
|
||||
|
||||
public bool IsAuthenticated(Guid connectionId)
|
||||
=> _byConn.ContainsKey(connectionId);
|
||||
|
||||
public bool TryGetAccountId(Guid connectionId, out ulong accountId)
|
||||
{
|
||||
if (_byConn.TryGetValue(connectionId, out var v)) { accountId = v.accountId; return true; }
|
||||
accountId = 0; return false;
|
||||
}
|
||||
|
||||
public bool TryGet(Guid connectionId, out ulong accountId, out string token)
|
||||
{
|
||||
if (_byConn.TryGetValue(connectionId, out var v))
|
||||
{ accountId = v.accountId; token = v.token; return true; }
|
||||
accountId = 0; token = string.Empty; return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user