bitwarden-estensione-browser/libs/angular/src/components/register.component.ts

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

277 lines
9.0 KiB
TypeScript
Raw Normal View History

2022-06-17 07:14:44 +02:00
import { Directive, ElementRef, OnInit, ViewChild } from "@angular/core";
import { FormBuilder, Validators } from "@angular/forms";
2018-04-04 15:47:43 +02:00
import { Router } from "@angular/router";
2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { AuthService } from "@bitwarden/common/abstractions/auth.service";
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { EnvironmentService } from "@bitwarden/common/abstractions/environment.service";
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 { DEFAULT_KDF_ITERATIONS, DEFAULT_KDF_TYPE } from "@bitwarden/common/enums/kdfType";
import { KeysRequest } from "@bitwarden/common/models/request/keysRequest";
import { ReferenceEventRequest } from "@bitwarden/common/models/request/referenceEventRequest";
import { RegisterRequest } from "@bitwarden/common/models/request/registerRequest";
2018-08-14 21:12:10 +02:00
import { CaptchaProtectedComponent } from "./captchaProtected.component";
@Directive()
export class RegisterComponent extends CaptchaProtectedComponent implements OnInit {
2022-06-17 07:14:44 +02:00
@ViewChild("masterPassword") masterPasswordRef: ElementRef;
@ViewChild("masterPasswordRetype") masterPasswordRetypeRef: ElementRef;
2022-02-22 15:39:11 +01:00
name = "";
email = "";
masterPassword = "";
confirmMasterPassword = "";
hint = "";
showPassword = false;
2018-04-04 15:47:43 +02:00
formPromise: Promise<any>;
2018-11-13 04:54:18 +01:00
masterPasswordScore: number;
2020-07-20 21:21:01 +02:00
referenceData: ReferenceEventRequest;
showTerms = true;
2022-02-22 15:39:11 +01:00
acceptPolicies = false;
2021-12-16 13:36:21 +01:00
2022-06-17 07:14:44 +02:00
formGroup = this.formBuilder.group({
email: ["", [Validators.required, Validators.email]],
name: [""],
masterPassword: ["", [Validators.required]],
confirmMasterPassword: ["", [Validators.required]],
hint: [],
acceptPolicies: [false, [Validators.requiredTrue]],
});
2018-04-04 15:47:43 +02:00
protected successRoute = "login";
2018-11-13 04:54:18 +01:00
private masterPasswordStrengthTimeout: any;
2021-12-16 13:36:21 +01:00
2018-04-04 15:47:43 +02:00
constructor(
2022-06-17 07:14:44 +02:00
protected formBuilder: FormBuilder,
2018-04-04 15:47:43 +02:00
protected authService: AuthService,
protected router: Router,
i18nService: I18nService,
protected cryptoService: CryptoService,
protected apiService: ApiService,
protected stateService: StateService,
platformUtilsService: PlatformUtilsService,
protected passwordGenerationService: PasswordGenerationService,
environmentService: EnvironmentService,
protected logService: LogService
) {
super(environmentService, i18nService, platformUtilsService);
this.showTerms = !platformUtilsService.isSelfHost();
2021-12-16 13:36:21 +01:00
}
async ngOnInit() {
this.setupCaptcha();
2021-12-16 13:36:21 +01:00
}
2018-11-15 21:27:04 +01:00
get masterPasswordScoreWidth() {
return this.masterPasswordScore == null ? 0 : (this.masterPasswordScore + 1) * 20;
2021-12-16 13:36:21 +01:00
}
2018-11-15 21:27:04 +01:00
get masterPasswordScoreColor() {
switch (this.masterPasswordScore) {
2021-12-16 13:36:21 +01:00
case 4:
2018-11-15 21:27:04 +01:00
return "success";
2021-12-16 13:36:21 +01:00
case 3:
2018-11-15 21:27:04 +01:00
return "primary";
2021-12-16 13:36:21 +01:00
case 2:
2018-11-15 21:27:04 +01:00
return "warning";
2021-12-16 13:36:21 +01:00
default:
2018-11-15 21:27:04 +01:00
return "danger";
}
2021-12-16 13:36:21 +01:00
}
get masterPasswordScoreText() {
switch (this.masterPasswordScore) {
2021-12-16 13:36:21 +01:00
case 4:
return this.i18nService.t("strong");
2021-12-16 13:36:21 +01:00
case 3:
return this.i18nService.t("good");
case 2:
return this.i18nService.t("weak");
2021-12-16 13:36:21 +01:00
default:
2018-11-15 21:27:04 +01:00
return this.masterPasswordScore != null ? this.i18nService.t("weak") : null;
}
2021-12-16 13:36:21 +01:00
}
2018-11-15 21:27:04 +01:00
async submit() {
2022-06-17 07:14:44 +02:00
let email = this.formGroup.get("email")?.value;
let name = this.formGroup.get("name")?.value;
const masterPassword = this.formGroup.get("masterPassword")?.value;
const confirmMasterPassword = this.formGroup.get("confirmMasterPassword")?.value;
const hint = this.formGroup.get("hint")?.value;
const acceptPolicies = this.formGroup.get("acceptPolicies")?.value;
this.formGroup.markAllAsTouched();
if (!this.formGroup.valid) {
return;
}
if (!acceptPolicies && this.showTerms) {
2018-11-15 21:27:04 +01:00
this.platformUtilsService.showToast(
"error",
this.i18nService.t("errorOccurred"),
this.i18nService.t("acceptPoliciesError")
2021-12-16 13:36:21 +01:00
);
2018-11-15 21:27:04 +01:00
return;
}
2022-06-17 07:14:44 +02:00
if (email == null || email === "") {
2018-11-15 21:27:04 +01:00
this.platformUtilsService.showToast(
"error",
this.i18nService.t("errorOccurred"),
this.i18nService.t("emailRequired")
);
return;
}
2022-06-17 07:14:44 +02:00
if (email.indexOf("@") === -1) {
2018-11-15 21:27:04 +01:00
this.platformUtilsService.showToast(
2021-12-16 13:36:21 +01:00
"error",
2018-11-15 21:27:04 +01:00
this.i18nService.t("errorOccurred"),
2018-04-04 15:47:43 +02:00
this.i18nService.t("invalidEmail")
2021-12-16 13:36:21 +01:00
);
2018-11-15 21:27:04 +01:00
return;
2021-12-16 13:36:21 +01:00
}
2022-06-17 07:14:44 +02:00
if (masterPassword == null || masterPassword === "") {
2018-11-15 21:27:04 +01:00
this.platformUtilsService.showToast(
2021-12-16 13:36:21 +01:00
"error",
2018-11-15 21:27:04 +01:00
this.i18nService.t("errorOccurred"),
this.i18nService.t("masterPassRequired")
2021-12-16 13:36:21 +01:00
);
return;
}
2022-06-17 07:14:44 +02:00
if (masterPassword.length < 8) {
this.platformUtilsService.showToast(
2021-12-16 13:36:21 +01:00
"error",
this.i18nService.t("errorOccurred"),
2018-04-04 15:47:43 +02:00
this.i18nService.t("masterPassLength")
2021-12-16 13:36:21 +01:00
);
2018-11-15 21:27:04 +01:00
return;
2021-12-16 13:36:21 +01:00
}
2022-06-17 07:14:44 +02:00
if (masterPassword !== confirmMasterPassword) {
this.platformUtilsService.showToast(
2021-12-16 13:36:21 +01:00
"error",
this.i18nService.t("errorOccurred"),
2018-04-04 15:47:43 +02:00
this.i18nService.t("masterPassDoesntMatch")
2021-12-16 13:36:21 +01:00
);
return;
2018-11-15 21:27:04 +01:00
}
const strengthResult = this.passwordGenerationService.passwordStrength(
2022-06-17 07:14:44 +02:00
masterPassword,
2018-10-03 05:09:19 +02:00
this.getPasswordStrengthUserInput()
2018-04-04 15:47:43 +02:00
);
if (strengthResult != null && strengthResult.score < 3) {
2018-10-03 05:09:19 +02:00
const result = await this.platformUtilsService.showDialog(
2018-11-13 04:54:18 +01:00
this.i18nService.t("weakMasterPasswordDesc"),
this.i18nService.t("weakMasterPassword"),
this.i18nService.t("yes"),
this.i18nService.t("no"),
"warning"
);
if (!result) {
return;
2021-12-16 13:36:21 +01:00
}
}
2018-07-03 17:41:55 +02:00
2022-06-17 07:14:44 +02:00
if (hint === masterPassword) {
2018-10-03 05:09:19 +02:00
this.platformUtilsService.showToast(
"error",
this.i18nService.t("errorOccurred"),
this.i18nService.t("hintEqualsPassword")
2018-07-13 16:44:47 +02:00
);
return;
2018-04-04 15:47:43 +02:00
}
2022-06-17 07:14:44 +02:00
name = name === "" ? null : name;
email = email.trim().toLowerCase();
2022-03-24 10:42:11 +01:00
const kdf = DEFAULT_KDF_TYPE;
const kdfIterations = DEFAULT_KDF_ITERATIONS;
2022-06-17 07:14:44 +02:00
const key = await this.cryptoService.makeKey(masterPassword, email, kdf, kdfIterations);
2018-04-04 15:47:43 +02:00
const encKey = await this.cryptoService.makeEncKey(key);
2022-06-17 07:14:44 +02:00
const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key);
2018-04-04 15:47:43 +02:00
const keys = await this.cryptoService.makeKeyPair(encKey[0]);
2018-07-03 17:41:55 +02:00
const request = new RegisterRequest(
2022-06-17 07:14:44 +02:00
email,
name,
2018-07-03 17:41:55 +02:00
hashedPassword,
2022-06-17 07:14:44 +02:00
hint,
encKey[1].encryptedString,
2021-12-16 13:36:21 +01:00
kdf,
kdfIterations,
this.referenceData,
this.captchaToken
2021-12-16 13:36:21 +01:00
);
2018-04-04 15:47:43 +02:00
request.keys = new KeysRequest(keys[0], keys[1].encryptedString);
const orgInvite = await this.stateService.getOrganizationInvitation();
if (orgInvite != null && orgInvite.token != null && orgInvite.organizationUserId != null) {
request.token = orgInvite.token;
request.organizationUserId = orgInvite.organizationUserId;
2018-04-04 15:47:43 +02:00
}
2018-11-13 04:54:18 +01:00
try {
this.formPromise = this.apiService.postRegister(request);
2021-12-16 13:36:21 +01:00
try {
2018-11-13 04:54:18 +01:00
await this.formPromise;
} catch (e) {
2018-11-13 04:54:18 +01:00
if (this.handleCaptchaRequired(e)) {
return;
2021-12-16 13:36:21 +01:00
} else {
2018-11-13 04:54:18 +01:00
throw e;
}
2021-12-16 13:36:21 +01:00
}
2018-11-13 05:22:37 +01:00
this.platformUtilsService.showToast("success", null, this.i18nService.t("newAccountCreated"));
2022-06-17 07:14:44 +02:00
this.router.navigate([this.successRoute], { queryParams: { email: email } });
2018-11-13 04:54:18 +01:00
} catch (e) {
this.logService.error(e);
}
2021-12-16 13:36:21 +01:00
}
2018-11-13 05:22:37 +01:00
2018-04-04 15:47:43 +02:00
togglePassword(confirmField: boolean) {
2018-07-03 17:41:55 +02:00
this.showPassword = !this.showPassword;
2022-06-17 07:14:44 +02:00
confirmField
? this.masterPasswordRetypeRef.nativeElement.focus()
: this.masterPasswordRef.nativeElement.focus();
// document.getElementById(confirmField ? "masterPasswordRetype" : "masterPassword").focus();
2021-12-16 13:36:21 +01:00
}
2018-11-13 04:54:18 +01:00
updatePasswordStrength() {
2022-06-17 07:14:44 +02:00
const masterPassword = this.formGroup.get("masterPassword")?.value;
2018-11-13 04:54:18 +01:00
if (this.masterPasswordStrengthTimeout != null) {
clearTimeout(this.masterPasswordStrengthTimeout);
2021-12-16 13:36:21 +01:00
}
2018-11-13 04:54:18 +01:00
this.masterPasswordStrengthTimeout = setTimeout(() => {
2018-11-13 05:22:37 +01:00
const strengthResult = this.passwordGenerationService.passwordStrength(
2022-06-17 07:14:44 +02:00
masterPassword,
2018-11-13 05:22:37 +01:00
this.getPasswordStrengthUserInput()
2021-12-16 13:36:21 +01:00
);
2018-11-13 04:54:18 +01:00
this.masterPasswordScore = strengthResult == null ? null : strengthResult.score;
2021-12-16 13:36:21 +01:00
}, 300);
}
2018-11-13 05:22:37 +01:00
private getPasswordStrengthUserInput() {
let userInput: string[] = [];
2022-06-17 07:14:44 +02:00
const email = this.formGroup.get("email")?.value;
const name = this.formGroup.get("name").value;
const atPosition = email.indexOf("@");
2018-11-13 05:22:37 +01:00
if (atPosition > -1) {
userInput = userInput.concat(
2022-06-17 07:14:44 +02:00
email
2018-11-13 05:22:37 +01:00
.substr(0, atPosition)
.trim()
.toLowerCase()
.split(/[^A-Za-z0-9]/)
);
}
2022-06-17 07:14:44 +02:00
if (name != null && name !== "") {
userInput = userInput.concat(name.trim().toLowerCase().split(" "));
2018-11-13 05:22:37 +01:00
}
return userInput;
2021-12-16 13:36:21 +01:00
}
2018-04-04 15:47:43 +02:00
}