mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 10:36:33 +00:00
get resource works
This commit is contained in:
+18
-6
@@ -6,6 +6,7 @@ using Azaion.Common.Entities;
|
||||
using Azaion.Common.Requests;
|
||||
using Azaion.Services;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
@@ -78,7 +79,6 @@ builder.Services.AddSingleton<IDbFactory, DbFactory>();
|
||||
|
||||
builder.Services.AddValidatorsFromAssemblyContaining<RegisterUserValidator>();
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
@@ -110,20 +110,32 @@ app.MapPost("/resources",
|
||||
.RequireAuthorization(apiAdminPolicy)
|
||||
.DisableAntiforgery();
|
||||
|
||||
app.MapPost("/resources/reset-hardware",
|
||||
async (string email, IUserService userService, CancellationToken cancellationToken)
|
||||
=> await userService.UpdateHardwareId(email, null!, cancellationToken));
|
||||
|
||||
|
||||
app.MapPost("/resources/get",
|
||||
async (GetResourceRequest request, IAuthService authService, IUserService userService, IResourcesService resourcesService, CancellationToken cancellationToken) =>
|
||||
{
|
||||
var user = authService.CurrentUser;
|
||||
if (user?.HardwareId != request.HardwareId)
|
||||
throw new BusinessException(ExceptionEnum.HardwareIdMismatch, "Hardware mismatch! You are not authorized to access this resource from this hardware.");
|
||||
if (user == null)
|
||||
throw new UnauthorizedAccessException();
|
||||
|
||||
if (string.IsNullOrEmpty(user.HardwareId))
|
||||
{
|
||||
await userService.UpdateHardwareId(user.Email, request.HardwareId);
|
||||
user.HardwareId = request.HardwareId;
|
||||
}
|
||||
|
||||
if (user.HardwareId != request.HardwareId)
|
||||
throw new BusinessException(ExceptionEnum.HardwareIdMismatch, "Hardware mismatch! You are not authorized to access this resource from this hardware.");
|
||||
|
||||
var ms = new MemoryStream();
|
||||
var key = Security.MakeEncryptionKey(user.Email, request.Password);
|
||||
await resourcesService.GetEncryptedResource(request.ResourceEnum, key, ms, cancellationToken);
|
||||
return ms;
|
||||
var filename = await resourcesService.GetEncryptedResource(request.ResourceEnum, key, ms, cancellationToken);
|
||||
|
||||
return Results.File(ms, "application/octet-stream", filename);
|
||||
}).RequireAuthorization();
|
||||
|
||||
app.Run();
|
||||
app.Run();
|
||||
|
||||
Reference in New Issue
Block a user