enforce policies when options already known (#83)

This commit is contained in:
Kyle Spearrin 2020-03-03 16:03:26 -05:00 committed by GitHub
parent da9b9b438c
commit 44b86f5dd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 15 deletions

View File

@ -5,6 +5,7 @@ export abstract class PasswordGenerationService {
generatePassword: (options: any) => Promise<string>; generatePassword: (options: any) => Promise<string>;
generatePassphrase: (options: any) => Promise<string>; generatePassphrase: (options: any) => Promise<string>;
getOptions: () => Promise<[any, PasswordGeneratorPolicyOptions]>; getOptions: () => Promise<[any, PasswordGeneratorPolicyOptions]>;
enforcePasswordGeneratorPoliciesOnOptions: (options: any) => Promise<[any, PasswordGeneratorPolicyOptions]>;
getPasswordGeneratorPolicyOptions: () => Promise<PasswordGeneratorPolicyOptions>; getPasswordGeneratorPolicyOptions: () => Promise<PasswordGeneratorPolicyOptions>;
saveOptions: (options: any) => Promise<any>; saveOptions: (options: any) => Promise<any>;
getHistory: () => Promise<GeneratedPasswordHistory[]>; getHistory: () => Promise<GeneratedPasswordHistory[]>;

View File

@ -222,47 +222,50 @@ export class PasswordGenerationService implements PasswordGenerationServiceAbstr
this.optionsCache = Object.assign({}, DefaultOptions, options); this.optionsCache = Object.assign({}, DefaultOptions, options);
} }
} }
const enforcedOptions = await this.enforcePasswordGeneratorPoliciesOnOptions(this.optionsCache);
this.optionsCache = enforcedOptions[0];
return [this.optionsCache, enforcedOptions[1]];
}
async enforcePasswordGeneratorPoliciesOnOptions(options: any): Promise<[any, PasswordGeneratorPolicyOptions]> {
let enforcedPolicyOptions = await this.getPasswordGeneratorPolicyOptions(); let enforcedPolicyOptions = await this.getPasswordGeneratorPolicyOptions();
if (enforcedPolicyOptions != null) { if (enforcedPolicyOptions != null) {
if (this.optionsCache.length < enforcedPolicyOptions.minLength) { if (options.length < enforcedPolicyOptions.minLength) {
this.optionsCache.length = enforcedPolicyOptions.minLength; options.length = enforcedPolicyOptions.minLength;
} }
if (enforcedPolicyOptions.useUppercase) { if (enforcedPolicyOptions.useUppercase) {
this.optionsCache.uppercase = true; options.uppercase = true;
} }
if (enforcedPolicyOptions.useLowercase) { if (enforcedPolicyOptions.useLowercase) {
this.optionsCache.lowercase = true; options.lowercase = true;
} }
if (enforcedPolicyOptions.useNumbers) { if (enforcedPolicyOptions.useNumbers) {
this.optionsCache.number = true; options.number = true;
} }
if (this.optionsCache.minNumber < enforcedPolicyOptions.numberCount) { if (options.minNumber < enforcedPolicyOptions.numberCount) {
this.optionsCache.minNumber = enforcedPolicyOptions.numberCount; options.minNumber = enforcedPolicyOptions.numberCount;
} }
if (enforcedPolicyOptions.useSpecial) { if (enforcedPolicyOptions.useSpecial) {
this.optionsCache.special = true; options.special = true;
} }
if (this.optionsCache.minSpecial < enforcedPolicyOptions.specialCount) { if (options.minSpecial < enforcedPolicyOptions.specialCount) {
this.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 (this.optionsCache.minSpecial + this.optionsCache.minNumber > this.optionsCache.length) { if (options.minSpecial + options.minNumber > options.length) {
this.optionsCache.minSpecial = this.optionsCache.length - this.optionsCache.minNumber; options.minSpecial = options.length - options.minNumber;
} }
} else { // UI layer expects an instantiated object to prevent more explicit null checks } else { // UI layer expects an instantiated object to prevent more explicit null checks
enforcedPolicyOptions = new PasswordGeneratorPolicyOptions(); enforcedPolicyOptions = new PasswordGeneratorPolicyOptions();
} }
return [options, enforcedPolicyOptions];
return [this.optionsCache, enforcedPolicyOptions];
} }
async getPasswordGeneratorPolicyOptions(): Promise<PasswordGeneratorPolicyOptions> { async getPasswordGeneratorPolicyOptions(): Promise<PasswordGeneratorPolicyOptions> {