2018-04-06 04:21:18 +02:00
|
|
|
import {
|
|
|
|
EventEmitter,
|
|
|
|
Input,
|
|
|
|
OnInit,
|
|
|
|
Output,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { I18nService } from '../../abstractions/i18n.service';
|
|
|
|
import { PasswordGenerationService } from '../../abstractions/passwordGeneration.service';
|
|
|
|
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
|
|
|
|
2020-02-26 23:38:11 +01:00
|
|
|
import { PasswordGeneratorPolicyOptions } from '../../models/domain/passwordGeneratorPolicyOptions';
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
export class PasswordGeneratorComponent implements OnInit {
|
|
|
|
@Input() showSelect: boolean = false;
|
|
|
|
@Output() onSelected = new EventEmitter<string>();
|
|
|
|
|
|
|
|
options: any = {};
|
|
|
|
password: string = '-';
|
|
|
|
showOptions = false;
|
|
|
|
avoidAmbiguous = false;
|
2020-02-26 23:38:11 +01:00
|
|
|
enforcedPolicyOptions: PasswordGeneratorPolicyOptions;
|
2018-04-06 04:21:18 +02:00
|
|
|
|
2018-10-03 06:03:49 +02:00
|
|
|
constructor(protected passwordGenerationService: PasswordGenerationService,
|
2018-04-06 04:21:18 +02:00
|
|
|
protected platformUtilsService: PlatformUtilsService, protected i18nService: I18nService,
|
2018-10-03 05:09:19 +02:00
|
|
|
private win: Window) { }
|
2018-04-06 04:21:18 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
2020-02-26 23:38:11 +01:00
|
|
|
const optionsResponse = await this.passwordGenerationService.getOptions();
|
|
|
|
this.options = optionsResponse[0];
|
|
|
|
this.enforcedPolicyOptions = optionsResponse[1];
|
2018-04-06 04:21:18 +02:00
|
|
|
this.avoidAmbiguous = !this.options.ambiguous;
|
2018-10-09 04:06:06 +02:00
|
|
|
this.options.type = this.options.type === 'passphrase' ? 'passphrase' : 'password';
|
2018-04-23 19:03:47 +02:00
|
|
|
this.password = await this.passwordGenerationService.generatePassword(this.options);
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Generated Password');
|
2018-04-06 04:21:18 +02:00
|
|
|
await this.passwordGenerationService.addHistory(this.password);
|
|
|
|
}
|
|
|
|
|
|
|
|
async sliderChanged() {
|
|
|
|
this.saveOptions(false);
|
|
|
|
await this.passwordGenerationService.addHistory(this.password);
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Regenerated Password');
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async sliderInput() {
|
|
|
|
this.normalizeOptions();
|
2018-04-23 19:03:47 +02:00
|
|
|
this.password = await this.passwordGenerationService.generatePassword(this.options);
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async saveOptions(regenerate: boolean = true) {
|
|
|
|
this.normalizeOptions();
|
|
|
|
await this.passwordGenerationService.saveOptions(this.options);
|
|
|
|
|
|
|
|
if (regenerate) {
|
|
|
|
await this.regenerate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async regenerate() {
|
2018-04-23 19:03:47 +02:00
|
|
|
this.password = await this.passwordGenerationService.generatePassword(this.options);
|
2018-04-06 04:21:18 +02:00
|
|
|
await this.passwordGenerationService.addHistory(this.password);
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Regenerated Password');
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
copy() {
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Copied Generated Password');
|
2018-08-17 18:24:56 +02:00
|
|
|
const copyOptions = this.win != null ? { window: this.win } : null;
|
2018-04-19 14:00:54 +02:00
|
|
|
this.platformUtilsService.copyToClipboard(this.password, copyOptions);
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('info', null,
|
|
|
|
this.i18nService.t('valueCopied', this.i18nService.t('password')));
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
select() {
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Selected Generated Password');
|
2018-04-06 04:21:18 +02:00
|
|
|
this.onSelected.emit(this.password);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleOptions() {
|
|
|
|
this.showOptions = !this.showOptions;
|
|
|
|
}
|
|
|
|
|
2020-02-28 18:09:57 +01:00
|
|
|
hasPolicyInEffect() {
|
|
|
|
if (this.enforcedPolicyOptions == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.enforcedPolicyOptions.minLength > 0
|
|
|
|
|| this.enforcedPolicyOptions.numberCount > 0
|
|
|
|
|| this.enforcedPolicyOptions.specialCount > 0
|
|
|
|
|| this.enforcedPolicyOptions.useUppercase
|
|
|
|
|| this.enforcedPolicyOptions.useLowercase
|
|
|
|
|| this.enforcedPolicyOptions.useNumbers
|
|
|
|
|| this.enforcedPolicyOptions.useSpecial) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
private normalizeOptions() {
|
|
|
|
this.options.minLowercase = 0;
|
|
|
|
this.options.minUppercase = 0;
|
|
|
|
this.options.ambiguous = !this.avoidAmbiguous;
|
2018-10-08 23:26:13 +02:00
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
if (!this.options.uppercase && !this.options.lowercase && !this.options.number && !this.options.special) {
|
|
|
|
this.options.lowercase = true;
|
2018-10-09 04:06:06 +02:00
|
|
|
if (this.win != null) {
|
|
|
|
const lowercase = this.win.document.querySelector('#lowercase') as HTMLInputElement;
|
|
|
|
if (lowercase) {
|
|
|
|
lowercase.checked = true;
|
|
|
|
}
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 04:12:15 +02:00
|
|
|
if (!this.options.length || this.options.length < 5) {
|
2018-04-06 04:21:18 +02:00
|
|
|
this.options.length = 5;
|
|
|
|
} else if (this.options.length > 128) {
|
|
|
|
this.options.length = 128;
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:38:11 +01:00
|
|
|
if (this.options.length < this.enforcedPolicyOptions.minLength) {
|
|
|
|
this.options.length = this.enforcedPolicyOptions.minLength;
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
if (!this.options.minNumber) {
|
|
|
|
this.options.minNumber = 0;
|
|
|
|
} else if (this.options.minNumber > this.options.length) {
|
|
|
|
this.options.minNumber = this.options.length;
|
|
|
|
} else if (this.options.minNumber > 9) {
|
|
|
|
this.options.minNumber = 9;
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:38:11 +01:00
|
|
|
if (this.options.minNumber < this.enforcedPolicyOptions.numberCount) {
|
|
|
|
this.options.minNumber = this.enforcedPolicyOptions.numberCount;
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
if (!this.options.minSpecial) {
|
|
|
|
this.options.minSpecial = 0;
|
|
|
|
} else if (this.options.minSpecial > this.options.length) {
|
|
|
|
this.options.minSpecial = this.options.length;
|
|
|
|
} else if (this.options.minSpecial > 9) {
|
|
|
|
this.options.minSpecial = 9;
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:38:11 +01:00
|
|
|
if (this.options.minSpecial < this.enforcedPolicyOptions.specialCount) {
|
|
|
|
this.options.minSpecial = this.enforcedPolicyOptions.specialCount;
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
if (this.options.minSpecial + this.options.minNumber > this.options.length) {
|
|
|
|
this.options.minSpecial = this.options.length - this.options.minNumber;
|
|
|
|
}
|
2018-10-08 23:26:13 +02:00
|
|
|
|
2018-10-08 23:54:54 +02:00
|
|
|
if (this.options.numWords == null || this.options.length < 3) {
|
2018-10-08 23:26:13 +02:00
|
|
|
this.options.numWords = 3;
|
2018-10-08 23:54:54 +02:00
|
|
|
} else if (this.options.numWords > 20) {
|
|
|
|
this.options.numWords = 20;
|
2018-10-08 23:26:13 +02:00
|
|
|
}
|
2018-10-09 04:06:06 +02:00
|
|
|
|
|
|
|
if (this.options.wordSeparator != null && this.options.wordSeparator.length > 1) {
|
|
|
|
this.options.wordSeparator = this.options.wordSeparator[0];
|
|
|
|
}
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
}
|