Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply WebOTP pattern to short messages in Boilerplate (#9612) #9613

Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ 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);
var message = Localizer[nameof(AppStrings.ConfirmPhoneTokenSmsText), token];
var smsMessage = $"{message}{Environment.NewLine}@{HttpContext.Request.GetWebAppUrl().Host} #{token}" /* Web OTP */;
await phoneService.SendSms(smsMessage, phoneNumber, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public async Task SendResetPasswordToken(SendResetPasswordTokenRequestDto reques

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

//#if (signalR == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ 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 message = Localizer[nameof(AppStrings.OtpShortText), token].ToString();
var smsMessage = $"{message}{Environment.NewLine}@{HttpContext.Request.GetWebAppUrl().Host} #{token}" /* Web OTP */;
sendMessagesTasks.Add(phoneService.SendSms(smsMessage, user.PhoneNumber!, cancellationToken));
}

Expand Down Expand Up @@ -368,7 +370,8 @@ public async Task SendTwoFactorToken(SignInRequestDto request, CancellationToken

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

if (firstStepAuthenticationMethod != "Push")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ 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);
var message = Localizer[nameof(AppStrings.ChangePhoneNumberTokenSmsText), token];
var smsMessage = $"{message}{Environment.NewLine}@{HttpContext.Request.GetWebAppUrl().Host} #{token}" /* Web OTP */;

await phoneService.SendSms(smsMessage, request.PhoneNumber!, cancellationToken);
}

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

List<Task> sendMessagesTasks = [];

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

if (await userManager.IsEmailConfirmedAsync(user))
{
Expand All @@ -397,7 +400,8 @@ public async Task SendElevatedAccessToken(CancellationToken cancellationToken)

if (await userManager.IsPhoneNumberConfirmedAsync(user))
{
sendMessagesTasks.Add(phoneService.SendSms(messageText, user.PhoneNumber!, cancellationToken));
var smsMessage = $"{message}{Environment.NewLine}@{HttpContext.Request.GetWebAppUrl().Host} #{token}" /* Web OTP */;
sendMessagesTasks.Add(phoneService.SendSms(smsMessage, user.PhoneNumber!, cancellationToken));
}

//#if (signalR == true)
Expand All @@ -406,11 +410,11 @@ public async Task SendElevatedAccessToken(CancellationToken cancellationToken)
.Where(us => us.UserId == user.Id && us.Id != currentUserSessionId && us.SignalRConnectionId != null)
.Select(us => us.SignalRConnectionId!)
.ToArrayAsync(cancellationToken);
sendMessagesTasks.Add(appHubContext.Clients.Clients(userSessionIdsExceptCurrentUserSessionId).SendAsync(SignalREvents.SHOW_MESSAGE, messageText, cancellationToken));
sendMessagesTasks.Add(appHubContext.Clients.Clients(userSessionIdsExceptCurrentUserSessionId).SendAsync(SignalREvents.SHOW_MESSAGE, message, cancellationToken));
//#endif

//#if (notification == true)
sendMessagesTasks.Add(pushNotificationService.RequestPush(message: messageText, userRelatedPush: true, customSubscriptionFilter: us => us.UserSession!.UserId == user.Id && us.UserSessionId != currentUserSessionId, cancellationToken: cancellationToken));
sendMessagesTasks.Add(pushNotificationService.RequestPush(message: message, userRelatedPush: true, customSubscriptionFilter: us => us.UserSession!.UserId == user.Id && us.UserSessionId != currentUserSessionId, cancellationToken: cancellationToken));
//#endif

await Task.WhenAll(sendMessagesTasks);
Expand Down
Loading