48 lines
1.1 KiB
C#
48 lines
1.1 KiB
C#
using OwnChar.Data;
|
|
using OwnChar.Model;
|
|
using OwnChar.ServerNew.Api;
|
|
using OwnChar.ServerNew.Api.Plugins;
|
|
using Pilz.Configuration;
|
|
using Pilz.Cryptography;
|
|
|
|
namespace OwnChar.ServerNew;
|
|
|
|
internal class ServerContext(ISettings settings) : IServer, IPluginLoadContextServer
|
|
{
|
|
private readonly Dictionary<string, UserAccount> users = [];
|
|
|
|
public IDataProvider? Data { get; private set; }
|
|
|
|
public ISettings Settings { get; } = settings;
|
|
|
|
public string Login(UserAccount account)
|
|
{
|
|
var secret = new UniquieID(UniquieIDGenerationMode.GenerateOnInit).ID;
|
|
users.Add(secret, account);
|
|
return secret;
|
|
}
|
|
|
|
public void Logout(string secret)
|
|
{
|
|
users.Remove(secret);
|
|
}
|
|
|
|
public bool IsLoggedIn(string secret)
|
|
{
|
|
return users.ContainsKey(secret);
|
|
}
|
|
|
|
public void CheckLogin(string secret)
|
|
{
|
|
if (!IsLoggedIn(secret))
|
|
throw new UnauthorizedAccessException();
|
|
}
|
|
|
|
public UserAccount? GetUser(string secret)
|
|
{
|
|
if (users.TryGetValue(secret, out UserAccount? value))
|
|
return value;
|
|
return null;
|
|
}
|
|
}
|