mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
[EC-400] Code clean up Device Verification (#2601)
* EC-400 Clean up code regarding Unknown Device Verification * EC-400 Fix formatting
This commit is contained in:

committed by
GitHub

parent
7594ca1122
commit
69511160cb
@ -323,7 +323,6 @@ public class EmergencyAccessService : IEmergencyAccessService
|
||||
grantor.Key = key;
|
||||
// Disable TwoFactor providers since they will otherwise block logins
|
||||
grantor.SetTwoFactorProviders(new Dictionary<TwoFactorProviderType, TwoFactorProvider>());
|
||||
grantor.UnknownDeviceVerificationEnabled = false;
|
||||
await _userRepository.ReplaceAsync(grantor);
|
||||
|
||||
// Remove grantor from all organizations unless Owner
|
||||
|
@ -113,21 +113,6 @@ public class HandlebarsMailService : IMailService
|
||||
await _mailDeliveryService.SendEmailAsync(message);
|
||||
}
|
||||
|
||||
public async Task SendNewDeviceLoginTwoFactorEmailAsync(string email, string token)
|
||||
{
|
||||
var message = CreateDefaultMessage("New Device Login Verification Code", email);
|
||||
var model = new EmailTokenViewModel
|
||||
{
|
||||
Token = token,
|
||||
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
|
||||
SiteName = _globalSettings.SiteName
|
||||
};
|
||||
await AddMessageContentAsync(message, "NewDeviceLoginTwoFactorEmail", model);
|
||||
message.MetaData.Add("SendGridBypassListManagement", true);
|
||||
message.Category = "TwoFactorEmail";
|
||||
await _mailDeliveryService.SendEmailAsync(message);
|
||||
}
|
||||
|
||||
public async Task SendMasterPasswordHintEmailAsync(string email, string hint)
|
||||
{
|
||||
var message = CreateDefaultMessage("Your Master Password Hint", email);
|
||||
|
@ -46,7 +46,6 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
private readonly IGlobalSettings _globalSettings;
|
||||
private readonly IOrganizationService _organizationService;
|
||||
private readonly IProviderUserRepository _providerUserRepository;
|
||||
private readonly IDeviceRepository _deviceRepository;
|
||||
private readonly IStripeSyncService _stripeSyncService;
|
||||
|
||||
public UserService(
|
||||
@ -77,7 +76,6 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
IGlobalSettings globalSettings,
|
||||
IOrganizationService organizationService,
|
||||
IProviderUserRepository providerUserRepository,
|
||||
IDeviceRepository deviceRepository,
|
||||
IStripeSyncService stripeSyncService)
|
||||
: base(
|
||||
store,
|
||||
@ -113,7 +111,6 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
_globalSettings = globalSettings;
|
||||
_organizationService = organizationService;
|
||||
_providerUserRepository = providerUserRepository;
|
||||
_deviceRepository = deviceRepository;
|
||||
_stripeSyncService = stripeSyncService;
|
||||
}
|
||||
|
||||
@ -353,7 +350,7 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
await _mailService.SendMasterPasswordHintEmailAsync(email, user.MasterPasswordHint);
|
||||
}
|
||||
|
||||
public async Task SendTwoFactorEmailAsync(User user, bool isBecauseNewDeviceLogin = false)
|
||||
public async Task SendTwoFactorEmailAsync(User user)
|
||||
{
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.ContainsKey("Email"))
|
||||
@ -365,14 +362,7 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
var token = await base.GenerateUserTokenAsync(user, TokenOptions.DefaultEmailProvider,
|
||||
"2faEmail:" + email);
|
||||
|
||||
if (isBecauseNewDeviceLogin)
|
||||
{
|
||||
await _mailService.SendNewDeviceLoginTwoFactorEmailAsync(email, token);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _mailService.SendTwoFactorEmailAsync(email, token);
|
||||
}
|
||||
await _mailService.SendTwoFactorEmailAsync(email, token);
|
||||
}
|
||||
|
||||
public async Task<bool> VerifyTwoFactorEmailAsync(User user, string token)
|
||||
@ -1478,36 +1468,4 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
? await VerifyOTPAsync(user, secret)
|
||||
: await CheckPasswordAsync(user, secret);
|
||||
}
|
||||
|
||||
public async Task<bool> Needs2FABecauseNewDeviceAsync(User user, string deviceIdentifier, string grantType)
|
||||
{
|
||||
return CanEditDeviceVerificationSettings(user)
|
||||
&& user.UnknownDeviceVerificationEnabled
|
||||
&& grantType != "authorization_code"
|
||||
&& await IsNewDeviceAndNotTheFirstOneAsync(user, deviceIdentifier);
|
||||
}
|
||||
|
||||
public bool CanEditDeviceVerificationSettings(User user)
|
||||
{
|
||||
return _globalSettings.TwoFactorAuth.EmailOnNewDeviceLogin
|
||||
&& user.EmailVerified
|
||||
&& !user.UsesKeyConnector
|
||||
&& !(user.GetTwoFactorProviders()?.Any() ?? false);
|
||||
}
|
||||
|
||||
private async Task<bool> IsNewDeviceAndNotTheFirstOneAsync(User user, string deviceIdentifier)
|
||||
{
|
||||
if (user == null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
var devices = await _deviceRepository.GetManyByUserIdAsync(user.Id);
|
||||
if (!devices.Any())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return !devices.Any(d => d.Identifier == deviceIdentifier);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user