mirror of
https://github.com/bitwarden/server.git
synced 2025-07-07 02:52:50 -05:00
yubikey setup for 2FA
This commit is contained in:
@ -57,12 +57,75 @@ namespace Bit.Core.Models.Api
|
||||
public string Key4 { get; set; }
|
||||
public string Key5 { get; set; }
|
||||
|
||||
public User ToUser(User extistingUser)
|
||||
{
|
||||
var providers = extistingUser.GetTwoFactorProviders();
|
||||
if(providers == null)
|
||||
{
|
||||
providers = new Dictionary<TwoFactorProviderType, TwoFactorProvider>();
|
||||
}
|
||||
else if(providers.ContainsKey(TwoFactorProviderType.YubiKey))
|
||||
{
|
||||
providers.Remove(TwoFactorProviderType.YubiKey);
|
||||
}
|
||||
|
||||
providers.Add(TwoFactorProviderType.YubiKey, new TwoFactorProvider
|
||||
{
|
||||
MetaData = new Dictionary<string, string>
|
||||
{
|
||||
["Key1"] = FormatKey(Key1),
|
||||
["Key2"] = FormatKey(Key2),
|
||||
["Key3"] = FormatKey(Key3),
|
||||
["Key4"] = FormatKey(Key4),
|
||||
["Key5"] = FormatKey(Key5)
|
||||
},
|
||||
Enabled = true
|
||||
});
|
||||
extistingUser.SetTwoFactorProviders(providers);
|
||||
return extistingUser;
|
||||
}
|
||||
|
||||
private string FormatKey(string keyValue)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(keyValue))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return keyValue.Substring(0, 12);
|
||||
}
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(Key1) && string.IsNullOrWhiteSpace(Key2) && string.IsNullOrWhiteSpace(Key3) &&
|
||||
string.IsNullOrWhiteSpace(Key4) && string.IsNullOrWhiteSpace(Key5))
|
||||
{
|
||||
yield return new ValidationResult("A Key is required.", new string[] { nameof(Key1) });
|
||||
yield return new ValidationResult("A key is required.", new string[] { nameof(Key1) });
|
||||
}
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(Key1) && Key1.Length < 12)
|
||||
{
|
||||
yield return new ValidationResult("Key 1 in invalid.", new string[] { nameof(Key1) });
|
||||
}
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(Key2) && Key2.Length < 12)
|
||||
{
|
||||
yield return new ValidationResult("Key 2 in invalid.", new string[] { nameof(Key2) });
|
||||
}
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(Key3) && Key3.Length < 12)
|
||||
{
|
||||
yield return new ValidationResult("Key 3 in invalid.", new string[] { nameof(Key3) });
|
||||
}
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(Key4) && Key4.Length < 12)
|
||||
{
|
||||
yield return new ValidationResult("Key 4 in invalid.", new string[] { nameof(Key4) });
|
||||
}
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(Key5) && Key5.Length < 12)
|
||||
{
|
||||
yield return new ValidationResult("Key 5 in invalid.", new string[] { nameof(Key5) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ namespace Bit.Core.Models.Api
|
||||
throw new ArgumentNullException(nameof(user));
|
||||
}
|
||||
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.YubiKey);
|
||||
if(provider?.MetaData != null && provider.MetaData.Count > 0)
|
||||
{
|
||||
Enabled = provider.Enabled;
|
||||
@ -29,7 +29,7 @@ namespace Bit.Core.Models.Api
|
||||
}
|
||||
if(provider.MetaData.ContainsKey("Key3"))
|
||||
{
|
||||
Key1 = provider.MetaData["Key3"];
|
||||
Key3 = provider.MetaData["Key3"];
|
||||
}
|
||||
if(provider.MetaData.ContainsKey("Key4"))
|
||||
{
|
||||
|
Reference in New Issue
Block a user