1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-05-01 00:34:07 +02:00
Minecraft-Phantom-Panel/Web/Phantom.Web.Services/Authentication/UserSessions.cs
2023-12-05 14:27:55 +01:00

55 lines
1.2 KiB
C#

using System.Collections.Immutable;
using System.Security.Cryptography;
namespace Phantom.Web.Services.Authentication;
sealed class UserSessions {
public UserInfo UserInfo { get; }
private readonly List<ImmutableArray<byte>> tokens = new ();
public UserSessions(UserInfo userInfo) {
UserInfo = userInfo;
}
private UserSessions(UserInfo userInfo, List<ImmutableArray<byte>> tokens) : this(userInfo) {
this.tokens.AddRange(tokens);
}
public UserSessions WithUserInfo(UserInfo user) {
List<ImmutableArray<byte>> tokensCopy;
lock (tokens) {
tokensCopy = new List<ImmutableArray<byte>>(tokens);
}
return new UserSessions(user, tokensCopy);
}
public void AddToken(ImmutableArray<byte> token) {
lock (tokens) {
if (!HasToken(token)) {
tokens.Add(token);
}
}
}
public bool HasToken(ImmutableArray<byte> token) {
return FindTokenIndex(token) != -1;
}
private int FindTokenIndex(ImmutableArray<byte> token) {
lock (tokens) {
return tokens.FindIndex(t => CryptographicOperations.FixedTimeEquals(t.AsSpan(), token.AsSpan()));
}
}
public void RemoveToken(ImmutableArray<byte> token) {
lock (tokens) {
int index = FindTokenIndex(token);
if (index != -1) {
tokens.RemoveAt(index);
}
}
}
}