This commit is contained in:
john 2025-05-18 22:22:20 +02:00
parent fbc6f562e3
commit d7e0c59559
31 changed files with 249 additions and 57 deletions

View file

@ -3,10 +3,11 @@ using System.Text.Encodings.Web;
using Femto.Api.Sessions;
using Femto.Common;
using Femto.Modules.Auth.Application;
using Femto.Modules.Auth.Application.Commands.ValidateSession;
using Femto.Modules.Auth.Application.Interface.ValidateSession;
using Femto.Modules.Auth.Errors;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Extensions;
namespace Femto.Api.Auth;
@ -27,20 +28,25 @@ internal class SessionAuthenticationHandler(
try
{
var result = await authModule.PostCommand(new ValidateSessionCommand(sessionId));
var result = await authModule.Command(new ValidateSessionCommand(sessionId));
var claims = new List<Claim>
{
new(ClaimTypes.Name, result.Username),
new("sub", result.UserId.ToString()),
new("user_id", result.UserId.ToString()),
new(ClaimTypes.Name, result.User.Username),
new("sub", result.User.Id.ToString()),
new("user_id", result.User.Id.ToString()),
};
claims.AddRange(
result.User.Roles
.Select(role => new Claim(ClaimTypes.Role, role.ToString()))
);
var identity = new ClaimsIdentity(claims, this.Scheme.Name);
var principal = new ClaimsPrincipal(identity);
this.Context.SetSession(result.Session, cookieOptions.Value);
currentUserContext.CurrentUser = new CurrentUser(result.UserId, result.Username);
currentUserContext.CurrentUser = new CurrentUser(result.User.Id, result.User.Username);
return AuthenticateResult.Success(
new AuthenticationTicket(principal, this.Scheme.Name)