Implements Authorization system

This commit is contained in:
BinaryAlley committed 2024-11-22 18:02:42 +02:00
1 parent 5bfb2ca212
commit 2c9c210e43
68 files changed
+2734 -336

No files matched your search

@@ -0,0 +1,37 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Authentication;
using Microsoft.AspNetCore.Http;
using System;
using System.Security.Claims;
#endregion
namespace Lumina.Presentation.Api.Common.Authentication;
/// <summary>
/// Service for retrieving the current user's information from the HTTP context.
/// </summary>
public class HttpContextCurrentUserService : ICurrentUserService
{
private readonly IHttpContextAccessor _httpContextAccessor;
/// <summary>
/// Initializes a new instance of the <see cref="HttpContextCurrentUserService"/> class.
/// </summary>
/// <param name="httpContextAccessor">The <see cref="IHttpContextAccessor"/> used to access the current HTTP context.</param>
public HttpContextCurrentUserService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
/// <summary>
/// Gets the unique identifier of the currently authenticated user. Returns <see langword="null"/> if no user is authenticated or if the claim is not present.
/// </summary>
public Guid? UserId
{
get
{
string? userIdClaim = _httpContextAccessor.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
return Guid.TryParse(userIdClaim, out Guid userId) ? userId : null;
}
}
}
@@ -1,6 +1,8 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using FastEndpoints.Swagger;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Presentation.Api.Common.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
@@ -206,6 +208,8 @@ public static class PresentationApiLayerServices
.AllowAnyHeader());
});
services.AddScoped<ICurrentUserService, HttpContextCurrentUserService>();
return services;
}
}