Files
clients/angular/src/components/password-generator.component.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

106 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-04-05 22:21:18 -04:00
import { Directive, EventEmitter, Input, OnInit, Output } from "@angular/core";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { PasswordGenerationService } from "jslib-common/abstractions/passwordGeneration.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { PasswordGeneratorPolicyOptions } from "jslib-common/models/domain/passwordGeneratorPolicyOptions";
@Directive()
2018-04-05 22:21:18 -04:00
export class PasswordGeneratorComponent implements OnInit {
2022-02-22 15:39:11 +01:00
@Input() showSelect = false;
2018-04-05 22:21:18 -04:00
@Output() onSelected = new EventEmitter<string>();
2021-12-16 13:36:21 +01:00
2021-07-02 07:03:11 +05:30
passTypeOptions: any[];
2018-04-05 22:21:18 -04:00
options: any = {};
2022-02-22 15:39:11 +01:00
password = "-";
2018-04-05 22:21:18 -04:00
showOptions = false;
avoidAmbiguous = false;
enforcedPolicyOptions: PasswordGeneratorPolicyOptions;
2021-12-16 13:36:21 +01:00
constructor(
protected passwordGenerationService: PasswordGenerationService,
2018-04-05 22:21:18 -04:00
protected platformUtilsService: PlatformUtilsService,
protected i18nService: I18nService,
2021-07-02 07:03:11 +05:30
private win: Window
) {
this.passTypeOptions = [
{ name: i18nService.t("password"), value: "password" },
{ name: i18nService.t("passphrase"), value: "passphrase" },
];
}
2021-12-16 13:36:21 +01:00
2018-04-05 22:21:18 -04:00
async ngOnInit() {
const optionsResponse = await this.passwordGenerationService.getOptions();
this.options = optionsResponse[0];
this.enforcedPolicyOptions = optionsResponse[1];
2018-04-05 22:21:18 -04:00
this.avoidAmbiguous = !this.options.ambiguous;
2018-10-08 22:06:06 -04:00
this.options.type = this.options.type === "passphrase" ? "passphrase" : "password";
2018-04-23 13:03:47 -04:00
this.password = await this.passwordGenerationService.generatePassword(this.options);
2018-04-05 22:21:18 -04:00
await this.passwordGenerationService.addHistory(this.password);
2021-12-16 13:36:21 +01:00
}
2018-04-05 22:21:18 -04:00
async sliderChanged() {
this.saveOptions(false);
await this.passwordGenerationService.addHistory(this.password);
2021-12-16 13:36:21 +01:00
}
2018-04-05 22:21:18 -04:00
async sliderInput() {
this.normalizeOptions();
2018-04-23 13:03:47 -04:00
this.password = await this.passwordGenerationService.generatePassword(this.options);
2021-12-16 13:36:21 +01:00
}
2022-02-22 15:39:11 +01:00
async saveOptions(regenerate = true) {
2018-04-05 22:21:18 -04:00
this.normalizeOptions();
await this.passwordGenerationService.saveOptions(this.options);
2021-12-16 13:36:21 +01:00
2018-04-05 22:21:18 -04:00
if (regenerate) {
await this.regenerate();
}
2021-12-16 13:36:21 +01:00
}
2018-04-05 22:21:18 -04:00
async regenerate() {
2018-04-23 13:03:47 -04:00
this.password = await this.passwordGenerationService.generatePassword(this.options);
2018-04-05 22:21:18 -04:00
await this.passwordGenerationService.addHistory(this.password);
2021-12-16 13:36:21 +01:00
}
2018-04-05 22:21:18 -04:00
copy() {
const copyOptions = this.win != null ? { window: this.win } : null;
this.platformUtilsService.copyToClipboard(this.password, copyOptions);
this.platformUtilsService.showToast(
2021-12-16 13:36:21 +01:00
"info",
null,
2018-10-02 23:09:19 -04:00
this.i18nService.t("valueCopied", this.i18nService.t("password"))
2021-12-16 13:36:21 +01:00
);
}
2018-04-05 22:21:18 -04:00
select() {
this.onSelected.emit(this.password);
2021-12-16 13:36:21 +01:00
}
2018-04-05 22:21:18 -04:00
toggleOptions() {
this.showOptions = !this.showOptions;
2021-12-16 13:36:21 +01:00
}
2018-04-05 22:21:18 -04:00
private normalizeOptions() {
// Application level normalize options depedent on class variables
this.options.ambiguous = !this.avoidAmbiguous;
2021-12-16 13:36:21 +01:00
if (
2018-04-05 22:21:18 -04:00
!this.options.uppercase &&
!this.options.lowercase &&
!this.options.number &&
!this.options.special
2021-12-16 13:36:21 +01:00
) {
2018-04-05 22:21:18 -04:00
this.options.lowercase = true;
if (this.win != null) {
const lowercase = this.win.document.querySelector("#lowercase") as HTMLInputElement;
2018-10-08 22:06:06 -04:00
if (lowercase) {
lowercase.checked = true;
2018-04-05 22:21:18 -04:00
}
2021-12-16 13:36:21 +01:00
}
2018-04-05 22:21:18 -04:00
}
2018-04-23 13:03:47 -04:00
this.passwordGenerationService.normalizeOptions(this.options, this.enforcedPolicyOptions);
2018-10-08 22:06:06 -04:00
}
2018-04-05 22:21:18 -04:00
}