mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 09:16:34 +00:00
switch to hardware string from object
This commit is contained in:
@@ -35,10 +35,10 @@ public enum ExceptionEnum
|
|||||||
|
|
||||||
WrongEmail = 37,
|
WrongEmail = 37,
|
||||||
|
|
||||||
[Description("HardwareInfo mismatch! You are not authorized to access this resource from this hardware.")]
|
[Description("Hardware mismatch! You are not authorized to access this resource from this hardware.")]
|
||||||
HardwareIdMismatch = 40,
|
HardwareIdMismatch = 40,
|
||||||
|
|
||||||
[Description("HardwareInfo should contain information about this hardware.")]
|
[Description("Hardware should be not empty.")]
|
||||||
BadHardware = 45,
|
BadHardware = 45,
|
||||||
|
|
||||||
[Description("Wrong resource file name.")]
|
[Description("Wrong resource file name.")]
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace Azaion.Common.Entities;
|
|
||||||
|
|
||||||
public class HardwareInfo
|
|
||||||
{
|
|
||||||
public string CPU { get; set; } = null!;
|
|
||||||
public string GPU { get; set; } = null!;
|
|
||||||
public string MacAddress { get; set; } = null!;
|
|
||||||
public string Memory { get; set; } = null!;
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
using Azaion.Common.Entities;
|
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
|
|
||||||
namespace Azaion.Common.Requests;
|
namespace Azaion.Common.Requests;
|
||||||
@@ -6,7 +5,7 @@ namespace Azaion.Common.Requests;
|
|||||||
public class GetResourceRequest
|
public class GetResourceRequest
|
||||||
{
|
{
|
||||||
public string Password { get; set; } = null!;
|
public string Password { get; set; } = null!;
|
||||||
public HardwareInfo Hardware { get; set; } = null!;
|
public string Hardware { get; set; } = null!;
|
||||||
public string FileName { get; set; } = null!;
|
public string FileName { get; set; } = null!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ public static class Security
|
|||||||
public static string ToHash(this string str) =>
|
public static string ToHash(this string str) =>
|
||||||
Convert.ToBase64String(SHA384.HashData(Encoding.UTF8.GetBytes(str)));
|
Convert.ToBase64String(SHA384.HashData(Encoding.UTF8.GetBytes(str)));
|
||||||
|
|
||||||
public static string GetHWHash(HardwareInfo hardware) =>
|
public static string GetHWHash(string hardware) =>
|
||||||
$"Azaion_{hardware.MacAddress}_{hardware.CPU}_{hardware.GPU}".ToHash();
|
$"Azaion_{hardware}_%$$$)0_".ToHash();
|
||||||
|
|
||||||
public static string GetApiEncryptionKey(string email, string password, string? hardwareHash) =>
|
public static string GetApiEncryptionKey(string email, string password, string? hardwareHash) =>
|
||||||
$"{email}-{password}-{hardwareHash}-#%@AzaionKey@%#---".ToHash();
|
$"{email}-{password}-{hardwareHash}-#%@AzaionKey@%#---".ToHash();
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public interface IUserService
|
|||||||
Task RegisterUser(RegisterUserRequest request, CancellationToken cancellationToken = default);
|
Task RegisterUser(RegisterUserRequest request, CancellationToken cancellationToken = default);
|
||||||
Task<User> ValidateUser(LoginRequest request, CancellationToken cancellationToken = default);
|
Task<User> ValidateUser(LoginRequest request, CancellationToken cancellationToken = default);
|
||||||
Task<User?> GetByEmail(string? email, CancellationToken cancellationToken = default);
|
Task<User?> GetByEmail(string? email, CancellationToken cancellationToken = default);
|
||||||
Task UpdateHardware(string email, HardwareInfo? hardwareInfo = null, CancellationToken cancellationToken = default);
|
Task UpdateHardware(string email, string? hardware = null, CancellationToken cancellationToken = default);
|
||||||
Task UpdateQueueOffsets(string email, UserQueueOffsets queueOffsets, CancellationToken cancellationToken = default);
|
Task UpdateQueueOffsets(string email, UserQueueOffsets queueOffsets, CancellationToken cancellationToken = default);
|
||||||
Task<IEnumerable<User>> GetUsers(string? searchEmail, RoleEnum? searchRole, CancellationToken cancellationToken);
|
Task<IEnumerable<User>> GetUsers(string? searchEmail, RoleEnum? searchRole, CancellationToken cancellationToken);
|
||||||
Task<string> CheckHardwareHash(User user, GetResourceRequest request);
|
Task<string> CheckHardwareHash(User user, GetResourceRequest request);
|
||||||
@@ -39,10 +39,15 @@ public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<User?> GetByEmail(string? email, CancellationToken cancellationToken = default) =>
|
public async Task<User?> GetByEmail(string? email, CancellationToken cancellationToken = default)
|
||||||
await cache.GetFromCacheAsync(User.GetCacheKey(email),
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(email)) throw new ArgumentNullException(nameof(email));
|
||||||
|
|
||||||
|
return await cache.GetFromCacheAsync(User.GetCacheKey(email),
|
||||||
async () => await dbFactory.Run(async db =>
|
async () => await dbFactory.Run(async db =>
|
||||||
await db.Users.FirstOrDefaultAsync(x => x.Email == email, cancellationToken)));
|
await db.Users.FirstOrDefaultAsync(x => x.Email == email, cancellationToken)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<User> ValidateUser(LoginRequest request, CancellationToken cancellationToken = default) =>
|
public async Task<User> ValidateUser(LoginRequest request, CancellationToken cancellationToken = default) =>
|
||||||
await dbFactory.Run(async db =>
|
await dbFactory.Run(async db =>
|
||||||
@@ -58,17 +63,12 @@ public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
public async Task UpdateHardware(string email, HardwareInfo? hardware = null, CancellationToken cancellationToken = default)
|
public async Task UpdateHardware(string email, string? hardware = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
await dbFactory.RunAdmin(async db =>
|
await dbFactory.RunAdmin(async db =>
|
||||||
{
|
{
|
||||||
var hardwareStr = hardware == null ? "" : JsonConvert.SerializeObject(hardware);
|
|
||||||
|
|
||||||
await db.Users.UpdateAsync(x => x.Email == email,
|
await db.Users.UpdateAsync(x => x.Email == email,
|
||||||
u => new User
|
u => new User { Hardware = hardware }, token: cancellationToken);
|
||||||
{
|
|
||||||
Hardware = hardwareStr
|
|
||||||
}, token: cancellationToken);
|
|
||||||
});
|
});
|
||||||
cache.Invalidate(User.GetCacheKey(email));
|
cache.Invalidate(User.GetCacheKey(email));
|
||||||
}
|
}
|
||||||
@@ -111,8 +111,7 @@ public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
|||||||
return requestHWHash;
|
return requestHWHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
var userHW = JsonConvert.DeserializeObject<HardwareInfo>(user.Hardware);
|
var userHWHash = Security.GetHWHash(user.Hardware);
|
||||||
var userHWHash = Security.GetHWHash(userHW!);
|
|
||||||
if (userHWHash != requestHWHash)
|
if (userHWHash != requestHWHash)
|
||||||
throw new BusinessException(ExceptionEnum.HardwareIdMismatch);
|
throw new BusinessException(ExceptionEnum.HardwareIdMismatch);
|
||||||
return userHWHash;
|
return userHWHash;
|
||||||
|
|||||||
Reference in New Issue
Block a user