Files
managing-apps/src/Managing.Application/Users/UserService.cs
2024-05-14 18:50:57 +07:00

94 lines
3.1 KiB
C#

using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Domain.Accounts;
using Managing.Domain.Users;
namespace Managing.Application.Users;
public class UserService : IUserService
{
private readonly IEvmManager _evmManager;
private readonly IUserRepository _userRepository;
private readonly IAccountService _accountService;
private string[] authorizedAddresses = new string[] { "0x6781920674dA695aa5120d95D80c4B1788046806" };
public UserService(
IEvmManager evmManager,
IUserRepository userRepository,
IAccountService accountService)
{
_evmManager = evmManager;
_userRepository = userRepository;
_accountService = accountService;
}
public async Task<User> Authenticate(string name, string address, string message, string signature)
{
var recoveredAddress = _evmManager.VerifySignature(signature, message);
if (!authorizedAddresses.Contains(recoveredAddress))
throw new Exception("Address not authorized");
if (recoveredAddress == null || !recoveredAddress.Equals(address))
throw new Exception("Address not corresponding");
// Check if account exist
var account = await _accountService.GetAccountByKey(recoveredAddress, true, false);
var user = await _userRepository.GetUserByNameAsync(name);
if (account != null && user != null)
{
// User and account found
user.Accounts = _accountService.GetAccountsByUser(user).ToList();
if (!user.Name.Equals(name))
throw new Exception("Name not corresponding");
return user;
}
else
{
// No account and no
account = new Account
{
Name = $"Auth-{new Random().Next(1, 99)}",
Key = recoveredAddress,
Secret = "",
Exchange = Common.Enums.TradingExchanges.Evm,
Type = Common.Enums.AccountType.Auth,
};
if (user != null)
{
_ = await _accountService.CreateAccount(user, account);
user.Accounts = _accountService.GetAccountsByUser(user).ToList();
return user;
}
else
{
// No user found, we create one
// Create user if not existing
user = new User()
{
Name = name,
Accounts = new List<Account> { account },
};
_ = await _accountService.CreateAccount(user, account);
await _userRepository.InsertUserAsync(user);
}
}
return user;
}
public async Task<User> GetUserByAddressAsync(string address)
{
var account = await _accountService.GetAccountByKey(address, true, false);
var user = await _userRepository.GetUserByNameAsync(account.User.Name);
user.Accounts = _accountService.GetAccountsByUser(user).ToList();
return user;
}
}