EnforcePasswordGeneratorPoliciesOnOptionsAsync(PasswordGenerationOptions options) { var enforcedPolicyOptions = await GetPasswordGeneratorPolicyOptions(); if (enforcedPolicyOptions != null) { if (options.Length < enforcedPolicyOptions.MinLength) { options.Length = enforcedPolicyOptions.MinLength; } if (enforcedPolicyOptions.UseUppercase) { options.Uppercase = true; } if (enforcedPolicyOptions.UseLowercase) { options.Lowercase = true; } if (enforcedPolicyOptions.UseNumbers) { options.Number = true; } if (options.MinNumber < enforcedPolicyOptions.NumberCount) { options.MinNumber = enforcedPolicyOptions.NumberCount; } if (enforcedPolicyOptions.UseSpecial) { options.Special = true; } if (options.MinSpecial < enforcedPolicyOptions.SpecialCount) { options.MinSpecial = enforcedPolicyOptions.SpecialCount; } // Must normalize these fields because the receiving call expects all options to pass the current rules if (options.MinSpecial + options.MinNumber > options.Length) { options.MinSpecial = options.Length - options.MinNumber; } } else { // UI layer expects an instantiated object to prevent more explicit null checks enforcedPolicyOptions = new PasswordGeneratorPolicyOptions(); } return(options, enforcedPolicyOptions); }
public void NormalizeOptions(PasswordGenerationOptions options, PasswordGeneratorPolicyOptions enforcedPolicyOptions) { options.MinLowercase = 0; options.MinUppercase = 0; if (!options.Uppercase.GetValueOrDefault() && !options.Lowercase.GetValueOrDefault() && !options.Number.GetValueOrDefault() && !options.Special.GetValueOrDefault()) { options.Lowercase = true; } var length = options.Length.GetValueOrDefault(); if (length < 5) { options.Length = 5; } else if (length > 128) { options.Length = 128; } if (options.Length < enforcedPolicyOptions.MinLength) { options.Length = enforcedPolicyOptions.MinLength; } if (options.MinNumber == null) { options.MinNumber = 0; } else if (options.MinNumber > options.Length) { options.MinNumber = options.Length; } else if (options.MinNumber > 9) { options.MinNumber = 9; } if (options.MinNumber < enforcedPolicyOptions.NumberCount) { options.MinNumber = enforcedPolicyOptions.NumberCount; } if (options.MinSpecial == null) { options.MinSpecial = 0; } else if (options.MinSpecial > options.Length) { options.MinSpecial = options.Length; } else if (options.MinSpecial > 9) { options.MinSpecial = 9; } if (options.MinSpecial < enforcedPolicyOptions.SpecialCount) { options.MinSpecial = enforcedPolicyOptions.SpecialCount; } if (options.MinSpecial + options.MinNumber > options.Length) { options.MinSpecial = options.Length - options.MinNumber; } if (options.NumWords == null || options.Length < 3) { options.NumWords = 3; } else if (options.NumWords > 20) { 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(); } SanitizePasswordLength(options, false); }
public async Task <PasswordGeneratorPolicyOptions> GetPasswordGeneratorPolicyOptions() { var policies = await _policyService.GetAll(PolicyType.PasswordGenerator); PasswordGeneratorPolicyOptions enforcedOptions = null; if (policies == null || !policies.Any()) { return(enforcedOptions); } foreach (var currentPolicy in policies) { if (!currentPolicy.Enabled || currentPolicy.Data == null) { continue; } if (enforcedOptions == null) { 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) { enforcedOptions.MinLength = (int)(long)minLength; } var useUpper = GetPolicyBool(currentPolicy, "useUpper"); if (useUpper != null && (bool)useUpper) { enforcedOptions.UseUppercase = true; } var useLower = GetPolicyBool(currentPolicy, "useLower"); if (useLower != null && (bool)useLower) { enforcedOptions.UseLowercase = true; } var useNumbers = GetPolicyBool(currentPolicy, "useNumbers"); if (useNumbers != null && (bool)useNumbers) { enforcedOptions.UseNumbers = true; } var minNumbers = GetPolicyInt(currentPolicy, "minNumbers"); if (minNumbers != null && (int)(long)minNumbers > enforcedOptions.NumberCount) { enforcedOptions.NumberCount = (int)(long)minNumbers; } var useSpecial = GetPolicyBool(currentPolicy, "useSpecial"); if (useSpecial != null && (bool)useSpecial) { enforcedOptions.UseSpecial = true; } var minSpecial = GetPolicyInt(currentPolicy, "minSpecial"); if (minSpecial != null && (int)(long)minSpecial > enforcedOptions.SpecialCount) { 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); }
EnforcePasswordGeneratorPoliciesOnOptionsAsync(PasswordGenerationOptions options) { var enforcedPolicyOptions = await GetPasswordGeneratorPolicyOptions(); if (enforcedPolicyOptions != null) { if (options.Length < enforcedPolicyOptions.MinLength) { options.Length = enforcedPolicyOptions.MinLength; } if (enforcedPolicyOptions.UseUppercase) { options.Uppercase = true; } if (enforcedPolicyOptions.UseLowercase) { options.Lowercase = true; } if (enforcedPolicyOptions.UseNumbers) { options.Number = true; } if (options.MinNumber < enforcedPolicyOptions.NumberCount) { options.MinNumber = enforcedPolicyOptions.NumberCount; } if (enforcedPolicyOptions.UseSpecial) { options.Special = true; } if (options.MinSpecial < enforcedPolicyOptions.SpecialCount) { options.MinSpecial = enforcedPolicyOptions.SpecialCount; } // Must normalize these fields because the receiving call expects all options to pass the current rules if (options.MinSpecial + options.MinNumber > options.Length) { 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 { // UI layer expects an instantiated object to prevent more explicit null checks enforcedPolicyOptions = new PasswordGeneratorPolicyOptions(); } return(options, enforcedPolicyOptions); }
public async Task <PasswordGeneratorPolicyOptions> GetPasswordGeneratorPolicyOptions() { var policies = await _policyService.GetAll(PolicyType.PasswordGenerator); PasswordGeneratorPolicyOptions enforcedOptions = null; if (policies == null || !policies.Any()) { return(enforcedOptions); } foreach (var currentPolicy in policies) { if (!currentPolicy.Enabled || currentPolicy.Data == null) { continue; } if (enforcedOptions == null) { enforcedOptions = new PasswordGeneratorPolicyOptions(); } var currentPolicyMinLength = currentPolicy.Data["minLength"]; if (currentPolicyMinLength != null && (int)(long)currentPolicyMinLength > enforcedOptions.MinLength) { enforcedOptions.MinLength = (int)(long)currentPolicyMinLength; } var currentPolicyUseUpper = currentPolicy.Data["useUpper"]; if (currentPolicyUseUpper != null && (bool)currentPolicyUseUpper) { enforcedOptions.UseUppercase = true; } var currentPolicyUseLower = currentPolicy.Data["useLower"]; if (currentPolicyUseLower != null && (bool)currentPolicyUseLower) { enforcedOptions.UseLowercase = true; } var currentPolicyUseNumbers = currentPolicy.Data["useNumbers"]; if (currentPolicyUseNumbers != null && (bool)currentPolicyUseNumbers) { enforcedOptions.UseNumbers = true; } var currentPolicyMinNumbers = currentPolicy.Data["minNumbers"]; if (currentPolicyMinNumbers != null && (int)(long)currentPolicyMinNumbers > enforcedOptions.NumberCount) { enforcedOptions.NumberCount = (int)(long)currentPolicyMinNumbers; } var currentPolicyUseSpecial = currentPolicy.Data["useSpecial"]; if (currentPolicyUseSpecial != null && (bool)currentPolicyUseSpecial) { enforcedOptions.UseSpecial = true; } var currentPolicyMinSpecial = currentPolicy.Data["minSpecial"]; if (currentPolicyMinSpecial != null && (int)(long)currentPolicyMinSpecial > enforcedOptions.SpecialCount) { enforcedOptions.SpecialCount = (int)(long)currentPolicyMinSpecial; } } return(enforcedOptions); }
public async Task <(PasswordGenerationOptions, PasswordGeneratorPolicyOptions)> GetOptionsAsync() { if (_optionsCache == null) { var options = await _storageService.GetAsync <PasswordGenerationOptions>(Keys_Options); if (options == null) { _optionsCache = _defaultOptions; } else { options.Merge(_defaultOptions); _optionsCache = options; } } var enforcedPolicyOptions = await GetPasswordGeneratorPolicyOptions(); if (enforcedPolicyOptions != null) { if (_optionsCache.Length < enforcedPolicyOptions.MinLength) { _optionsCache.Length = enforcedPolicyOptions.MinLength; } if (enforcedPolicyOptions.UseUppercase) { _optionsCache.Uppercase = true; } if (enforcedPolicyOptions.UseLowercase) { _optionsCache.Lowercase = true; } if (enforcedPolicyOptions.UseNumbers) { _optionsCache.Number = true; } if (_optionsCache.MinNumber < enforcedPolicyOptions.NumberCount) { _optionsCache.MinNumber = enforcedPolicyOptions.NumberCount; } if (enforcedPolicyOptions.UseSpecial) { _optionsCache.Special = true; } if (_optionsCache.MinSpecial < enforcedPolicyOptions.SpecialCount) { _optionsCache.MinSpecial = enforcedPolicyOptions.SpecialCount; } // Must normalize these fields because the receiving call expects all options to pass the current rules if (_optionsCache.MinSpecial + _optionsCache.MinNumber > _optionsCache.Length) { _optionsCache.MinSpecial = _optionsCache.Length - _optionsCache.MinNumber; } } else { // UI layer expects an instantiated object to prevent more explicit null checks enforcedPolicyOptions = new PasswordGeneratorPolicyOptions(); } return(_optionsCache, enforcedPolicyOptions); }