76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
using System.Text;
|
||
using FluentValidation;
|
||
using Helpdesk.Application.Users;
|
||
|
||
namespace Helpdesk.Application.Auth;
|
||
|
||
public sealed record RegisterRequest(string Email, string FirstName, string LastName);
|
||
|
||
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);
|
||
|
||
public sealed record ConfirmEmailRequest(string Token, string NewPassword);
|
||
|
||
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
|
||
{
|
||
private const int MinLength = 8;
|
||
private const int MaxLength = 64;
|
||
private const int MaxUtf8Bytes = 72;
|
||
|
||
public static IRuleBuilderOptions<T, string?> ValidPassword<T>(this IRuleBuilderInitial<T, string?> rule) =>
|
||
rule.Cascade(CascadeMode.Stop)
|
||
.NotEmpty().MinimumLength(MinLength).MaximumLength(MaxLength)
|
||
.Must(password => Encoding.UTF8.GetByteCount(password!) <= MaxUtf8Bytes)
|
||
.WithMessage("Heslo může mít nejvýše 72 bajtů v UTF-8.");
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
|
||
public sealed class ConfirmEmailRequestValidator : AbstractValidator<ConfirmEmailRequest>
|
||
{
|
||
public ConfirmEmailRequestValidator()
|
||
{
|
||
RuleFor(x => x.Token).NotEmpty();
|
||
RuleFor(x => x.NewPassword).ValidPassword();
|
||
}
|
||
}
|
||
|
||
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();
|
||
}
|
||
}
|