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,19 @@
using System.ComponentModel.DataAnnotations;
namespace AipsCore.Infrastructure.Persistence.RefreshToken;
public class RefreshToken
{
[Key]
public Guid Id { get; set; }
[Required]
[MaxLength(255)]
public required string Token { get; set; }
public Guid UserId { get; set; }
public User.User User { get; set; } = null!;
public DateTime ExpiresAt { get; set; }
}

View File

@@ -0,0 +1,16 @@
namespace AipsCore.Infrastructure.Persistence.RefreshToken;
public class RefreshTokenException : Exception
{
private const string InvalidTokenMessage = "Invalud token";
private const string TokenExpiredMessage = "Token expired";
public RefreshTokenException(string message)
: base(message)
{
}
public static RefreshTokenException InvalidToken() => new(InvalidTokenMessage);
public static RefreshTokenException TokenExpired() => new(TokenExpiredMessage);
}

View File

@@ -0,0 +1,12 @@
namespace AipsCore.Infrastructure.Persistence.RefreshToken;
public static class RefreshTokenMappers
{
public static Application.Common.Authentication.Models.RefreshToken MapToModel(this Persistence.RefreshToken.RefreshToken entity)
{
return new Application.Common.Authentication.Models.RefreshToken(
entity.Token,
entity.UserId.ToString(),
entity.ExpiresAt);
}
}

View File

@@ -0,0 +1,64 @@
using AipsCore.Application.Abstract.UserContext;
using AipsCore.Domain.Models.User.ValueObjects;
using AipsCore.Infrastructure.Authentication.Jwt;
using AipsCore.Infrastructure.Persistence.Db;
using Microsoft.EntityFrameworkCore;
namespace AipsCore.Infrastructure.Persistence.RefreshToken;
public class RefreshTokenRepository : IRefreshTokenRepository
{
private readonly AipsDbContext _dbContext;
private readonly JwtSettings _jwtSettings;
public RefreshTokenRepository(AipsDbContext dbContext, JwtSettings jwtSettings)
{
_dbContext = dbContext;
_jwtSettings = jwtSettings;
}
public async Task AddAsync(string token, UserId userId, CancellationToken cancellationToken = default)
{
var refreshToken = new Persistence.RefreshToken.RefreshToken()
{
Id = Guid.NewGuid(),
Token = token,
UserId = new Guid(userId.IdValue),
ExpiresAt = DateTime.UtcNow.AddDays(_jwtSettings.RefreshTokenExpirationDays)
};
await _dbContext.AddAsync(refreshToken, cancellationToken);
}
public async Task<Application.Common.Authentication.Models.RefreshToken> GetByValueAsync(string token, CancellationToken cancellationToken = default)
{
var entity = await _dbContext.RefreshTokens.FirstOrDefaultAsync(x => x.Token == token, cancellationToken);
if (entity is null)
{
throw RefreshTokenException.InvalidToken();
}
if (entity.ExpiresAt < DateTime.UtcNow)
{
throw RefreshTokenException.TokenExpired();
}
return entity.MapToModel();
}
public async Task RevokeAsync(string token, CancellationToken cancellationToken = default)
{
await _dbContext.RefreshTokens
.Where(x => x.Token == token)
.ExecuteDeleteAsync(cancellationToken);
}
public async Task RevokeAllAsync(UserId userId, CancellationToken cancellationToken = default)
{
await _dbContext.RefreshTokens
.Where(x => x.UserId == new Guid(userId.IdValue))
.ExecuteDeleteAsync(cancellationToken);
}
}