2018-04-06 04:21:18 +02:00
|
|
|
import { Directive, EventEmitter, Input, OnInit, Output } from "@angular/core";
|
2022-03-24 17:19:19 +01:00
|
|
|
import { ActivatedRoute } from "@angular/router";
|
|
|
|
import { first } from "rxjs/operators";
|
2018-04-06 04:21:18 +02:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
|
|
import { PasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
|
|
|
import { UsernameGenerationService } from "@bitwarden/common/abstractions/usernameGeneration.service";
|
|
|
|
import { PasswordGeneratorPolicyOptions } from "@bitwarden/common/models/domain/passwordGeneratorPolicyOptions";
|
2020-02-26 23:38:11 +01:00
|
|
|
|
2020-08-17 18:14:40 +02:00
|
|
|
@Directive()
|
2022-04-01 00:26:59 +02:00
|
|
|
export class GeneratorComponent implements OnInit {
|
|
|
|
@Input() comingFromAddEdit = false;
|
|
|
|
@Input() type: string;
|
2018-04-06 04:21:18 +02:00
|
|
|
@Output() onSelected = new EventEmitter<string>();
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2022-04-27 16:08:46 +02:00
|
|
|
usernameGeneratingPromise: Promise<string>;
|
2022-03-24 17:19:19 +01:00
|
|
|
typeOptions: any[];
|
2021-07-02 03:33:11 +02:00
|
|
|
passTypeOptions: any[];
|
2022-03-24 17:19:19 +01:00
|
|
|
usernameTypeOptions: any[];
|
|
|
|
subaddressOptions: any[];
|
|
|
|
catchallOptions: any[];
|
|
|
|
forwardOptions: any[];
|
|
|
|
usernameOptions: any = {};
|
|
|
|
passwordOptions: any = {};
|
|
|
|
username = "-";
|
2022-02-22 15:39:11 +01:00
|
|
|
password = "-";
|
2018-04-06 04:21:18 +02:00
|
|
|
showOptions = false;
|
|
|
|
avoidAmbiguous = false;
|
2022-03-24 17:19:19 +01:00
|
|
|
enforcedPasswordPolicyOptions: PasswordGeneratorPolicyOptions;
|
2022-03-25 10:32:01 +01:00
|
|
|
usernameWebsite: string = null;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-10-03 06:03:49 +02:00
|
|
|
constructor(
|
|
|
|
protected passwordGenerationService: PasswordGenerationService,
|
2022-03-24 17:19:19 +01:00
|
|
|
protected usernameGenerationService: UsernameGenerationService,
|
2018-04-06 04:21:18 +02:00
|
|
|
protected platformUtilsService: PlatformUtilsService,
|
2022-03-24 17:19:19 +01:00
|
|
|
protected stateService: StateService,
|
2018-04-06 04:21:18 +02:00
|
|
|
protected i18nService: I18nService,
|
2022-04-27 16:08:46 +02:00
|
|
|
protected logService: LogService,
|
2022-03-24 17:19:19 +01:00
|
|
|
protected route: ActivatedRoute,
|
2021-07-02 03:33:11 +02:00
|
|
|
private win: Window
|
|
|
|
) {
|
2022-03-24 17:19:19 +01:00
|
|
|
this.typeOptions = [
|
|
|
|
{ name: i18nService.t("password"), value: "password" },
|
|
|
|
{ name: i18nService.t("username"), value: "username" },
|
|
|
|
];
|
2021-07-02 03:33:11 +02:00
|
|
|
this.passTypeOptions = [
|
|
|
|
{ name: i18nService.t("password"), value: "password" },
|
|
|
|
{ name: i18nService.t("passphrase"), value: "passphrase" },
|
|
|
|
];
|
2022-03-24 17:19:19 +01:00
|
|
|
this.usernameTypeOptions = [
|
|
|
|
{
|
|
|
|
name: i18nService.t("plusAddressedEmail"),
|
|
|
|
value: "subaddress",
|
|
|
|
desc: i18nService.t("plusAddressedEmailDesc"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: i18nService.t("catchallEmail"),
|
|
|
|
value: "catchall",
|
|
|
|
desc: i18nService.t("catchallEmailDesc"),
|
|
|
|
},
|
2022-04-27 16:08:46 +02:00
|
|
|
{
|
|
|
|
name: i18nService.t("forwardedEmail"),
|
|
|
|
value: "forwarded",
|
|
|
|
desc: i18nService.t("forwardedEmailDesc"),
|
|
|
|
},
|
2022-03-24 17:19:19 +01:00
|
|
|
{ name: i18nService.t("randomWord"), value: "word" },
|
|
|
|
];
|
|
|
|
this.subaddressOptions = [{ name: i18nService.t("random"), value: "random" }];
|
|
|
|
this.catchallOptions = [{ name: i18nService.t("random"), value: "random" }];
|
|
|
|
this.forwardOptions = [
|
|
|
|
{ name: "SimpleLogin", value: "simplelogin" },
|
2022-04-27 16:08:46 +02:00
|
|
|
{ name: "AnonAddy", value: "anonaddy" },
|
|
|
|
{ name: "Firefox Relay", value: "firefoxrelay" },
|
2022-08-19 15:45:02 +02:00
|
|
|
{ name: "DuckDuckGo", value: "duckduckgo" },
|
2022-04-27 16:08:46 +02:00
|
|
|
// { name: "FastMail", value: "fastmail" },
|
2022-03-24 17:19:19 +01:00
|
|
|
];
|
2021-07-02 03:33:11 +02:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
async ngOnInit() {
|
2022-03-24 17:19:19 +01:00
|
|
|
this.route.queryParams.pipe(first()).subscribe(async (qParams) => {
|
|
|
|
const passwordOptionsResponse = await this.passwordGenerationService.getOptions();
|
|
|
|
this.passwordOptions = passwordOptionsResponse[0];
|
|
|
|
this.enforcedPasswordPolicyOptions = passwordOptionsResponse[1];
|
|
|
|
this.avoidAmbiguous = !this.passwordOptions.ambiguous;
|
|
|
|
this.passwordOptions.type =
|
|
|
|
this.passwordOptions.type === "passphrase" ? "passphrase" : "password";
|
|
|
|
|
|
|
|
this.usernameOptions = await this.usernameGenerationService.getOptions();
|
|
|
|
if (this.usernameOptions.type == null) {
|
|
|
|
this.usernameOptions.type = "word";
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
this.usernameOptions.subaddressEmail == null ||
|
|
|
|
this.usernameOptions.subaddressEmail === ""
|
|
|
|
) {
|
|
|
|
this.usernameOptions.subaddressEmail = await this.stateService.getEmail();
|
|
|
|
}
|
2022-04-01 00:26:59 +02:00
|
|
|
if (this.usernameWebsite == null) {
|
2022-03-24 17:19:19 +01:00
|
|
|
this.usernameOptions.subaddressType = this.usernameOptions.catchallType = "random";
|
2022-04-01 00:26:59 +02:00
|
|
|
} else {
|
2022-03-25 10:32:01 +01:00
|
|
|
this.usernameOptions.website = this.usernameWebsite;
|
2022-04-01 00:26:59 +02:00
|
|
|
const websiteOption = { name: this.i18nService.t("websiteName"), value: "website-name" };
|
|
|
|
this.subaddressOptions.push(websiteOption);
|
|
|
|
this.catchallOptions.push(websiteOption);
|
2022-03-25 10:32:01 +01:00
|
|
|
}
|
2022-03-24 17:19:19 +01:00
|
|
|
|
2022-04-01 00:26:59 +02:00
|
|
|
if (this.type !== "username" && this.type !== "password") {
|
|
|
|
if (qParams.type === "username" || qParams.type === "password") {
|
|
|
|
this.type = qParams.type;
|
|
|
|
} else {
|
|
|
|
const generatorOptions = await this.stateService.getGeneratorOptions();
|
|
|
|
this.type = generatorOptions?.type ?? "password";
|
2022-03-24 17:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-27 16:08:46 +02:00
|
|
|
if (this.regenerateWithoutButtonPress()) {
|
|
|
|
await this.regenerate();
|
|
|
|
}
|
2022-03-24 17:19:19 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async typeChanged() {
|
|
|
|
await this.stateService.setGeneratorOptions({ type: this.type });
|
2022-04-27 16:08:46 +02:00
|
|
|
if (this.regenerateWithoutButtonPress()) {
|
|
|
|
await this.regenerate();
|
|
|
|
}
|
2022-03-24 17:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async regenerate() {
|
|
|
|
if (this.type === "password") {
|
|
|
|
await this.regeneratePassword();
|
|
|
|
} else if (this.type === "username") {
|
|
|
|
await this.regenerateUsername();
|
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
async sliderChanged() {
|
2022-03-24 17:19:19 +01:00
|
|
|
this.savePasswordOptions(false);
|
2018-04-06 04:21:18 +02:00
|
|
|
await this.passwordGenerationService.addHistory(this.password);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
async sliderInput() {
|
2022-03-24 17:19:19 +01:00
|
|
|
this.normalizePasswordOptions();
|
|
|
|
this.password = await this.passwordGenerationService.generatePassword(this.passwordOptions);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:19:19 +01:00
|
|
|
async savePasswordOptions(regenerate = true) {
|
|
|
|
this.normalizePasswordOptions();
|
|
|
|
await this.passwordGenerationService.saveOptions(this.passwordOptions);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2022-04-27 16:08:46 +02:00
|
|
|
if (regenerate && this.regenerateWithoutButtonPress()) {
|
2022-03-24 17:19:19 +01:00
|
|
|
await this.regeneratePassword();
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:19:19 +01:00
|
|
|
async saveUsernameOptions(regenerate = true) {
|
|
|
|
await this.usernameGenerationService.saveOptions(this.usernameOptions);
|
2022-04-27 16:08:46 +02:00
|
|
|
if (this.usernameOptions.type === "forwarded") {
|
|
|
|
this.username = "-";
|
|
|
|
}
|
|
|
|
if (regenerate && this.regenerateWithoutButtonPress()) {
|
2022-03-24 17:19:19 +01:00
|
|
|
await this.regenerateUsername();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async regeneratePassword() {
|
|
|
|
this.password = await this.passwordGenerationService.generatePassword(this.passwordOptions);
|
2018-04-06 04:21:18 +02:00
|
|
|
await this.passwordGenerationService.addHistory(this.password);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:19:19 +01:00
|
|
|
regenerateUsername() {
|
|
|
|
return this.generateUsername();
|
|
|
|
}
|
|
|
|
|
|
|
|
async generateUsername() {
|
2022-04-27 16:08:46 +02:00
|
|
|
try {
|
|
|
|
this.usernameGeneratingPromise = this.usernameGenerationService.generateUsername(
|
|
|
|
this.usernameOptions
|
|
|
|
);
|
|
|
|
this.username = await this.usernameGeneratingPromise;
|
|
|
|
if (this.username === "" || this.username === null) {
|
|
|
|
this.username = "-";
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2022-03-24 17:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
copy() {
|
2022-03-24 17:19:19 +01:00
|
|
|
const password = this.type === "password";
|
2018-04-06 04:21:18 +02:00
|
|
|
const copyOptions = this.win != null ? { window: this.win } : null;
|
2022-03-24 17:19:19 +01:00
|
|
|
this.platformUtilsService.copyToClipboard(
|
|
|
|
password ? this.password : this.username,
|
|
|
|
copyOptions
|
|
|
|
);
|
2018-04-06 04:21:18 +02:00
|
|
|
this.platformUtilsService.showToast(
|
2021-12-16 13:36:21 +01:00
|
|
|
"info",
|
|
|
|
null,
|
2022-03-24 17:19:19 +01:00
|
|
|
this.i18nService.t("valueCopied", this.i18nService.t(password ? "password" : "username"))
|
2021-12-16 13:36:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
select() {
|
2022-03-24 17:19:19 +01:00
|
|
|
this.onSelected.emit(this.type === "password" ? this.password : this.username);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
toggleOptions() {
|
|
|
|
this.showOptions = !this.showOptions;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2022-04-27 16:08:46 +02:00
|
|
|
regenerateWithoutButtonPress() {
|
|
|
|
return this.type !== "username" || this.usernameOptions.type !== "forwarded";
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:19:19 +01:00
|
|
|
private normalizePasswordOptions() {
|
2018-04-06 04:21:18 +02:00
|
|
|
// Application level normalize options depedent on class variables
|
2022-03-24 17:19:19 +01:00
|
|
|
this.passwordOptions.ambiguous = !this.avoidAmbiguous;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
|
|
|
if (
|
2022-03-24 17:19:19 +01:00
|
|
|
!this.passwordOptions.uppercase &&
|
|
|
|
!this.passwordOptions.lowercase &&
|
|
|
|
!this.passwordOptions.number &&
|
|
|
|
!this.passwordOptions.special
|
2021-12-16 13:36:21 +01:00
|
|
|
) {
|
2022-03-24 17:19:19 +01:00
|
|
|
this.passwordOptions.lowercase = true;
|
2018-04-06 04:21:18 +02:00
|
|
|
if (this.win != null) {
|
|
|
|
const lowercase = this.win.document.querySelector("#lowercase") as HTMLInputElement;
|
2018-10-09 04:06:06 +02:00
|
|
|
if (lowercase) {
|
|
|
|
lowercase.checked = true;
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:19:19 +01:00
|
|
|
this.passwordGenerationService.normalizeOptions(
|
|
|
|
this.passwordOptions,
|
|
|
|
this.enforcedPasswordPolicyOptions
|
|
|
|
);
|
2018-10-09 04:06:06 +02:00
|
|
|
}
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|