Allows us to pass in some options to have policies enforced upon. (#755)

This commit is contained in:
Kyle Spearrin 2020-03-05 10:11:54 -05:00 committed by GitHub
parent d1a1342587
commit b9c134654f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 16 deletions

View file

@ -13,6 +13,8 @@ namespace Bit.Core.Abstractions
Task<string> GeneratePasswordAsync(PasswordGenerationOptions options); Task<string> GeneratePasswordAsync(PasswordGenerationOptions options);
Task<List<GeneratedPasswordHistory>> GetHistoryAsync(); Task<List<GeneratedPasswordHistory>> GetHistoryAsync();
Task<(PasswordGenerationOptions, PasswordGeneratorPolicyOptions)> GetOptionsAsync(); Task<(PasswordGenerationOptions, PasswordGeneratorPolicyOptions)> GetOptionsAsync();
Task<(PasswordGenerationOptions, PasswordGeneratorPolicyOptions)>
EnforcePasswordGeneratorPoliciesOnOptionsAsync(PasswordGenerationOptions options);
Task<object> PasswordStrength(string password, List<string> userInputs = null); Task<object> PasswordStrength(string password, List<string> userInputs = null);
Task SaveOptionsAsync(PasswordGenerationOptions options); Task SaveOptionsAsync(PasswordGenerationOptions options);
void NormalizeOptions(PasswordGenerationOptions options, PasswordGeneratorPolicyOptions enforcedPolicyOptions); void NormalizeOptions(PasswordGenerationOptions options, PasswordGeneratorPolicyOptions enforcedPolicyOptions);

View file

@ -244,7 +244,7 @@ namespace Bit.Core.Services
return string.Join(options.WordSeparator, wordList); return string.Join(options.WordSeparator, wordList);
} }
public async Task<(PasswordGenerationOptions,PasswordGeneratorPolicyOptions)> GetOptionsAsync() public async Task<(PasswordGenerationOptions, PasswordGeneratorPolicyOptions)> GetOptionsAsync()
{ {
if(_optionsCache == null) if(_optionsCache == null)
{ {
@ -260,48 +260,57 @@ namespace Bit.Core.Services
} }
} }
var (enforcedOptions, enforcedPolicyOptions) = await EnforcePasswordGeneratorPoliciesOnOptionsAsync(
_optionsCache);
_optionsCache = enforcedOptions;
return (_optionsCache, enforcedPolicyOptions);
}
public async Task<(PasswordGenerationOptions, PasswordGeneratorPolicyOptions)>
EnforcePasswordGeneratorPoliciesOnOptionsAsync(PasswordGenerationOptions options)
{
var enforcedPolicyOptions = await GetPasswordGeneratorPolicyOptions(); var enforcedPolicyOptions = await GetPasswordGeneratorPolicyOptions();
if(enforcedPolicyOptions != null) if(enforcedPolicyOptions != null)
{ {
if(_optionsCache.Length < enforcedPolicyOptions.MinLength) if(options.Length < enforcedPolicyOptions.MinLength)
{ {
_optionsCache.Length = enforcedPolicyOptions.MinLength; options.Length = enforcedPolicyOptions.MinLength;
} }
if(enforcedPolicyOptions.UseUppercase) if(enforcedPolicyOptions.UseUppercase)
{ {
_optionsCache.Uppercase = true; options.Uppercase = true;
} }
if(enforcedPolicyOptions.UseLowercase) if(enforcedPolicyOptions.UseLowercase)
{ {
_optionsCache.Lowercase = true; options.Lowercase = true;
} }
if(enforcedPolicyOptions.UseNumbers) if(enforcedPolicyOptions.UseNumbers)
{ {
_optionsCache.Number = true; options.Number = true;
} }
if(_optionsCache.MinNumber < enforcedPolicyOptions.NumberCount) if(options.MinNumber < enforcedPolicyOptions.NumberCount)
{ {
_optionsCache.MinNumber = enforcedPolicyOptions.NumberCount; options.MinNumber = enforcedPolicyOptions.NumberCount;
} }
if(enforcedPolicyOptions.UseSpecial) if(enforcedPolicyOptions.UseSpecial)
{ {
_optionsCache.Special = true; options.Special = true;
} }
if(_optionsCache.MinSpecial < enforcedPolicyOptions.SpecialCount) if(options.MinSpecial < enforcedPolicyOptions.SpecialCount)
{ {
_optionsCache.MinSpecial = enforcedPolicyOptions.SpecialCount; options.MinSpecial = enforcedPolicyOptions.SpecialCount;
} }
// Must normalize these fields because the receiving call expects all options to pass the current rules // Must normalize these fields because the receiving call expects all options to pass the current rules
if(_optionsCache.MinSpecial + _optionsCache.MinNumber > _optionsCache.Length) if(options.MinSpecial + options.MinNumber > options.Length)
{ {
_optionsCache.MinSpecial = _optionsCache.Length - _optionsCache.MinNumber; options.MinSpecial = options.Length - options.MinNumber;
} }
} }
else else
@ -310,7 +319,7 @@ namespace Bit.Core.Services
enforcedPolicyOptions = new PasswordGeneratorPolicyOptions(); enforcedPolicyOptions = new PasswordGeneratorPolicyOptions();
} }
return (_optionsCache, enforcedPolicyOptions); return (options, enforcedPolicyOptions);
} }
public async Task<PasswordGeneratorPolicyOptions> GetPasswordGeneratorPolicyOptions() public async Task<PasswordGeneratorPolicyOptions> GetPasswordGeneratorPolicyOptions()
@ -441,7 +450,7 @@ namespace Bit.Core.Services
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void NormalizeOptions(PasswordGenerationOptions options, public void NormalizeOptions(PasswordGenerationOptions options,
PasswordGeneratorPolicyOptions enforcedPolicyOptions) PasswordGeneratorPolicyOptions enforcedPolicyOptions)
{ {
options.MinLowercase = 0; options.MinLowercase = 0;
@ -480,7 +489,7 @@ namespace Bit.Core.Services
{ {
options.MinNumber = 9; options.MinNumber = 9;
} }
if(options.MinNumber < enforcedPolicyOptions.NumberCount) if(options.MinNumber < enforcedPolicyOptions.NumberCount)
{ {
options.MinNumber = enforcedPolicyOptions.NumberCount; options.MinNumber = enforcedPolicyOptions.NumberCount;