backend: Rate limit login attempts per email and IP
This commit is contained in:
1 parent
5470818841
commit
52fb8af684
18 files changed
+106
-116
No files matched your search
@@ -17,7 +17,6 @@ public sealed class AuthService(
|
||||
TokenRevocationService revocation,
|
||||
IEmailSender emailSender,
|
||||
IOptions<RegistrationOptions> registrationOptions,
|
||||
TimeProvider clock,
|
||||
IValidator<RegisterRequest> registerValidator,
|
||||
IValidator<ConfirmEmailRequest> confirmEmailValidator,
|
||||
IValidator<LoginRequest> loginValidator,
|
||||
@@ -71,36 +70,21 @@ public sealed class AuthService(
|
||||
await loginValidator.ValidateAndThrowAsync(request, ct);
|
||||
|
||||
var normalized = User.NormalizeEmail(request.Email);
|
||||
var account = await db.Users.Where(u => u.EmailNormalized == normalized)
|
||||
.Select(u => new { u.Id, u.EmailNormalized }).SingleOrDefaultAsync(ct);
|
||||
var account = await db.Users.Where(u => u.EmailNormalized == normalized && u.IsActive && !u.IsDeleted && u.EmailConfirmed)
|
||||
.Select(u => new { u.Id, u.EmailNormalized, u.PasswordHash }).SingleOrDefaultAsync(ct);
|
||||
|
||||
// Neexistující účet a špatné heslo vracejí stejnou chybu – nelze tak zjišťovat registrované e-maily.
|
||||
const string invalidCredentials = "Neplatný e-mail nebo heslo.";
|
||||
if (account is null)
|
||||
var passwordHash = account?.EmailNormalized == normalized ? account.PasswordHash : null;
|
||||
if (!passwordHasher.Verify(request.Password, passwordHash) || account is null)
|
||||
throw new UnauthorizedException(invalidCredentials);
|
||||
|
||||
// Po ověření hesla ověříme pod zámkem, že se účet mezitím nezměnil.
|
||||
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 || user.PasswordHash.Length == 0)
|
||||
if (user is not { IsActive: true, IsDeleted: false, EmailConfirmed: true } ||
|
||||
user.EmailNormalized != normalized || user.PasswordHash != passwordHash)
|
||||
throw new UnauthorizedException(invalidCredentials);
|
||||
var now = clock.GetUtcNow().UtcDateTime;
|
||||
if (user.IsLockedOut(now))
|
||||
throw new UnauthorizedException("Účet je po opakovaných neúspěšných pokusech dočasně zamčen. Zkuste to později.");
|
||||
|
||||
if (!passwordHasher.Verify(request.Password, user.PasswordHash))
|
||||
{
|
||||
user.RegisterFailedLogin(now);
|
||||
await db.SaveChangesAsync(ct);
|
||||
await transaction.CommitAsync(ct);
|
||||
throw new UnauthorizedException(invalidCredentials);
|
||||
}
|
||||
|
||||
if (!user.IsActive)
|
||||
throw new ForbiddenException("Účet je deaktivován.");
|
||||
if (!user.EmailConfirmed)
|
||||
throw new ForbiddenException("E-mail ještě nebyl ověřen. Použijte odkaz z ověřovacího e-mailu.");
|
||||
|
||||
user.RegisterSuccessfulLogin();
|
||||
var response = await IssueTokenPairAsync(user, ct);
|
||||
await transaction.CommitAsync(ct);
|
||||
return response;
|
||||
|
||||
Reference in new issue
Block a user