backend + web: Set passwords through email confirmation
This commit is contained in:
1 parent
bd8eb65e56
commit
ee5f1d3d10
23 files changed
+281
-110
No files matched your search
@@ -1,17 +1,17 @@
|
||||
using System.Text;
|
||||
using FluentValidation;
|
||||
using Helpdesk.Application.Users;
|
||||
|
||||
namespace Helpdesk.Application.Auth;
|
||||
|
||||
public sealed record RegisterRequest(string Email, string Password, string FirstName, string LastName);
|
||||
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);
|
||||
|
||||
/// <summary>Token z odkazu v e-mailu (ověření e-mailu).</summary>
|
||||
public sealed record EmailTokenRequest(string Token);
|
||||
public sealed record ConfirmEmailRequest(string Token, string NewPassword);
|
||||
|
||||
public sealed record ForgotPasswordRequest(string Email);
|
||||
|
||||
@@ -38,12 +38,22 @@ public sealed class RegisterRequestValidator : AbstractValidator<RegisterRequest
|
||||
public RegisterRequestValidator()
|
||||
{
|
||||
RuleFor(x => x.Email).NotEmpty().EmailAddress().MaximumLength(254);
|
||||
RuleFor(x => x.Password).ValidPassword();
|
||||
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).Cascade(CascadeMode.Stop).ValidPassword()
|
||||
.Must(password => Encoding.UTF8.GetByteCount(password) <= 72)
|
||||
.WithMessage("Heslo může mít nejvýše 72 bajtů v UTF-8.");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LoginRequestValidator : AbstractValidator<LoginRequest>
|
||||
{
|
||||
public LoginRequestValidator()
|
||||
|
||||
@@ -19,6 +19,7 @@ public sealed class AuthService(
|
||||
IOptions<RegistrationOptions> registrationOptions,
|
||||
TimeProvider clock,
|
||||
IValidator<RegisterRequest> registerValidator,
|
||||
IValidator<ConfirmEmailRequest> confirmEmailValidator,
|
||||
IValidator<LoginRequest> loginValidator,
|
||||
IValidator<ResetPasswordRequest> resetPasswordValidator)
|
||||
{
|
||||
@@ -35,11 +36,11 @@ public sealed class AuthService(
|
||||
var user = await db.Users.SingleOrDefaultAsync(u => u.EmailNormalized == normalized, ct);
|
||||
if (user is null)
|
||||
{
|
||||
user = User.Create(request.Email, passwordHasher.Hash(request.Password), request.FirstName, request.LastName, role);
|
||||
user = User.Create(request.Email, "", request.FirstName, request.LastName, role);
|
||||
db.Users.Add(user);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
else if (!user.IsActive || user.EmailConfirmed || !passwordHasher.Verify(request.Password, user.PasswordHash))
|
||||
else if (!user.IsActive || user.EmailConfirmed)
|
||||
{
|
||||
throw new ConflictException("Účet s tímto e-mailem už existuje.");
|
||||
}
|
||||
@@ -51,13 +52,18 @@ public sealed class AuthService(
|
||||
internal Task<bool> SendEmailConfirmationAsync(User user, CancellationToken ct) =>
|
||||
SendEmailTokenAsync(user, TokenType.EmailConfirm, ct);
|
||||
|
||||
public async Task ConfirmEmailAsync(EmailTokenRequest request, CancellationToken ct)
|
||||
public async Task ConfirmEmailAsync(ConfirmEmailRequest request, CancellationToken ct)
|
||||
{
|
||||
await confirmEmailValidator.ValidateAndThrowAsync(request, ct);
|
||||
|
||||
await using var transaction = await db.BeginWriteAsync(ct);
|
||||
var user = await ConsumeEmailTokenAsync(request.Token, TokenType.EmailConfirm, ct);
|
||||
var revoked = await revocation.StageRevokeAllAsync([user.Id], RevokeReason.PasswordChanged, ct);
|
||||
user.ChangePasswordHash(passwordHasher.Hash(request.NewPassword));
|
||||
user.ConfirmEmail();
|
||||
await db.SaveChangesAsync(ct);
|
||||
await transaction.CommitAsync(ct);
|
||||
revocation.Publish(revoked);
|
||||
}
|
||||
|
||||
public async Task<AuthResponse> LoginAsync(LoginRequest request, CancellationToken ct)
|
||||
@@ -75,7 +81,7 @@ public sealed class AuthService(
|
||||
|
||||
await using var transaction = await db.BeginWriteAsync(ct);
|
||||
var user = await db.LockUserAsync(account.Id, ct);
|
||||
if (user is null || user.IsDeleted || user.EmailNormalized != account.EmailNormalized)
|
||||
if (user is null || user.IsDeleted || user.EmailNormalized != account.EmailNormalized || user.PasswordHash.Length == 0)
|
||||
throw new UnauthorizedException(invalidCredentials);
|
||||
var now = clock.GetUtcNow().UtcDateTime;
|
||||
if (user.IsLockedOut(now))
|
||||
@@ -207,7 +213,7 @@ public sealed class AuthService(
|
||||
var baseUrl = _registration.WebBaseUrl.TrimEnd('/');
|
||||
var (subject, path, text) = type switch
|
||||
{
|
||||
TokenType.EmailConfirm => ("Ověření e-mailu", "confirm-email", "Ověřte svůj e-mail"),
|
||||
TokenType.EmailConfirm => ("Ověření e-mailu", "confirm-email", "Ověřte svůj e-mail a nastavte heslo k účtu"),
|
||||
TokenType.PasswordReset => ("Obnova hesla", "reset-password", "Nové heslo nastavíte"),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(type)),
|
||||
};
|
||||
@@ -222,7 +228,8 @@ public sealed class AuthService(
|
||||
|
||||
var validated = await tokens.ValidateAsync(token ?? "", type) ?? throw new BadRequestException(invalid);
|
||||
var user = await db.LockUserAsync(validated.UserId, ct);
|
||||
if (user is null || !user.IsActive || !await revocation.TryConsumeAsync(validated.Jti, type, ct))
|
||||
if (user is null || !user.IsActive || (type == TokenType.EmailConfirm && user.EmailConfirmed) ||
|
||||
!await revocation.TryConsumeAsync(validated.Jti, type, ct))
|
||||
throw new BadRequestException(invalid);
|
||||
|
||||
return user;
|
||||
|
||||
Reference in new issue
Block a user