mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 08:56:33 +00:00
add check resource endpoint
This commit is contained in:
+11
-1
@@ -186,7 +186,7 @@ app.MapPost("/resources/get/{dataFolder?}", //Need to have POST method for secur
|
|||||||
if (user == null)
|
if (user == null)
|
||||||
throw new UnauthorizedAccessException();
|
throw new UnauthorizedAccessException();
|
||||||
|
|
||||||
var hwHash = await userService.CheckHardwareHash(user, request);
|
var hwHash = await userService.CheckHardwareHash(user, request.Hardware);
|
||||||
|
|
||||||
var key = Security.GetApiEncryptionKey(user.Email, request.Password, hwHash);
|
var key = Security.GetApiEncryptionKey(user.Email, request.Password, hwHash);
|
||||||
var stream = await resourcesService.GetEncryptedResource(dataFolder, request.FileName, key, ct);
|
var stream = await resourcesService.GetEncryptedResource(dataFolder, request.FileName, key, ct);
|
||||||
@@ -208,6 +208,16 @@ app.MapGet("/resources/get-installer/{dataFolder?}",
|
|||||||
}).RequireAuthorization()
|
}).RequireAuthorization()
|
||||||
.WithOpenApi(op => new OpenApiOperation(op){ Summary = "Gets latest installer"});
|
.WithOpenApi(op => new OpenApiOperation(op){ Summary = "Gets latest installer"});
|
||||||
|
|
||||||
|
app.MapPost("/resources/check",
|
||||||
|
async (CheckResourceRequest request, IAuthService authService, IUserService userService) =>
|
||||||
|
{
|
||||||
|
var user = await authService.GetCurrentUser();
|
||||||
|
if (user == null)
|
||||||
|
throw new UnauthorizedAccessException();
|
||||||
|
await userService.CheckHardwareHash(user, request.Hardware);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
app.UseExceptionHandler(_ => {});
|
app.UseExceptionHandler(_ => {});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
@@ -2,6 +2,11 @@ using FluentValidation;
|
|||||||
|
|
||||||
namespace Azaion.Common.Requests;
|
namespace Azaion.Common.Requests;
|
||||||
|
|
||||||
|
public class CheckResourceRequest
|
||||||
|
{
|
||||||
|
public string Hardware { get; set; } = null!;
|
||||||
|
}
|
||||||
|
|
||||||
public class GetResourceRequest
|
public class GetResourceRequest
|
||||||
{
|
{
|
||||||
public string Password { get; set; } = null!;
|
public string Password { get; set; } = null!;
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public interface IUserService
|
|||||||
Task UpdateHardware(string email, string? hardware = null, CancellationToken ct = default);
|
Task UpdateHardware(string email, string? hardware = null, CancellationToken ct = default);
|
||||||
Task UpdateQueueOffsets(string email, UserQueueOffsets queueOffsets, CancellationToken ct = default);
|
Task UpdateQueueOffsets(string email, UserQueueOffsets queueOffsets, CancellationToken ct = default);
|
||||||
Task<IEnumerable<User>> GetUsers(string? searchEmail, RoleEnum? searchRole, CancellationToken ct = default);
|
Task<IEnumerable<User>> GetUsers(string? searchEmail, RoleEnum? searchRole, CancellationToken ct = default);
|
||||||
Task<string> CheckHardwareHash(User user, GetResourceRequest request, CancellationToken ct = default);
|
Task<string> CheckHardwareHash(User user, string hardware, CancellationToken ct = default);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
||||||
@@ -99,14 +99,14 @@ public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
|||||||
u => u.Role == searchRole)
|
u => u.Role == searchRole)
|
||||||
.ToListAsync(token: ct));
|
.ToListAsync(token: ct));
|
||||||
|
|
||||||
public async Task<string> CheckHardwareHash(User user, GetResourceRequest request, CancellationToken ct = default)
|
public async Task<string> CheckHardwareHash(User user, string hardware, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var requestHWHash = Security.GetHWHash(request.Hardware);
|
var requestHWHash = Security.GetHWHash(hardware);
|
||||||
|
|
||||||
//For the new users Hardware would be empty, fill it with actual hardware on the very first request
|
//For the new users Hardware would be empty, fill it with actual hardware on the very first request
|
||||||
if (string.IsNullOrEmpty(user.Hardware))
|
if (string.IsNullOrEmpty(user.Hardware))
|
||||||
{
|
{
|
||||||
await UpdateHardware(user.Email, request.Hardware, ct);
|
await UpdateHardware(user.Email, hardware, ct);
|
||||||
cache.Invalidate(User.GetCacheKey(user.Email));
|
cache.Invalidate(User.GetCacheKey(user.Email));
|
||||||
return requestHWHash;
|
return requestHWHash;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user