Reorganized file structure and namespaces
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
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;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.Authentication.AuthService;
|
||||
|
||||
public class EfAuthService : IAuthService
|
||||
{
|
||||
private readonly AipsDbContext _dbContext;
|
||||
private readonly UserManager<User.User> _userManager;
|
||||
|
||||
public EfAuthService(AipsDbContext dbContext, UserManager<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);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace AipsCore.Infrastructure.Persistence.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; }
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
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.Persistence.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));
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
using System.Security.Claims;
|
||||
using AipsCore.Application.Abstract.UserContext;
|
||||
using AipsCore.Domain.Models.User.ValueObjects;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.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);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using AipsCore.Infrastructure.Persistence.Authentication.RefreshToken;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -7,7 +6,7 @@ namespace AipsCore.Infrastructure.Persistence.Db;
|
||||
|
||||
public class AipsDbContext : IdentityDbContext<User.User, IdentityRole<Guid>, Guid>
|
||||
{
|
||||
public DbSet<RefreshToken> RefreshTokens { get; set; }
|
||||
public DbSet<RefreshToken.RefreshToken> RefreshTokens { get; set; }
|
||||
|
||||
public DbSet<Whiteboard.Whiteboard> Whiteboards { get; set; }
|
||||
public DbSet<Shape.Shape> Shapes { get; set; }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.Authentication.RefreshToken;
|
||||
namespace AipsCore.Infrastructure.Persistence.RefreshToken;
|
||||
|
||||
public class RefreshToken
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace AipsCore.Infrastructure.Persistence.Authentication.RefreshToken;
|
||||
namespace AipsCore.Infrastructure.Persistence.RefreshToken;
|
||||
|
||||
public class RefreshTokenException : Exception
|
||||
{
|
||||
@@ -1,8 +1,8 @@
|
||||
namespace AipsCore.Infrastructure.Persistence.Authentication.RefreshToken;
|
||||
namespace AipsCore.Infrastructure.Persistence.RefreshToken;
|
||||
|
||||
public static class RefreshTokenMappers
|
||||
{
|
||||
public static Application.Common.Authentication.Models.RefreshToken MapToModel(this RefreshToken entity)
|
||||
public static Application.Common.Authentication.Models.RefreshToken MapToModel(this Persistence.RefreshToken.RefreshToken entity)
|
||||
{
|
||||
return new Application.Common.Authentication.Models.RefreshToken(
|
||||
entity.Token,
|
||||
@@ -1,10 +1,10 @@
|
||||
using AipsCore.Application.Abstract.UserContext;
|
||||
using AipsCore.Domain.Models.User.ValueObjects;
|
||||
using AipsCore.Infrastructure.Persistence.Authentication.Jwt;
|
||||
using AipsCore.Infrastructure.Authentication.Jwt;
|
||||
using AipsCore.Infrastructure.Persistence.Db;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.Authentication.RefreshToken;
|
||||
namespace AipsCore.Infrastructure.Persistence.RefreshToken;
|
||||
|
||||
public class RefreshTokenRepository : IRefreshTokenRepository
|
||||
{
|
||||
@@ -20,7 +20,7 @@ public class RefreshTokenRepository : IRefreshTokenRepository
|
||||
|
||||
public async Task AddAsync(string token, UserId userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var refreshToken = new RefreshToken()
|
||||
var refreshToken = new Persistence.RefreshToken.RefreshToken()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Token = token,
|
||||
Reference in New Issue
Block a user