2026-07-22 19:43:13 +00:00
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.DTOs;
|
|
|
|
|
using Heimdall.Application.Services;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Auth;
|
|
|
|
|
|
|
|
|
|
internal static class AuthHelper
|
|
|
|
|
{
|
|
|
|
|
internal static async Task<ServiceResult<AuthResponse>> CreateAuthResponseAsync(
|
|
|
|
|
AppUser user,
|
|
|
|
|
IJwtService jwtService,
|
|
|
|
|
IRolePermissionService rolePermissionService,
|
2026-10-20 07:21:28 +02:00
|
|
|
UserManager<AppUser> userManager,
|
2026-11-04 10:26:07 +01:00
|
|
|
DateTime? sessionStarted = null,
|
|
|
|
|
CancellationToken ct = default)
|
2026-07-22 19:43:13 +00:00
|
|
|
{
|
2026-07-27 18:59:12 +00:00
|
|
|
var roles = await userManager.GetRolesAsync(user).ConfigureAwait(false);
|
2026-11-04 10:26:07 +01:00
|
|
|
var permissions = await rolePermissionService.GetPermissionsForRolesAsync(roles, ct).ConfigureAwait(false);
|
2026-07-22 19:43:13 +00:00
|
|
|
|
2026-10-20 07:21:28 +02:00
|
|
|
var (accessToken, expiresAt) = sessionStarted is null
|
|
|
|
|
? jwtService.GenerateAccessToken(user, roles, permissions)
|
|
|
|
|
: jwtService.GenerateAccessToken(user, roles, permissions, sessionStarted.Value);
|
|
|
|
|
var (refreshToken, _) = sessionStarted is null
|
|
|
|
|
? jwtService.GenerateRefreshToken(user)
|
|
|
|
|
: jwtService.GenerateRefreshToken(user, sessionStarted.Value);
|
2026-07-22 19:43:13 +00:00
|
|
|
|
|
|
|
|
return ServiceResult<AuthResponse>.Success(new AuthResponse(
|
|
|
|
|
accessToken,
|
|
|
|
|
refreshToken,
|
|
|
|
|
expiresAt,
|
|
|
|
|
false,
|
2026-11-04 10:26:07 +01:00
|
|
|
new UserInfo(user.Id, user.Email ?? "", user.DisplayName, [.. roles], [.. permissions])));
|
2026-07-22 19:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-04 10:26:07 +01:00
|
|
|
internal static async Task<UserInfo> MapUserInfoAsync(AppUser user, UserManager<AppUser> userManager,
|
|
|
|
|
IRolePermissionService rolePermissionService, CancellationToken ct)
|
2026-07-22 19:43:13 +00:00
|
|
|
{
|
2026-07-27 18:59:12 +00:00
|
|
|
var roles = await userManager.GetRolesAsync(user).ConfigureAwait(false);
|
2026-11-04 10:26:07 +01:00
|
|
|
var permissions = await rolePermissionService.GetPermissionsForRolesAsync(roles, ct).ConfigureAwait(false);
|
|
|
|
|
return new UserInfo(user.Id, user.Email ?? "", user.DisplayName, [.. roles], [.. permissions]);
|
2026-07-22 19:43:13 +00:00
|
|
|
}
|
|
|
|
|
}
|