2026-12-04 20:14:00 +01:00
|
|
|
|
using System.Text;
|
2026-10-28 21:43:17 +01:00
|
|
|
|
using FluentValidation;
|
|
|
|
|
|
using Helpdesk.Application.Users;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.Application.Auth;
|
|
|
|
|
|
|
2026-12-04 20:14:00 +01:00
|
|
|
|
public sealed record RegisterRequest(string Email, string FirstName, string LastName);
|
2026-10-28 21:43:17 +01:00
|
|
|
|
|
|
|
|
|
|
public sealed record LoginRequest(string Email, string Password);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Refresh token – pro <c>/auth/refresh</c> i <c>/auth/logout</c>.</summary>
|
|
|
|
|
|
public sealed record RefreshTokenRequest(string RefreshToken);
|
|
|
|
|
|
|
2026-12-04 20:14:00 +01:00
|
|
|
|
public sealed record ConfirmEmailRequest(string Token, string NewPassword);
|
2026-10-28 21:43:17 +01:00
|
|
|
|
|
|
|
|
|
|
public sealed record ForgotPasswordRequest(string Email);
|
|
|
|
|
|
|
|
|
|
|
|
public sealed record ResetPasswordRequest(string Token, string NewPassword);
|
|
|
|
|
|
|
|
|
|
|
|
public sealed record AuthResponse(
|
|
|
|
|
|
string AccessToken,
|
|
|
|
|
|
DateTime AccessTokenExpiresAt,
|
|
|
|
|
|
string RefreshToken,
|
|
|
|
|
|
DateTime RefreshTokenExpiresAt,
|
|
|
|
|
|
UserDto User);
|
|
|
|
|
|
|
|
|
|
|
|
internal static class PasswordRules
|
|
|
|
|
|
{
|
|
|
|
|
|
public const int MinLength = 8;
|
|
|
|
|
|
public const int MaxLength = 64; // BCrypt zpracuje nejvýše 72 bajtů
|
|
|
|
|
|
|
|
|
|
|
|
public static IRuleBuilderOptions<T, string> ValidPassword<T>(this IRuleBuilder<T, string> rule) =>
|
|
|
|
|
|
rule.NotEmpty().MinimumLength(MinLength).MaximumLength(MaxLength);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public sealed class RegisterRequestValidator : AbstractValidator<RegisterRequest>
|
|
|
|
|
|
{
|
|
|
|
|
|
public RegisterRequestValidator()
|
|
|
|
|
|
{
|
|
|
|
|
|
RuleFor(x => x.Email).NotEmpty().EmailAddress().MaximumLength(254);
|
|
|
|
|
|
RuleFor(x => x.FirstName).NotEmpty().MaximumLength(100);
|
|
|
|
|
|
RuleFor(x => x.LastName).NotEmpty().MaximumLength(100);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-04 20:14:00 +01:00
|
|
|
|
public sealed class ConfirmEmailRequestValidator : AbstractValidator<ConfirmEmailRequest>
|
|
|
|
|
|
{
|
|
|
|
|
|
public ConfirmEmailRequestValidator()
|
|
|
|
|
|
{
|
|
|
|
|
|
RuleFor(x => x.Token).NotEmpty();
|
|
|
|
|
|
RuleFor(x => x.NewPassword).Cascade(CascadeMode.Stop).ValidPassword()
|
|
|
|
|
|
.Must(password => Encoding.UTF8.GetByteCount(password) <= 72)
|
|
|
|
|
|
.WithMessage("Heslo může mít nejvýše 72 bajtů v UTF-8.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 21:43:17 +01:00
|
|
|
|
public sealed class LoginRequestValidator : AbstractValidator<LoginRequest>
|
|
|
|
|
|
{
|
|
|
|
|
|
public LoginRequestValidator()
|
|
|
|
|
|
{
|
|
|
|
|
|
RuleFor(x => x.Email).NotEmpty();
|
|
|
|
|
|
RuleFor(x => x.Password).NotEmpty();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public sealed class ResetPasswordRequestValidator : AbstractValidator<ResetPasswordRequest>
|
|
|
|
|
|
{
|
|
|
|
|
|
public ResetPasswordRequestValidator()
|
|
|
|
|
|
{
|
|
|
|
|
|
RuleFor(x => x.Token).NotEmpty();
|
|
|
|
|
|
RuleFor(x => x.NewPassword).ValidPassword();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|