Skip to content

Commit

Permalink
Apply Web OTP pattern to Boilerplate short sent messages (#9612)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmoradi committed Jan 3, 2025
1 parent 9a3fcb4 commit b61957d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ private async Task SendConfirmPhoneToken(User user, CancellationToken cancellati
var phoneNumber = user.PhoneNumber!;
var token = await userManager.GenerateUserTokenAsync(user, TokenOptions.DefaultPhoneProvider, FormattableString.Invariant($"VerifyPhoneNumber:{phoneNumber},{user.PhoneNumberTokenRequestedOn?.ToUniversalTime()}"));

await phoneService.SendSms(Localizer[nameof(AppStrings.ConfirmPhoneTokenSmsText), token], phoneNumber, cancellationToken);
await phoneService.SendSms(Localizer[nameof(AppStrings.ConfirmPhoneTokenSmsText), $"#{token}", $"@{HttpContext.Request.GetWebAppUrl().Host}" /*Web OTP*/], phoneNumber, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ public async Task SendResetPasswordToken(SendResetPasswordTokenRequestDto reques
sendMessagesTasks.Add(emailService.SendResetPasswordToken(user, token, link, cancellationToken));
}

var message = Localizer[nameof(AppStrings.ResetPasswordTokenShortText), token].ToString();

if (await userManager.IsPhoneNumberConfirmedAsync(user))
{
sendMessagesTasks.Add(phoneService.SendSms(message, user.PhoneNumber!, cancellationToken));
var smsMessage = Localizer[nameof(AppStrings.ResetPasswordTokenShortText), $"#{token}", $"@{HttpContext.Request.GetWebAppUrl().Host}" /*Web OTP*/].ToString();
sendMessagesTasks.Add(phoneService.SendSms(smsMessage, user.PhoneNumber!, cancellationToken));
}

var message = Localizer[nameof(AppStrings.ResetPasswordTokenShortText), token].ToString();
//#if (signalR == true)
sendMessagesTasks.Add(appHubContext.Clients.User(user.Id.ToString()).SendAsync(SignalREvents.SHOW_MESSAGE, message, cancellationToken));
//#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ public async Task SendOtp(IdentityRequestDto request, string? returnUrl = null,

if (await userManager.IsPhoneNumberConfirmedAsync(user))
{
var smsMessage = Localizer[nameof(AppStrings.OtpShortText), await userManager.GenerateUserTokenAsync(user, TokenOptions.DefaultPhoneProvider, FormattableString.Invariant($"Otp_Sms,{user.OtpRequestedOn?.ToUniversalTime()}"))].ToString();
var token = await userManager.GenerateUserTokenAsync(user, TokenOptions.DefaultPhoneProvider, FormattableString.Invariant($"Otp_Sms,{user.OtpRequestedOn?.ToUniversalTime()}"));
var smsMessage = Localizer[nameof(AppStrings.OtpShortText), $"#{token}", $"@{HttpContext.Request.GetWebAppUrl().Host}" /*Web OTP*/].ToString();
sendMessagesTasks.Add(phoneService.SendSms(smsMessage, user.PhoneNumber!, cancellationToken));
}

Expand Down Expand Up @@ -364,15 +365,15 @@ public async Task SendTwoFactorToken(SignInRequestDto request, CancellationToken
sendMessagesTasks.Add(emailService.SendTwoFactorToken(user, token, cancellationToken));
}

var message = Localizer[nameof(AppStrings.TwoFactorTokenShortText), token].ToString();

if (firstStepAuthenticationMethod != "Sms" && await userManager.IsPhoneNumberConfirmedAsync(user))
{
sendMessagesTasks.Add(phoneService.SendSms(message, user.PhoneNumber!, cancellationToken));
var smsMessage = Localizer[nameof(AppStrings.TwoFactorTokenShortText), $"#{token}", $"@{HttpContext.Request.GetWebAppUrl().Host}" /*Web OTP*/].ToString();
sendMessagesTasks.Add(phoneService.SendSms(smsMessage, user.PhoneNumber!, cancellationToken));
}

if (firstStepAuthenticationMethod != "Push")
{
var message = Localizer[nameof(AppStrings.TwoFactorTokenShortText), token].ToString();
//#if (signalR == true)
sendMessagesTasks.Add(appHubContext.Clients.User(user.Id.ToString()).SendAsync(SignalREvents.SHOW_MESSAGE, message, cancellationToken));
//#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public async Task SendChangePhoneNumberToken(SendPhoneTokenRequestDto request, C

var token = await userManager.GenerateChangePhoneNumberTokenAsync(user!, request.PhoneNumber!);

await phoneService.SendSms(Localizer[nameof(AppStrings.ChangePhoneNumberTokenSmsText), token], request.PhoneNumber!, cancellationToken);
await phoneService.SendSms(Localizer[nameof(AppStrings.ChangePhoneNumberTokenSmsText), $"#{token}", $"@{HttpContext.Request.GetWebAppUrl().Host}" /*Web OTP*/], request.PhoneNumber!, cancellationToken);
}

[HttpPost]
Expand Down Expand Up @@ -388,16 +388,18 @@ public async Task SendElevatedAccessToken(CancellationToken cancellationToken)

List<Task> sendMessagesTasks = [];

var messageText = Localizer[nameof(AppStrings.ElevatedAccessToken), token].ToString();

if (await userManager.IsEmailConfirmedAsync(user))
if (await userManager.IsPhoneNumberConfirmedAsync(user))
{
sendMessagesTasks.Add(emailService.SendElevatedAccessToken(user, token, cancellationToken));
var smsMessage = Localizer[nameof(AppStrings.ElevatedAccessToken), $"#{token}", $"@{HttpContext.Request.GetWebAppUrl().Host}" /*Web OTP*/].ToString();
sendMessagesTasks.Add(phoneService.SendSms(smsMessage, user.PhoneNumber!, cancellationToken));
}

if (await userManager.IsPhoneNumberConfirmedAsync(user))
var messageText = Localizer[nameof(AppStrings.ElevatedAccessToken), token].ToString();

if (await userManager.IsEmailConfirmedAsync(user))
{
sendMessagesTasks.Add(phoneService.SendSms(messageText, user.PhoneNumber!, cancellationToken));
sendMessagesTasks.Add(emailService.SendElevatedAccessToken(user, token, cancellationToken));
}

//#if (signalR == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,8 @@
<value>لطفا کد ۶ رقمی که ارسال کردیم یا کد Authenticator app خود را وارد کنید</value>
</data>
<data name="ElevatedAccessToken" xml:space="preserve">
<value>توکن {0}</value>
<value>توکن {0}
{1} {0}</value>
</data>
<data name="WaitForEmailTokenRequestResendDelay" xml:space="preserve">
<value>شما قبلا ایمیل تایید را درخواست کرده اید. دوباره امتحان کنید در {0}</value>
Expand Down Expand Up @@ -988,19 +989,24 @@
<value>شما قبلا درخواست ایمیل توکن 2FA را دارید. دوباره امتحان کنید در {0}</value>
</data>
<data name="ChangePhoneNumberTokenSmsText" xml:space="preserve">
<value>{0} توکن تغییر شماره تلفن شما است در Boilerplate.</value>
<value>{0} توکن تغییر شماره تلفن شما است در Boilerplate
{1} {0}</value>
</data>
<data name="ConfirmPhoneTokenSmsText" xml:space="preserve">
<value>{0} توکن تایید شماره تلفن شما است در Boilerplate.</value>
<value>{0} توکن تایید شماره تلفن شما است در Boilerplate
{1} {0}</value>
</data>
<data name="TwoFactorTokenShortText" xml:space="preserve">
<value>{0} توکن احراز هویت مرحله دو شما است در Boilerplate.</value>
<value>{0} توکن احراز هویت مرحله دو شما است در Boilerplate
{1} {0}</value>
</data>
<data name="OtpShortText" xml:space="preserve">
<value>{0} پسورد یک‌بار مصرف شما است در Boilerplate.</value>
<value>{0} پسورد یک‌بار مصرف شما است در Boilerplate
{1} {0}</value>
</data>
<data name="ResetPasswordTokenShortText" xml:space="preserve">
<value>{0} توکن تغییر رمز عبور شما است در Boilerplate.</value>
<value>{0} توکن تغییر رمز عبور شما است در Boilerplate
{1} {0}</value>
</data>
<data name="Online" xml:space="preserve">
<value>آنلاین</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,8 @@
<value>Voer de verhoogde toegangstoken in die we u zojuist hebben gestuurd of de code van uw authenticator-app om door te gaan.</value>
</data>
<data name="ElevatedAccessToken" xml:space="preserve">
<value>Token {0}</value>
<value>Token {0}
{1} {0}</value>
</data>
<data name="WaitForEmailTokenRequestResendDelay" xml:space="preserve">
<value>Je hebt de bevestigingsmail al aangevraagd. Probeer het opnieuw in {0}</value>
Expand Down Expand Up @@ -988,19 +989,24 @@
<value>Je hebt de e-mail met de 2FA-token al aangevraagd. Probeer het opnieuw in {0}.</value>
</data>
<data name="ChangePhoneNumberTokenSmsText" xml:space="preserve">
<value>{0} is het token voor het wijzigen van uw telefoonnummer in Boilerplate.</value>
<value>{0} is het token voor het wijzigen van uw telefoonnummer in Boilerplate
{1} {0}</value>
</data>
<data name="ConfirmPhoneTokenSmsText" xml:space="preserve">
<value>{0} is uw bevestigingstelefoonnummer token in Boilerplate.</value>
<value>{0} is uw bevestigingstelefoonnummer token in Boilerplate
{1} {0}</value>
</data>
<data name="TwoFactorTokenShortText" xml:space="preserve">
<value>{0} is je tweefactortoken in Boilerplate.</value>
<value>{0} is je tweefactortoken in Boilerplate
{1} {0}</value>
</data>
<data name="OtpShortText" xml:space="preserve">
<value>{0} is uw OTP in Boilerplate.</value>
<value>{0} is uw OTP in Boilerplate
{1} {0}</value>
</data>
<data name="ResetPasswordTokenShortText" xml:space="preserve">
<value>{0} is het token voor het opnieuw instellen van uw wachtwoord in Boilerplate.</value>
<value>{0} is het token voor het opnieuw instellen van uw wachtwoord in Boilerplate
{1} {0}</value>
</data>
<data name="Online" xml:space="preserve">
<value>Online</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,8 @@
<value>Please enter the elevated access token we just sent you or your authenticator app code to continue.</value>
</data>
<data name="ElevatedAccessToken" xml:space="preserve">
<value>Token {0}</value>
<value>Token {0}
{1} {0}</value>
</data>
<data name="WaitForEmailTokenRequestResendDelay" xml:space="preserve">
<value>You have already requested the confirmation email. Try again in {0}</value>
Expand Down Expand Up @@ -988,19 +989,24 @@
<value>You have already requested the 2FA token email. Try again in {0}.</value>
</data>
<data name="ChangePhoneNumberTokenSmsText" xml:space="preserve">
<value>{0} is your change phone number token in Boilerplate.</value>
<value>{0} is your change phone number token in Boilerplate
{1} {0}</value>
</data>
<data name="ConfirmPhoneTokenSmsText" xml:space="preserve">
<value>{0} is your confirm phone number token in Boilerplate.</value>
<value>{0} is your confirm phone number token in Boilerplate
{1} {0}</value>
</data>
<data name="TwoFactorTokenShortText" xml:space="preserve">
<value>{0} is your two factor token in Boilerplate.</value>
<value>{0} is your two factor token in Boilerplate
{1} {0}</value>
</data>
<data name="OtpShortText" xml:space="preserve">
<value>{0} is your OTP in Boilerplate.</value>
<value>{0} is your OTP in Boilerplate
{1} {0}</value>
</data>
<data name="ResetPasswordTokenShortText" xml:space="preserve">
<value>{0} is your reset password token in Boilerplate.</value>
<value>{0} is your reset password token in Boilerplate
{1} {0}</value>
</data>
<data name="Online" xml:space="preserve">
<value>Online</value>
Expand Down

0 comments on commit b61957d

Please sign in to comment.