Skip to content

Commit

Permalink
resend timer
Browse files Browse the repository at this point in the history
  • Loading branch information
GNUGradyn committed Nov 1, 2023
1 parent 633549b commit c72fec9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
18 changes: 17 additions & 1 deletion JournalyApiV2/Services/BLL/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using JournalyApiV2.Data.Models;
using JournalyApiV2.Models;
using JournalyApiV2.Models.Responses;
using JournalyApiV2.Pipeline;
using JournalyApiV2.Services.DAL;
using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens;
Expand Down Expand Up @@ -237,7 +238,22 @@ public async Task ResetPasswordAsync(string email)
{
var user = await _userManager.FindByEmailAsync(email);
if (user == null) throw new ArgumentException("User not found");
var code = await _authDbService.GetOrGeneratePasswordResetCode(Guid.Parse(user.Id));
var code = await _authDbService.GetPasswordResetCode(Guid.Parse(user.Id));
if (code == null)
{
code = await _authDbService.GeneratePasswordResetCode(Guid.Parse(user.Id));
}
else
{
try
{
await _authDbService.ResetPasswordResetTimerAsync(Guid.Parse(user.Id));
}
catch (TooEarlyException ex)
{
throw new HttpBadRequestException(ex.Message);
}
}
await _emailService.SendPasswordResetEmailAsync(user.Email, user.FirstName, user.LastName, code);
}
}
49 changes: 35 additions & 14 deletions JournalyApiV2/Services/DAL/AuthDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ public async Task ResetEmailVerificationTimerAsync(Guid userId)
var code = await db.EmailVerificationCodes.SingleOrDefaultAsync(x => x.User == userId);
if (code == null) throw new ArgumentException("No user verification found for given ID");
if (code.LastSent.AddSeconds(60) >= DateTime.UtcNow)
throw new TooEarlyException($"Too early to resend: Please wait an additional {Convert.ToInt16((code.LastSent.AddSeconds(60) - DateTime.UtcNow).TotalSeconds)} seconds");
throw new TooEarlyException(
$"Too early to resend: Please wait an additional {Convert.ToInt16((code.LastSent.AddSeconds(60) - DateTime.UtcNow).TotalSeconds)} seconds");
code.LastSent = DateTime.UtcNow;
await db.SaveChangesAsync();
}

public async Task<Guid?> GetUserByLongCode(string longCode)
public async Task<Guid?> GetUserByLongCode(string longCode)
{
await using var db = _db.Journaly();
return (await db.EmailVerificationCodes.SingleOrDefaultAsync(x => x.LongCode == longCode))?.User;
Expand All @@ -158,27 +159,47 @@ public async Task VerifyUser(Guid user)
public async Task<bool> CheckShortCode(Guid userId, string shortCode)
{
await using var db = _db.Journaly();
var code = await db.EmailVerificationCodes.SingleOrDefaultAsync(x => x.User == userId && x.ShortCode == shortCode);
var code = await db.EmailVerificationCodes.SingleOrDefaultAsync(x =>
x.User == userId && x.ShortCode == shortCode);

return code != null;
}

public async Task<string> GetOrGeneratePasswordResetCode(Guid userId)
public async Task<string?> GetPasswordResetCode(Guid userId)
{
await using var db = _db.Journaly();
var code = await db.PasswordResetCodes.SingleOrDefaultAsync(x => x.User == userId);
if (code == null)
return code?.Code;
}

public async Task<string> GeneratePasswordResetCode(Guid userId)
{
await using var db = _db.Journaly();
var code = await db.PasswordResetCodes.SingleOrDefaultAsync(x => x.User == userId);
if (code != null) return code.Code;

code = new PasswordResetCode
{
code = new PasswordResetCode
{
User = userId,
Code = GenerateSecureOpaqueToken(),
LastSent = DateTime.UtcNow
};
await db.PasswordResetCodes.AddAsync(code);
await db.SaveChangesAsync();
}
Code = GenerateSecureOpaqueToken(),
LastSent = DateTime.UtcNow,
User = userId
};

await db.PasswordResetCodes.AddAsync(code);
await db.SaveChangesAsync();

return code.Code;
}

public async Task ResetPasswordResetTimerAsync(Guid userId)
{
await using var db = _db.Journaly();
var code = await db.PasswordResetCodes.SingleOrDefaultAsync(x => x.User == userId);
if (code == null) throw new ArgumentException("User not found");
if (code.LastSent.AddSeconds(60) >= DateTime.UtcNow)
throw new TooEarlyException(
$"Too early to send another reset, please try again in {Convert.ToInt16((code.LastSent.AddSeconds(60) - DateTime.UtcNow).TotalSeconds)} seconds");
code.LastSent = DateTime.UtcNow;
await db.SaveChangesAsync();
}
}
4 changes: 3 additions & 1 deletion JournalyApiV2/Services/DAL/IAuthDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public interface IAuthDbService
Task VerifyUser(Guid user);
Task<bool> CheckShortCode(Guid userId, string shortCode);
Task ResetEmailVerificationTimerAsync(Guid userId);
Task<string> GetOrGeneratePasswordResetCode(Guid userId);
Task<string?> GetPasswordResetCode(Guid userId);
Task<string> GeneratePasswordResetCode(Guid userId);
Task ResetPasswordResetTimerAsync(Guid userId);
}

0 comments on commit c72fec9

Please sign in to comment.