mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 09:06:33 +00:00
don't send hardware hash, calc on the api
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Azaion.Common.Entities;
|
||||
|
||||
namespace Azaion.Services;
|
||||
|
||||
@@ -10,7 +11,10 @@ public static class Security
|
||||
public static string ToHash(this string str) =>
|
||||
Convert.ToBase64String(SHA384.HashData(Encoding.UTF8.GetBytes(str)));
|
||||
|
||||
public static string MakeEncryptionKey(string email, string password, string? hardwareHash) =>
|
||||
public static string GetHWHash(HardwareInfo hardware) =>
|
||||
$"Azaion_{hardware.MacAddress}_{hardware.CPU}_{hardware.GPU}".ToHash();
|
||||
|
||||
public static string GetApiEncryptionKey(string email, string password, string? hardwareHash) =>
|
||||
$"{email}-{password}-{hardwareHash}-#%@AzaionKey@%#---".ToHash();
|
||||
|
||||
public static async Task EncryptTo(this Stream stream, Stream toStream, string key, CancellationToken cancellationToken = default)
|
||||
|
||||
@@ -16,7 +16,7 @@ public interface IUserService
|
||||
Task<User?> GetByEmail(string email, CancellationToken cancellationToken = default);
|
||||
Task UpdateHardware(string email, HardwareInfo hardwareInfo, CancellationToken cancellationToken = default);
|
||||
Task<IEnumerable<User>> GetUsers(string? searchEmail, RoleEnum? searchRole, CancellationToken cancellationToken);
|
||||
Task CheckHardware(User user, GetResourceRequest request);
|
||||
Task<string> CheckHardwareHash(User user, GetResourceRequest request);
|
||||
}
|
||||
|
||||
public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
||||
@@ -62,7 +62,8 @@ public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
||||
});
|
||||
|
||||
|
||||
public async Task UpdateHardware(string email, HardwareInfo hardware, CancellationToken cancellationToken = default) =>
|
||||
public async Task UpdateHardware(string email, HardwareInfo hardware, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await dbFactory.RunAdmin(async db =>
|
||||
{
|
||||
var hardwareStr = JsonConvert.SerializeObject(hardware);
|
||||
@@ -70,11 +71,11 @@ public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
||||
await db.Users.UpdateAsync(x => x.Email == email,
|
||||
u => new User
|
||||
{
|
||||
Hardware = hardwareStr,
|
||||
HardwareHash = hardware.Hash
|
||||
Hardware = hardwareStr
|
||||
}, token: cancellationToken);
|
||||
});
|
||||
|
||||
cache.Invalidate(User.GetCacheKey(email));
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> GetUsers(string? searchEmail, RoleEnum? searchRole, CancellationToken cancellationToken) =>
|
||||
await dbFactory.Run(async db =>
|
||||
@@ -85,23 +86,22 @@ public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
||||
u => u.Role == searchRole)
|
||||
.ToListAsync(token: cancellationToken));
|
||||
|
||||
public async Task CheckHardware(User user, GetResourceRequest request)
|
||||
public async Task<string> CheckHardwareHash(User user, GetResourceRequest request)
|
||||
{
|
||||
if (string.IsNullOrEmpty(user.HardwareHash))
|
||||
var requestHWHash = Security.GetHWHash(request.Hardware);
|
||||
|
||||
//For the new users Hardware would be empty, fill it with actual hardware on the very first request
|
||||
if (string.IsNullOrEmpty(user.Hardware))
|
||||
{
|
||||
await UpdateHardware(user.Email, request.Hardware);
|
||||
user.HardwareHash = request.Hardware.Hash;
|
||||
cache.Invalidate(User.GetCacheKey(user.Email));
|
||||
return requestHWHash;
|
||||
}
|
||||
|
||||
var hwHash = await dbFactory.Run(async db =>
|
||||
await db.Users
|
||||
.Where(x => x.Email == user.Email)
|
||||
.Select(x => x.HardwareHash)
|
||||
.FirstOrDefaultAsync());
|
||||
if (hwHash != user.HardwareHash)
|
||||
user.HardwareHash = hwHash;
|
||||
|
||||
if (user.HardwareHash != request.Hardware.Hash)
|
||||
var userHW = JsonConvert.DeserializeObject<HardwareInfo>(user.Hardware);
|
||||
var userHWHash = Security.GetHWHash(userHW!);
|
||||
if (userHWHash != requestHWHash)
|
||||
throw new BusinessException(ExceptionEnum.HardwareIdMismatch);
|
||||
return userHWHash;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user