mirror of
https://github.com/bitwarden/mobile
synced 2026-01-07 02:53:56 +00:00
Enforce Passphrase Policy (#772)
* Enforce passphrase policy * Update multi-line conditional formatting * Updated formatting round 2 Co-authored-by: Vincent Salucci <vsalucci@bitwarden.com>
This commit is contained in:
@@ -312,6 +312,27 @@ namespace Bit.Core.Services
|
||||
{
|
||||
options.MinSpecial = options.Length - options.MinNumber;
|
||||
}
|
||||
|
||||
if(options.NumWords < enforcedPolicyOptions.MinNumberOfWords)
|
||||
{
|
||||
options.NumWords = enforcedPolicyOptions.MinNumberOfWords;
|
||||
}
|
||||
|
||||
if(enforcedPolicyOptions.Capitalize)
|
||||
{
|
||||
options.Capitalize = true;
|
||||
}
|
||||
|
||||
if(enforcedPolicyOptions.IncludeNumber)
|
||||
{
|
||||
options.IncludeNumber = true;
|
||||
}
|
||||
|
||||
// Force default type if password/passphrase selected via policy
|
||||
if(enforcedPolicyOptions.DefaultType == "password" || enforcedPolicyOptions.DefaultType == "passphrase")
|
||||
{
|
||||
options.Type = enforcedPolicyOptions.DefaultType;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -344,6 +365,12 @@ namespace Bit.Core.Services
|
||||
enforcedOptions = new PasswordGeneratorPolicyOptions();
|
||||
}
|
||||
|
||||
var defaultType = GetPolicyString(currentPolicy, "defaultType");
|
||||
if(defaultType != null && enforcedOptions.DefaultType != "password")
|
||||
{
|
||||
enforcedOptions.DefaultType = defaultType;
|
||||
}
|
||||
|
||||
var minLength = GetPolicyInt(currentPolicy, "minLength");
|
||||
if(minLength != null && (int)(long)minLength > enforcedOptions.MinLength)
|
||||
{
|
||||
@@ -385,6 +412,24 @@ namespace Bit.Core.Services
|
||||
{
|
||||
enforcedOptions.SpecialCount = (int)(long)minSpecial;
|
||||
}
|
||||
|
||||
var minNumberWords = GetPolicyInt(currentPolicy, "minNumberWords");
|
||||
if(minNumberWords != null && (int)(long)minNumberWords > enforcedOptions.MinNumberOfWords)
|
||||
{
|
||||
enforcedOptions.MinNumberOfWords = (int)(long)minNumberWords;
|
||||
}
|
||||
|
||||
var capitalize = GetPolicyBool(currentPolicy, "capitalize");
|
||||
if(capitalize != null && (bool)capitalize)
|
||||
{
|
||||
enforcedOptions.Capitalize = true;
|
||||
}
|
||||
|
||||
var includeNumber = GetPolicyBool(currentPolicy, "includeNumber");
|
||||
if(includeNumber != null && (bool)includeNumber)
|
||||
{
|
||||
enforcedOptions.IncludeNumber = true;
|
||||
}
|
||||
}
|
||||
|
||||
return enforcedOptions;
|
||||
@@ -415,6 +460,19 @@ namespace Bit.Core.Services
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private string GetPolicyString(Policy policy, string key)
|
||||
{
|
||||
if(policy.Data.ContainsKey(key))
|
||||
{
|
||||
var value = policy.Data[key];
|
||||
if(value != null)
|
||||
{
|
||||
return (string)value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task SaveOptionsAsync(PasswordGenerationOptions options)
|
||||
{
|
||||
@@ -550,6 +608,11 @@ namespace Bit.Core.Services
|
||||
options.NumWords = 20;
|
||||
}
|
||||
|
||||
if(options.NumWords < enforcedPolicyOptions.MinNumberOfWords)
|
||||
{
|
||||
options.NumWords = enforcedPolicyOptions.MinNumberOfWords;
|
||||
}
|
||||
|
||||
if(options.WordSeparator != null && options.WordSeparator.Length > 1)
|
||||
{
|
||||
options.WordSeparator = options.WordSeparator[0].ToString();
|
||||
|
||||
Reference in New Issue
Block a user