Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions API/Services/Account/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,18 @@ public async Task<OneOf<Success<User>, AccountWithEmailOrUsernameExists>> Create

var user = accountCreate.AsT0.Value;

var secret = CryptoUtils.RandomString(AuthConstants.GeneratedTokenLength);
var token = CryptoUtils.RandomString(AuthConstants.GeneratedTokenLength);

user.UserActivationRequest = new UserActivationRequest
{
UserId = user.Id,
SecretHash = HashingUtils.HashToken(secret)
TokenHash = HashingUtils.HashToken(token)
};

await _db.SaveChangesAsync();

await _emailService.VerifyEmail(new Contact(email, username),
new Uri(_frontendConfig.BaseUrl, $"/#/account/activate/{user.Id}/{secret}"));
new Uri(_frontendConfig.BaseUrl, $"/#/account/activate/{user.Id}/{token}"));
return new Success<User>(user);
}

Expand Down Expand Up @@ -268,7 +268,7 @@ public async Task<OneOf<Success, NotFound, SecretInvalid>> CheckPasswordResetExi

if (reset is null) return new NotFound();

var result = HashingUtils.VerifyToken(secret, reset.SecretHash);
var result = HashingUtils.VerifyToken(secret, reset.TokenHash);
if (!result.Verified) return new SecretInvalid();

return new Success();
Expand All @@ -292,19 +292,18 @@ public async Task<OneOf<Success, TooManyPasswordResets, AccountDeactivated, NotF
if (user.User.UserDeactivation is not null) return new AccountDeactivated();
if (user.PasswordResetCount >= 3) return new TooManyPasswordResets();

var secret = CryptoUtils.RandomString(AuthConstants.GeneratedTokenLength);
var secretHash = HashingUtils.HashToken(secret);
var token = CryptoUtils.RandomString(AuthConstants.GeneratedTokenLength);
var passwordReset = new UserPasswordReset
{
Id = Guid.CreateVersion7(),
UserId = user.User.Id,
SecretHash = secretHash
TokenHash = HashingUtils.HashToken(token)
};
_db.UserPasswordResets.Add(passwordReset);
await _db.SaveChangesAsync();

await _emailService.PasswordReset(new Contact(user.User.Email, user.User.Name),
new Uri(_frontendConfig.BaseUrl, $"/#/account/password/recover/{passwordReset.Id}/{secret}"));
new Uri(_frontendConfig.BaseUrl, $"/#/account/password/recover/{passwordReset.Id}/{token}"));

return new Success();
}
Expand All @@ -322,7 +321,7 @@ public async Task<OneOf<Success, NotFound, AccountDeactivated, SecretInvalid>> C
if (reset is null) return new NotFound();
if (reset.User.UserDeactivation is not null) return new AccountDeactivated();

var result = HashingUtils.VerifyToken(secret, reset.SecretHash);
var result = HashingUtils.VerifyToken(secret, reset.TokenHash);
if (!result.Verified) return new SecretInvalid();

reset.UsedAt = DateTime.UtcNow;
Expand Down
Loading