mirror of
https://github.com/bitwarden/server
synced 2025-12-21 18:53:41 +00:00
fix(2fa): [PM-22323] Do not show 2FA warning for 2FA setup and login emails
* Added configuration to not display 2FA setup instruction * Refactored to new service. * Linting. * Dependency injection * Changed to scoped to have access to ICurrentContext. * Inverted logic for EmailTotpAction * Fixed tests. * Fixed tests. * More tests. * Fixed tests. * Linting. * Added tests at controller level. * Linting * Fixed error in test. * Review updates. * Accidentally deleted imports.
This commit is contained in:
@@ -21,6 +21,7 @@ using Bit.Core.SecretsManager.Models.Mail;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Core.Vault.Models.Data;
|
||||
using Core.Auth.Enums;
|
||||
using HandlebarsDotNet;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
@@ -166,14 +167,14 @@ public class HandlebarsMailService : IMailService
|
||||
await _mailDeliveryService.SendEmailAsync(message);
|
||||
}
|
||||
|
||||
public async Task SendTwoFactorEmailAsync(string email, string accountEmail, string token, string deviceIp, string deviceType, bool authentication = true)
|
||||
public async Task SendTwoFactorEmailAsync(string email, string accountEmail, string token, string deviceIp, string deviceType, TwoFactorEmailPurpose purpose)
|
||||
{
|
||||
var message = CreateDefaultMessage("Your Bitwarden Verification Code", email);
|
||||
var requestDateTime = DateTime.UtcNow;
|
||||
var model = new TwoFactorEmailTokenViewModel
|
||||
{
|
||||
Token = token,
|
||||
EmailTotpAction = authentication ? "logging in" : "setting up two-step login",
|
||||
EmailTotpAction = (purpose == TwoFactorEmailPurpose.Setup) ? "setting up two-step login" : "logging in",
|
||||
AccountEmail = accountEmail,
|
||||
TheDate = requestDateTime.ToLongDateString(),
|
||||
TheTime = requestDateTime.ToShortTimeString(),
|
||||
@@ -182,6 +183,9 @@ public class HandlebarsMailService : IMailService
|
||||
DeviceType = deviceType,
|
||||
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
|
||||
SiteName = _globalSettings.SiteName,
|
||||
// We only want to remind users to set up 2FA if they're getting a new device verification email.
|
||||
// For login with 2FA, and setup of 2FA, we do not want to show the reminder because users are already doing so.
|
||||
DisplayTwoFactorReminder = purpose == TwoFactorEmailPurpose.NewDeviceVerification
|
||||
};
|
||||
await AddMessageContentAsync(message, "Auth.TwoFactorEmail", model);
|
||||
message.MetaData.Add("SendGridBypassListManagement", true);
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Reflection;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Claims;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.Enums;
|
||||
using Bit.Core.AdminConsole.Models.Data;
|
||||
@@ -337,52 +335,6 @@ public class UserService : UserManager<User>, IUserService
|
||||
await _mailService.SendMasterPasswordHintEmailAsync(email, user.MasterPasswordHint);
|
||||
}
|
||||
|
||||
public async Task SendTwoFactorEmailAsync(User user, bool authentication = true)
|
||||
{
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.TryGetValue("Email", out var emailValue))
|
||||
{
|
||||
throw new ArgumentNullException("No email.");
|
||||
}
|
||||
|
||||
var email = ((string)emailValue).ToLowerInvariant();
|
||||
var token = await base.GenerateTwoFactorTokenAsync(user,
|
||||
CoreHelpers.CustomProviderName(TwoFactorProviderType.Email));
|
||||
|
||||
var deviceType = _currentContext.DeviceType?.GetType().GetMember(_currentContext.DeviceType?.ToString())
|
||||
.FirstOrDefault()?.GetCustomAttribute<DisplayAttribute>()?.GetName() ?? "Unknown Browser";
|
||||
|
||||
await _mailService.SendTwoFactorEmailAsync(
|
||||
email, user.Email, token, _currentContext.IpAddress, deviceType, authentication);
|
||||
}
|
||||
|
||||
public async Task SendNewDeviceVerificationEmailAsync(User user)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(user);
|
||||
|
||||
var token = await base.GenerateUserTokenAsync(user, TokenOptions.DefaultEmailProvider,
|
||||
"otp:" + user.Email);
|
||||
|
||||
var deviceType = _currentContext.DeviceType?.GetType().GetMember(_currentContext.DeviceType?.ToString())
|
||||
.FirstOrDefault()?.GetCustomAttribute<DisplayAttribute>()?.GetName() ?? "Unknown Browser";
|
||||
|
||||
await _mailService.SendTwoFactorEmailAsync(
|
||||
user.Email, user.Email, token, _currentContext.IpAddress, deviceType);
|
||||
}
|
||||
|
||||
public async Task<bool> VerifyTwoFactorEmailAsync(User user, string token)
|
||||
{
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.TryGetValue("Email", out var emailValue))
|
||||
{
|
||||
throw new ArgumentNullException("No email.");
|
||||
}
|
||||
|
||||
var email = ((string)emailValue).ToLowerInvariant();
|
||||
return await base.VerifyTwoFactorTokenAsync(user,
|
||||
CoreHelpers.CustomProviderName(TwoFactorProviderType.Email), token);
|
||||
}
|
||||
|
||||
public async Task<CredentialCreateOptions> StartWebAuthnRegistrationAsync(User user)
|
||||
{
|
||||
var providers = user.GetTwoFactorProviders();
|
||||
@@ -1454,20 +1406,6 @@ public class UserService : UserManager<User>, IUserService
|
||||
return isVerified;
|
||||
}
|
||||
|
||||
public async Task ResendNewDeviceVerificationEmail(string email, string secret)
|
||||
{
|
||||
var user = await _userRepository.GetByEmailAsync(email);
|
||||
if (user == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (await VerifySecretAsync(user, secret))
|
||||
{
|
||||
await SendNewDeviceVerificationEmailAsync(user);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> ActiveNewDeviceVerificationException(Guid userId)
|
||||
{
|
||||
var cacheKey = string.Format(AuthConstants.NewDeviceVerificationExceptionCacheKeyFormat, userId.ToString());
|
||||
|
||||
Reference in New Issue
Block a user