-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resending the one time password for login and signup does (#666)
### Summary & Motivation Introduce the ability to resend one-time passwords during the signup and login processes. To achieve this, new `signup/resend-code` and `login/resend-code` API endpoints have been implemented, along with `ResendSignupCodeCommand` and `ResendLoginCodeCommand`. The Signup and Login aggregates have been updated with a new `UpdateVerificationCode` method to support this functionality. The link to resend the login code has been moved below the Verify button for simplicity. When a code is resent, the verification timeout is extended by an additional 5 minutes. A 30-second cooldown between sending verification codes has also been introduced to prevent abuse. ### Checklist - [x] I have added tests, or done manual regression tests - [x] I have updated the documentation, if necessary
- Loading branch information
Showing
13 changed files
with
373 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
application/account-management/Core/Features/Authentication/Commands/ResendLoginCode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using JetBrains.Annotations; | ||
using Microsoft.AspNetCore.Identity; | ||
using PlatformPlatform.AccountManagement.Features.Authentication.Domain; | ||
using PlatformPlatform.AccountManagement.Features.Users.Domain; | ||
using PlatformPlatform.SharedKernel.Authentication; | ||
using PlatformPlatform.SharedKernel.Cqrs; | ||
using PlatformPlatform.SharedKernel.Integrations.Email; | ||
using PlatformPlatform.SharedKernel.Telemetry; | ||
|
||
namespace PlatformPlatform.AccountManagement.Features.Authentication.Commands; | ||
|
||
[PublicAPI] | ||
public sealed record ResendLoginCodeCommand : ICommand, IRequest<Result<ResendLoginCodeResponse>> | ||
{ | ||
[JsonIgnore] // Removes this property from the API contract | ||
public LoginId Id { get; init; } = null!; | ||
} | ||
|
||
[PublicAPI] | ||
public sealed record ResendLoginCodeResponse(int ValidForSeconds); | ||
|
||
public sealed class ResendLoginCodeCommandHandler( | ||
ILoginRepository loginRepository, | ||
IUserRepository userRepository, | ||
IEmailClient emailClient, | ||
IPasswordHasher<object> passwordHasher, | ||
ITelemetryEventsCollector events, | ||
ILogger<ResendLoginCodeCommandHandler> logger | ||
) : IRequestHandler<ResendLoginCodeCommand, Result<ResendLoginCodeResponse>> | ||
{ | ||
public async Task<Result<ResendLoginCodeResponse>> Handle(ResendLoginCodeCommand command, CancellationToken cancellationToken) | ||
{ | ||
var login = await loginRepository.GetByIdAsync(command.Id, cancellationToken); | ||
if (login is null) | ||
{ | ||
return Result<ResendLoginCodeResponse>.NotFound($"Login with id '{command.Id}' not found."); | ||
} | ||
|
||
if (login.Completed) | ||
{ | ||
logger.LogWarning("Login with id '{LoginId}' has already been completed.", login.Id); | ||
return Result<ResendLoginCodeResponse>.BadRequest($"The login with id {login.Id} has already been completed."); | ||
} | ||
|
||
if (login.ModifiedAt > TimeProvider.System.GetUtcNow().AddSeconds(-30)) | ||
{ | ||
return Result<ResendLoginCodeResponse>.BadRequest("You must wait at least 30 seconds before requesting a new code."); | ||
} | ||
|
||
var user = await userRepository.GetByIdUnfilteredAsync(login.UserId, cancellationToken); | ||
|
||
var oneTimePassword = OneTimePasswordHelper.GenerateOneTimePassword(6); | ||
var oneTimePasswordHash = passwordHasher.HashPassword(this, oneTimePassword); | ||
login.UpdateVerificationCode(oneTimePasswordHash); | ||
loginRepository.Update(login); | ||
|
||
var secondsSinceLoginStarted = (TimeProvider.System.GetUtcNow() - login.CreatedAt).TotalSeconds; | ||
events.CollectEvent(new LoginCodeResend(login.UserId, (int)secondsSinceLoginStarted)); | ||
|
||
await emailClient.SendAsync(user!.Email, "PlatformPlatform login verification code", | ||
$""" | ||
<h1 style="text-align:center;font-family=sans-serif;font-size:20px">Your confirmation code is below</h1> | ||
<p style="text-align:center;font-family=sans-serif;font-size:16px">Enter it in your open browser window. It is only valid for a few minutes.</p> | ||
<p style="text-align:center;font-family=sans-serif;font-size:40px;background:#f5f4f5">{oneTimePassword}</p> | ||
""", | ||
cancellationToken | ||
); | ||
|
||
return new ResendLoginCodeResponse(Login.ValidForSeconds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
application/account-management/Core/Features/Signups/Commands/ResendSignupCode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using JetBrains.Annotations; | ||
using Microsoft.AspNetCore.Identity; | ||
using PlatformPlatform.AccountManagement.Features.Signups.Domain; | ||
using PlatformPlatform.SharedKernel.Authentication; | ||
using PlatformPlatform.SharedKernel.Cqrs; | ||
using PlatformPlatform.SharedKernel.Integrations.Email; | ||
using PlatformPlatform.SharedKernel.Telemetry; | ||
|
||
namespace PlatformPlatform.AccountManagement.Features.Signups.Commands; | ||
|
||
[PublicAPI] | ||
public sealed record ResendSignupCodeCommand : ICommand, IRequest<Result<ResendSignupCodeResponse>> | ||
{ | ||
[JsonIgnore] // Removes this property from the API contract | ||
public SignupId Id { get; init; } = null!; | ||
} | ||
|
||
[PublicAPI] | ||
public sealed record ResendSignupCodeResponse(int ValidForSeconds); | ||
|
||
public sealed class ResendSignupCodeCommandHandler( | ||
ISignupRepository signupRepository, | ||
IEmailClient emailClient, | ||
IPasswordHasher<object> passwordHasher, | ||
ITelemetryEventsCollector events, | ||
ILogger<ResendSignupCodeCommandHandler> logger | ||
) : IRequestHandler<ResendSignupCodeCommand, Result<ResendSignupCodeResponse>> | ||
{ | ||
public async Task<Result<ResendSignupCodeResponse>> Handle(ResendSignupCodeCommand command, CancellationToken cancellationToken) | ||
{ | ||
var signup = await signupRepository.GetByIdAsync(command.Id, cancellationToken); | ||
if (signup is null) | ||
{ | ||
return Result<ResendSignupCodeResponse>.NotFound($"Signup with id '{command.Id}' not found."); | ||
} | ||
|
||
if (signup.Completed) | ||
{ | ||
logger.LogWarning("Signup with id '{SignupId}' has already been completed.", signup.Id); | ||
return Result<ResendSignupCodeResponse>.BadRequest($"The signup with id {signup.Id} has already been completed."); | ||
} | ||
|
||
if (signup.ModifiedAt > TimeProvider.System.GetUtcNow().AddSeconds(-30)) | ||
{ | ||
return Result<ResendSignupCodeResponse>.BadRequest("You must wait at least 30 seconds before requesting a new code."); | ||
} | ||
|
||
var oneTimePassword = OneTimePasswordHelper.GenerateOneTimePassword(6); | ||
var oneTimePasswordHash = passwordHasher.HashPassword(this, oneTimePassword); | ||
signup.UpdateVerificationCode(oneTimePasswordHash); | ||
signupRepository.Update(signup); | ||
|
||
var secondsSinceSignupStarted = (TimeProvider.System.GetUtcNow() - signup.CreatedAt).TotalSeconds; | ||
events.CollectEvent(new SignupCodeResend(signup.TenantId, (int)secondsSinceSignupStarted)); | ||
|
||
await emailClient.SendAsync(signup.Email, "Confirm your email address", | ||
$""" | ||
<h1 style="text-align:center;font-family=sans-serif;font-size:20px">Your confirmation code is below</h1> | ||
<p style="text-align:center;font-family=sans-serif;font-size:16px">Enter it in your open browser window. It is only valid for a few minutes.</p> | ||
<p style="text-align:center;font-family=sans-serif;font-size:40px;background:#f5f4f5">{oneTimePassword}</p> | ||
""", | ||
cancellationToken | ||
); | ||
|
||
|
||
return new ResendSignupCodeResponse(Signup.ValidForSeconds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.