Reorganized file structure and namespaces

This commit is contained in:
Veljko Tosic
2026-02-14 19:16:06 +01:00
parent 12bded085b
commit b2812feaab
11 changed files with 19 additions and 22 deletions

View File

@@ -0,0 +1,91 @@
using AipsCore.Application.Common.Authentication;
using AipsCore.Domain.Common.Validation;
using AipsCore.Domain.Models.User.External;
using AipsCore.Domain.Models.User.Validation;
using AipsCore.Infrastructure.Persistence.Db;
using AipsCore.Infrastructure.Persistence.User.Mappers;
using Microsoft.AspNetCore.Identity;
namespace AipsCore.Infrastructure.Authentication.AuthService;
public class EfAuthService : IAuthService
{
private readonly AipsDbContext _dbContext;
private readonly UserManager<Persistence.User.User> _userManager;
public EfAuthService(AipsDbContext dbContext, UserManager<Persistence.User.User> userManager)
{
_dbContext = dbContext;
_userManager = userManager;
}
public async Task SignUpWithPasswordAsync(Domain.Models.User.User user, string password, CancellationToken cancellationToken = default)
{
var entity = user.MapToEntity();
var result = await _userManager.CreateAsync(entity, password);
if (!result.Succeeded)
{
var errors = string.Join(", ", result.Errors.Select(e => e.Description));
throw new Exception($"User registration failed: {errors}");
}
await _userManager.AddToRoleAsync(entity, UserRole.User.Name);
}
public async Task<LoginResult> LoginWithEmailAndPasswordAsync(string email, string password, CancellationToken cancellationToken = default)
{
var entity = await _userManager.FindByEmailAsync(email);
if (entity is null)
{
throw new ValidationException(UserErrors.InvalidCredentials());
}
var isPasswordValid = await _userManager.CheckPasswordAsync(entity, password);
if (!isPasswordValid)
{
throw new ValidationException(UserErrors.InvalidCredentials());
}
var roles = new List<UserRole>();
var rolesNames = await _userManager.GetRolesAsync(entity);
foreach (var roleName in rolesNames)
{
var role = UserRole.FromString(roleName);
if (role is null) throw new Exception($"Role {roleName} not found");
roles.Add(role);
}
return new LoginResult(entity.MapToModel(), roles);
}
public async Task<LoginResult> LoginWithRefreshTokenAsync(Application.Common.Authentication.Models.RefreshToken refreshToken, CancellationToken cancellationToken = default)
{
var entity = await _userManager.FindByIdAsync(refreshToken.UserId);
if (entity is null)
{
throw new ValidationException(UserErrors.InvalidCredentials());
}
var roles = new List<UserRole>();
var rolesNames = await _userManager.GetRolesAsync(entity);
foreach (var roleName in rolesNames)
{
var role = UserRole.FromString(roleName);
if (role is null) throw new Exception($"Role {roleName} not found");
roles.Add(role);
}
return new LoginResult(entity.MapToModel(), roles);
}
}

View File

@@ -0,0 +1,10 @@
namespace AipsCore.Infrastructure.Authentication.Jwt;
public sealed class JwtSettings
{
public string Issuer { get; init; } = null!;
public string Audience { get; init; } = null!;
public string Key { get; init; } = null!;
public int ExpirationMinutes { get; init; }
public int RefreshTokenExpirationDays { get; init; }
}

View File

@@ -0,0 +1,51 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using AipsCore.Application.Abstract.UserContext;
using AipsCore.Domain.Models.User.External;
using Microsoft.IdentityModel.Tokens;
namespace AipsCore.Infrastructure.Authentication.Jwt;
public class JwtTokenProvider : ITokenProvider
{
private readonly JwtSettings _jwtSettings;
public JwtTokenProvider(JwtSettings jwtSettings)
{
_jwtSettings = jwtSettings;
}
public string GenerateAccessToken(Domain.Models.User.User user, IList<UserRole> roles)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, user.Id.IdValue),
new Claim(ClaimTypes.Email, user.Email.EmailValue)
};
foreach (var role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role.Name));
}
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.Key));
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: _jwtSettings.Issuer,
audience: _jwtSettings.Audience,
claims: claims,
expires: DateTime.UtcNow.AddMinutes(_jwtSettings.ExpirationMinutes),
signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(token);
}
public string GenerateRefreshToken()
{
return Convert.ToBase64String(RandomNumberGenerator.GetBytes(64));
}
}

View File

@@ -0,0 +1,35 @@
using System.Security.Claims;
using AipsCore.Application.Abstract.UserContext;
using AipsCore.Domain.Models.User.ValueObjects;
using Microsoft.AspNetCore.Http;
namespace AipsCore.Infrastructure.Authentication.UserContext;
public class HttpUserContext : IUserContext
{
private readonly IHttpContextAccessor _httpContextAccessor;
public HttpUserContext(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public UserId GetCurrentUserId()
{
var user = _httpContextAccessor.HttpContext?.User;
if (user is null || !user.Identity!.IsAuthenticated)
{
throw new UnauthorizedAccessException("User is not authenticated");
}
var userIdClaim = user.FindFirst(ClaimTypes.NameIdentifier);
if (userIdClaim is null)
{
throw new UnauthorizedAccessException("User id claim not found");
}
return new UserId(userIdClaim.Value);
}
}