get resource works

This commit is contained in:
Alex Bezdieniezhnykh
2024-11-13 00:24:09 +02:00
parent ddbf8114ba
commit 4445fcd673
6 changed files with 58 additions and 11 deletions
+18 -6
View File
@@ -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();