mirror of
https://github.com/bitwarden/server
synced 2025-12-19 01:33:20 +00:00
[PM-14476] Avoid multiple lookups in dictionaries (#4973)
* Avoid multiple lookups in dictionaries * Consistency in fallback to empty CollectionIds * Readability at the cost of lines changed * Readability * Changes after running dotnet format
This commit is contained in:
@@ -357,12 +357,12 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
public async Task SendTwoFactorEmailAsync(User user, bool authentication = true)
|
||||
{
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.ContainsKey("Email"))
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.TryGetValue("Email", out var emailValue))
|
||||
{
|
||||
throw new ArgumentNullException("No email.");
|
||||
}
|
||||
|
||||
var email = ((string)provider.MetaData["Email"]).ToLowerInvariant();
|
||||
var email = ((string)emailValue).ToLowerInvariant();
|
||||
var token = await base.GenerateTwoFactorTokenAsync(user,
|
||||
CoreHelpers.CustomProviderName(TwoFactorProviderType.Email));
|
||||
|
||||
@@ -390,12 +390,12 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
public async Task<bool> VerifyTwoFactorEmailAsync(User user, string token)
|
||||
{
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.ContainsKey("Email"))
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.TryGetValue("Email", out var emailValue))
|
||||
{
|
||||
throw new ArgumentNullException("No email.");
|
||||
}
|
||||
|
||||
var email = ((string)provider.MetaData["Email"]).ToLowerInvariant();
|
||||
var email = ((string)emailValue).ToLowerInvariant();
|
||||
return await base.VerifyTwoFactorTokenAsync(user,
|
||||
CoreHelpers.CustomProviderName(TwoFactorProviderType.Email), token);
|
||||
}
|
||||
@@ -453,12 +453,12 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
var keyId = $"Key{id}";
|
||||
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.WebAuthn);
|
||||
if (!provider?.MetaData?.ContainsKey("pending") ?? true)
|
||||
if (provider?.MetaData is null || !provider.MetaData.TryGetValue("pending", out var pendingValue))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var options = CredentialCreateOptions.FromJson((string)provider.MetaData["pending"]);
|
||||
var options = CredentialCreateOptions.FromJson((string)pendingValue);
|
||||
|
||||
// Callback to ensure credential ID is unique. Always return true since we don't care if another
|
||||
// account uses the same 2FA key.
|
||||
@@ -1353,14 +1353,14 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
public void SetTwoFactorProvider(User user, TwoFactorProviderType type, bool setEnabled = true)
|
||||
{
|
||||
var providers = user.GetTwoFactorProviders();
|
||||
if (!providers?.ContainsKey(type) ?? true)
|
||||
if (providers is null || !providers.TryGetValue(type, out var provider))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (setEnabled)
|
||||
{
|
||||
providers[type].Enabled = true;
|
||||
provider.Enabled = true;
|
||||
}
|
||||
user.SetTwoFactorProviders(providers);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user