2021-06-02 18:35:49 +02:00
|
|
|
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.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 { PolicyService } from "@bitwarden/common/abstractions/policy.service";
|
|
|
|
import { EncString } from "@bitwarden/common/models/domain/encString";
|
|
|
|
import { MasterPasswordPolicyOptions } from "@bitwarden/common/models/domain/masterPasswordPolicyOptions";
|
|
|
|
import { SymmetricCryptoKey } from "@bitwarden/common/models/domain/symmetricCryptoKey";
|
|
|
|
import { OrganizationUserResetPasswordRequest } from "@bitwarden/common/models/request/organizationUserResetPasswordRequest";
|
2021-06-02 18:35:49 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-reset-password",
|
|
|
|
templateUrl: "reset-password.component.html",
|
|
|
|
})
|
|
|
|
export class ResetPasswordComponent implements OnInit {
|
|
|
|
@Input() name: string;
|
|
|
|
@Input() email: string;
|
|
|
|
@Input() id: string;
|
|
|
|
@Input() organizationId: string;
|
|
|
|
@Output() onPasswordReset = new EventEmitter();
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
enforcedPolicyOptions: MasterPasswordPolicyOptions;
|
|
|
|
newPassword: string = null;
|
2022-02-24 12:10:07 +01:00
|
|
|
showPassword = false;
|
2021-06-02 18:35:49 +02:00
|
|
|
masterPasswordScore: number;
|
|
|
|
formPromise: Promise<any>;
|
|
|
|
private newPasswordStrengthTimeout: any;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
constructor(
|
|
|
|
private apiService: ApiService,
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private passwordGenerationService: PasswordGenerationService,
|
2021-10-20 18:30:04 +02:00
|
|
|
private policyService: PolicyService,
|
|
|
|
private cryptoService: CryptoService,
|
|
|
|
private logService: LogService
|
|
|
|
) {}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
async ngOnInit() {
|
|
|
|
// Get Enforced Policy Options
|
|
|
|
this.enforcedPolicyOptions = await this.policyService.getMasterPasswordPolicyOptions();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
get loggedOutWarningName() {
|
|
|
|
return this.name != null ? this.name : this.i18nService.t("thisUser");
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
async generatePassword() {
|
|
|
|
const options = (await this.passwordGenerationService.getOptions())[0];
|
|
|
|
this.newPassword = await this.passwordGenerationService.generatePassword(options);
|
|
|
|
this.updatePasswordStrength();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
togglePassword() {
|
|
|
|
this.showPassword = !this.showPassword;
|
|
|
|
document.getElementById("newPassword").focus();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
copy(value: string) {
|
|
|
|
if (value == null) {
|
2021-12-17 15:57:11 +01:00
|
|
|
return;
|
2021-06-02 18:35:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.platformUtilsService.copyToClipboard(value, { window: window });
|
|
|
|
this.platformUtilsService.showToast(
|
2021-12-17 15:57:11 +01:00
|
|
|
"info",
|
2021-06-02 18:35:49 +02:00
|
|
|
null,
|
|
|
|
this.i18nService.t("valueCopied", this.i18nService.t("password"))
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
async submit() {
|
|
|
|
// Validation
|
|
|
|
if (this.newPassword == null || this.newPassword === "") {
|
|
|
|
this.platformUtilsService.showToast(
|
2021-12-17 15:57:11 +01:00
|
|
|
"error",
|
2021-06-02 18:35:49 +02:00
|
|
|
this.i18nService.t("errorOccurred"),
|
|
|
|
this.i18nService.t("masterPassRequired")
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
2021-06-02 18:35:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.newPassword.length < 8) {
|
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"error",
|
|
|
|
this.i18nService.t("errorOccurred"),
|
|
|
|
this.i18nService.t("masterPassLength")
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
if (
|
2021-06-02 18:35:49 +02:00
|
|
|
this.enforcedPolicyOptions != null &&
|
|
|
|
!this.policyService.evaluateMasterPassword(
|
|
|
|
this.masterPasswordScore,
|
|
|
|
this.newPassword,
|
|
|
|
this.enforcedPolicyOptions
|
2021-12-17 15:57:11 +01:00
|
|
|
)
|
|
|
|
) {
|
2021-06-02 18:35:49 +02:00
|
|
|
this.platformUtilsService.showToast(
|
2021-12-17 15:57:11 +01:00
|
|
|
"error",
|
2021-06-02 18:35:49 +02:00
|
|
|
this.i18nService.t("errorOccurred"),
|
|
|
|
this.i18nService.t("masterPasswordPolicyRequirementsNotMet")
|
|
|
|
);
|
2021-12-17 15:57:11 +01:00
|
|
|
return;
|
2021-06-02 18:35:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.masterPasswordScore < 3) {
|
|
|
|
const result = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t("weakMasterPasswordDesc"),
|
|
|
|
this.i18nService.t("weakMasterPassword"),
|
|
|
|
this.i18nService.t("yes"),
|
|
|
|
this.i18nService.t("no"),
|
2021-12-17 15:57:11 +01:00
|
|
|
"warning"
|
2021-06-02 18:35:49 +02:00
|
|
|
);
|
|
|
|
if (!result) {
|
|
|
|
return false;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-06-02 18:35:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get user Information (kdf type, kdf iterations, resetPasswordKey, private key) and change password
|
|
|
|
try {
|
|
|
|
this.formPromise = this.apiService
|
|
|
|
.getOrganizationUserResetPasswordDetails(this.organizationId, this.id)
|
|
|
|
.then(async (response) => {
|
|
|
|
if (response == null) {
|
|
|
|
throw new Error(this.i18nService.t("resetPasswordDetailsError"));
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
const kdfType = response.kdf;
|
|
|
|
const kdfIterations = response.kdfIterations;
|
|
|
|
const resetPasswordKey = response.resetPasswordKey;
|
|
|
|
const encryptedPrivateKey = response.encryptedPrivateKey;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
// Decrypt Organization's encrypted Private Key with org key
|
|
|
|
const orgSymKey = await this.cryptoService.getOrgKey(this.organizationId);
|
|
|
|
const decPrivateKey = await this.cryptoService.decryptToBytes(
|
|
|
|
new EncString(encryptedPrivateKey),
|
|
|
|
orgSymKey
|
|
|
|
);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
// Decrypt User's Reset Password Key to get EncKey
|
|
|
|
const decValue = await this.cryptoService.rsaDecrypt(resetPasswordKey, decPrivateKey);
|
|
|
|
const userEncKey = new SymmetricCryptoKey(decValue);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
// Create new key and hash new password
|
|
|
|
const newKey = await this.cryptoService.makeKey(
|
|
|
|
this.newPassword,
|
|
|
|
this.email.trim().toLowerCase(),
|
|
|
|
kdfType,
|
|
|
|
kdfIterations
|
|
|
|
);
|
|
|
|
const newPasswordHash = await this.cryptoService.hashPassword(this.newPassword, newKey);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
// Create new encKey for the User
|
|
|
|
const newEncKey = await this.cryptoService.remakeEncKey(newKey, userEncKey);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
// Create request
|
|
|
|
const request = new OrganizationUserResetPasswordRequest();
|
|
|
|
request.key = newEncKey[1].encryptedString;
|
|
|
|
request.newMasterPasswordHash = newPasswordHash;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
// Change user's password
|
|
|
|
return this.apiService.putOrganizationUserResetPassword(
|
|
|
|
this.organizationId,
|
|
|
|
this.id,
|
|
|
|
request
|
|
|
|
);
|
|
|
|
});
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
await this.formPromise;
|
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("resetPasswordSuccess")
|
|
|
|
);
|
|
|
|
this.onPasswordReset.emit();
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2021-06-02 18:35:49 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-06-02 18:35:49 +02:00
|
|
|
|
|
|
|
updatePasswordStrength() {
|
|
|
|
if (this.newPasswordStrengthTimeout != null) {
|
|
|
|
clearTimeout(this.newPasswordStrengthTimeout);
|
|
|
|
}
|
|
|
|
this.newPasswordStrengthTimeout = setTimeout(() => {
|
|
|
|
const strengthResult = this.passwordGenerationService.passwordStrength(
|
|
|
|
this.newPassword,
|
|
|
|
this.getPasswordStrengthUserInput()
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
2021-06-02 18:35:49 +02:00
|
|
|
this.masterPasswordScore = strengthResult == null ? null : strengthResult.score;
|
2021-12-17 15:57:11 +01:00
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
private getPasswordStrengthUserInput() {
|
|
|
|
let userInput: string[] = [];
|
|
|
|
const atPosition = this.email.indexOf("@");
|
|
|
|
if (atPosition > -1) {
|
|
|
|
userInput = userInput.concat(
|
|
|
|
this.email
|
|
|
|
.substr(0, atPosition)
|
|
|
|
.trim()
|
|
|
|
.toLowerCase()
|
|
|
|
.split(/[^A-Za-z0-9]/)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (this.name != null && this.name !== "") {
|
|
|
|
userInput = userInput.concat(this.name.trim().toLowerCase().split(" "));
|
|
|
|
}
|
|
|
|
return userInput;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-06-02 18:35:49 +02:00
|
|
|
}
|