1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 16:12:49 -05:00

two factor email setup

This commit is contained in:
Kyle Spearrin
2017-06-20 09:21:35 -04:00
parent 2eaaecd95c
commit 475160cfe1
4 changed files with 81 additions and 2 deletions

View File

@ -182,6 +182,45 @@ namespace Bit.Core.Services
await _mailService.SendMasterPasswordHintEmailAsync(email, user.MasterPasswordHint);
}
public async Task SendTwoFactorEmailAsync(User user, string email = null)
{
if(string.IsNullOrWhiteSpace(email))
{
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
if(provider != null && provider.MetaData != null && provider.MetaData.ContainsKey("Email"))
{
email = provider.MetaData["Email"];
}
}
if(string.IsNullOrWhiteSpace(email))
{
throw new ArgumentNullException(nameof(email));
}
var token = await base.GenerateUserTokenAsync(user, TokenOptions.DefaultEmailProvider, "2faEmail:" + email);
await _mailService.SendChangeEmailEmailAsync(email, token);
}
public async Task<bool> VerifyTwoFactorEmailAsync(User user, string token, string email = null)
{
if(string.IsNullOrWhiteSpace(email))
{
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
if(provider != null && provider.MetaData != null && provider.MetaData.ContainsKey("Email"))
{
email = provider.MetaData["Email"];
}
}
if(string.IsNullOrWhiteSpace(email))
{
throw new ArgumentNullException(nameof(email));
}
return await base.VerifyUserTokenAsync(user, TokenOptions.DefaultEmailProvider, "2faEmail:" + email, token);
}
public async Task InitiateEmailChangeAsync(User user, string newEmail)
{
var existingUser = await _userRepository.GetByEmailAsync(newEmail);
@ -329,6 +368,11 @@ namespace Bit.Core.Services
return;
}
break;
case TwoFactorProviderType.Email:
case TwoFactorProviderType.U2F:
case TwoFactorProviderType.YubiKey:
case TwoFactorProviderType.Duo:
break;
default:
throw new ArgumentException(nameof(provider));
}
@ -356,6 +400,11 @@ namespace Bit.Core.Services
var key = KeyGeneration.GenerateRandomKey(20);
providerInfo.MetaData = new Dictionary<string, string> { ["Key"] = Base32Encoding.ToString(key) };
break;
case TwoFactorProviderType.Email:
case TwoFactorProviderType.U2F:
case TwoFactorProviderType.YubiKey:
case TwoFactorProviderType.Duo:
break;
default:
throw new ArgumentException(nameof(provider));
}