2020-08-13 20:32:07 +02:00
|
|
|
import { Component } from "@angular/core";
|
2022-05-09 14:21:52 +02:00
|
|
|
import { Router } from "@angular/router";
|
2022-07-12 20:25:18 +02:00
|
|
|
import { firstValueFrom } from "rxjs";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2023-02-06 22:53:37 +01:00
|
|
|
import { ChangePasswordComponent as BaseChangePasswordComponent } from "@bitwarden/angular/auth/components/change-password.component";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
2023-02-23 16:15:45 +01:00
|
|
|
import { AuditService } from "@bitwarden/common/abstractions/audit.service";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { MessagingService } from "@bitwarden/common/abstractions/messaging.service";
|
2022-12-19 19:56:16 +01:00
|
|
|
import { OrganizationUserService } from "@bitwarden/common/abstractions/organization-user/organization-user.service";
|
|
|
|
import { OrganizationUserResetPasswordEnrollmentRequest } from "@bitwarden/common/abstractions/organization-user/requests";
|
2022-08-24 18:33:05 +02:00
|
|
|
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/abstractions/organization/organization-api.service.abstraction";
|
2022-09-27 22:25:19 +02:00
|
|
|
import { OrganizationService } from "@bitwarden/common/abstractions/organization/organization.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
2022-08-08 11:04:36 +02:00
|
|
|
import { PolicyService } from "@bitwarden/common/abstractions/policy/policy.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { SendService } from "@bitwarden/common/abstractions/send.service";
|
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
2023-02-06 22:53:37 +01:00
|
|
|
import { KeyConnectorService } from "@bitwarden/common/auth/abstractions/key-connector.service";
|
|
|
|
import { EmergencyAccessStatusType } from "@bitwarden/common/auth/enums/emergency-access-status-type";
|
|
|
|
import { EmergencyAccessUpdateRequest } from "@bitwarden/common/auth/models/request/emergency-access-update.request";
|
|
|
|
import { PasswordRequest } from "@bitwarden/common/auth/models/request/password.request";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
2022-10-14 18:25:50 +02:00
|
|
|
import { EncString } from "@bitwarden/common/models/domain/enc-string";
|
|
|
|
import { SymmetricCryptoKey } from "@bitwarden/common/models/domain/symmetric-crypto-key";
|
2022-10-18 19:01:42 +02:00
|
|
|
import { SendWithIdRequest } from "@bitwarden/common/models/request/send-with-id.request";
|
|
|
|
import { UpdateKeyRequest } from "@bitwarden/common/models/request/update-key.request";
|
2023-03-10 21:39:46 +01:00
|
|
|
import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/password";
|
2023-01-31 22:08:37 +01:00
|
|
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
|
|
|
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
|
|
|
import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction";
|
|
|
|
import { CipherWithIdRequest } from "@bitwarden/common/vault/models/request/cipher-with-id.request";
|
|
|
|
import { FolderWithIdRequest } from "@bitwarden/common/vault/models/request/folder-with-id.request";
|
2018-06-21 17:47:23 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-change-password",
|
|
|
|
templateUrl: "change-password.component.html",
|
|
|
|
})
|
2020-08-13 20:32:07 +02:00
|
|
|
export class ChangePasswordComponent extends BaseChangePasswordComponent {
|
2018-11-13 17:06:16 +01:00
|
|
|
rotateEncKey = false;
|
2020-08-13 20:32:07 +02:00
|
|
|
currentMasterPassword: string;
|
2022-07-18 23:47:53 +02:00
|
|
|
masterPasswordHint: string;
|
2023-02-23 16:15:45 +01:00
|
|
|
checkForBreaches = true;
|
|
|
|
characterMinimumMessage = "";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-08-21 20:40:48 +02:00
|
|
|
constructor(
|
|
|
|
i18nService: I18nService,
|
2020-08-13 20:32:07 +02:00
|
|
|
cryptoService: CryptoService,
|
|
|
|
messagingService: MessagingService,
|
2021-12-14 17:10:26 +01:00
|
|
|
stateService: StateService,
|
2023-03-10 21:39:46 +01:00
|
|
|
passwordGenerationService: PasswordGenerationServiceAbstraction,
|
2020-08-21 20:40:48 +02:00
|
|
|
platformUtilsService: PlatformUtilsService,
|
|
|
|
policyService: PolicyService,
|
2023-02-23 16:15:45 +01:00
|
|
|
private auditService: AuditService,
|
2020-08-21 20:40:48 +02:00
|
|
|
private folderService: FolderService,
|
|
|
|
private cipherService: CipherService,
|
2021-12-14 17:10:26 +01:00
|
|
|
private syncService: SyncService,
|
|
|
|
private apiService: ApiService,
|
|
|
|
private sendService: SendService,
|
2022-05-09 14:21:52 +02:00
|
|
|
private organizationService: OrganizationService,
|
|
|
|
private keyConnectorService: KeyConnectorService,
|
2022-08-24 18:33:05 +02:00
|
|
|
private router: Router,
|
2022-12-19 19:56:16 +01:00
|
|
|
private organizationApiService: OrganizationApiServiceAbstraction,
|
|
|
|
private organizationUserService: OrganizationUserService
|
2018-11-13 17:06:16 +01:00
|
|
|
) {
|
2018-11-14 21:54:13 +01:00
|
|
|
super(
|
|
|
|
i18nService,
|
|
|
|
cryptoService,
|
|
|
|
messagingService,
|
|
|
|
passwordGenerationService,
|
|
|
|
platformUtilsService,
|
|
|
|
policyService,
|
|
|
|
stateService
|
|
|
|
);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2022-05-09 14:21:52 +02:00
|
|
|
async ngOnInit() {
|
|
|
|
if (await this.keyConnectorService.getUsesKeyConnector()) {
|
|
|
|
this.router.navigate(["/settings/security/two-factor"]);
|
|
|
|
}
|
2022-07-18 23:47:53 +02:00
|
|
|
|
|
|
|
this.masterPasswordHint = (await this.apiService.getProfile()).masterPasswordHint;
|
2022-05-11 21:39:56 +02:00
|
|
|
await super.ngOnInit();
|
2023-02-23 16:15:45 +01:00
|
|
|
|
|
|
|
this.characterMinimumMessage = this.i18nService.t("characterMinimum", this.minimumLength);
|
2022-05-09 14:21:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 18:25:27 +01:00
|
|
|
async rotateEncKeyClicked() {
|
2018-11-14 21:54:13 +01:00
|
|
|
if (this.rotateEncKey) {
|
2018-11-13 17:06:16 +01:00
|
|
|
const ciphers = await this.cipherService.getAllDecrypted();
|
|
|
|
let hasOldAttachments = false;
|
2018-11-14 21:54:13 +01:00
|
|
|
if (ciphers != null) {
|
2018-11-13 17:06:16 +01:00
|
|
|
for (let i = 0; i < ciphers.length; i++) {
|
2018-11-14 21:54:13 +01:00
|
|
|
if (ciphers[i].organizationId == null && ciphers[i].hasOldAttachments) {
|
2018-11-13 17:06:16 +01:00
|
|
|
hasOldAttachments = true;
|
2021-12-17 15:57:11 +01:00
|
|
|
break;
|
2018-11-13 17:06:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-08-17 21:04:59 +02:00
|
|
|
if (hasOldAttachments) {
|
|
|
|
const learnMore = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t("oldAttachmentsNeedFixDesc"),
|
|
|
|
null,
|
|
|
|
this.i18nService.t("learnMore"),
|
2018-11-14 21:54:13 +01:00
|
|
|
this.i18nService.t("close"),
|
2021-12-17 15:57:11 +01:00
|
|
|
"warning"
|
2020-08-17 21:04:59 +02:00
|
|
|
);
|
|
|
|
if (learnMore) {
|
|
|
|
this.platformUtilsService.launchUri(
|
2022-01-31 20:11:27 +01:00
|
|
|
"https://bitwarden.com/help/attachments/#add-storage-space"
|
2020-08-17 21:04:59 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
this.rotateEncKey = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-17 21:04:59 +02:00
|
|
|
const result = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t("updateEncryptionKeyWarning") +
|
2021-12-17 15:57:11 +01:00
|
|
|
" " +
|
2020-12-04 15:58:26 +01:00
|
|
|
this.i18nService.t("updateEncryptionKeyExportWarning") +
|
2021-12-17 15:57:11 +01:00
|
|
|
" " +
|
2018-11-13 17:06:16 +01:00
|
|
|
this.i18nService.t("rotateEncKeyConfirmation"),
|
2020-08-17 21:04:59 +02:00
|
|
|
this.i18nService.t("rotateEncKeyTitle"),
|
2018-11-13 17:06:16 +01:00
|
|
|
this.i18nService.t("yes"),
|
|
|
|
this.i18nService.t("no"),
|
2021-12-17 15:57:11 +01:00
|
|
|
"warning"
|
2020-08-17 21:04:59 +02:00
|
|
|
);
|
|
|
|
if (!result) {
|
2018-11-13 17:06:16 +01:00
|
|
|
this.rotateEncKey = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2020-08-17 21:04:59 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2020-08-17 21:04:59 +02:00
|
|
|
|
2020-08-13 20:32:07 +02:00
|
|
|
async submit() {
|
|
|
|
const hasEncKey = await this.cryptoService.hasEncKey();
|
|
|
|
if (!hasEncKey) {
|
|
|
|
this.platformUtilsService.showToast("error", null, this.i18nService.t("updateKey"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:15:45 +01:00
|
|
|
if (this.masterPasswordHint != null && this.masterPasswordHint == this.masterPassword) {
|
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"error",
|
|
|
|
this.i18nService.t("errorOccurred"),
|
|
|
|
this.i18nService.t("hintEqualsPassword")
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.leakedPassword = false;
|
|
|
|
if (this.checkForBreaches) {
|
|
|
|
this.leakedPassword = (await this.auditService.passwordLeaked(this.masterPassword)) > 0;
|
|
|
|
}
|
|
|
|
|
2020-08-17 21:04:59 +02:00
|
|
|
await super.submit();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2020-08-17 21:04:59 +02:00
|
|
|
async setupSubmitActions() {
|
2020-08-13 20:32:07 +02:00
|
|
|
if (this.currentMasterPassword == null || this.currentMasterPassword === "") {
|
|
|
|
this.platformUtilsService.showToast(
|
2021-12-17 15:57:11 +01:00
|
|
|
"error",
|
2020-08-13 20:32:07 +02:00
|
|
|
this.i18nService.t("errorOccurred"),
|
2022-09-15 19:02:01 +02:00
|
|
|
this.i18nService.t("masterPasswordRequired")
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
2020-08-17 21:04:59 +02:00
|
|
|
return false;
|
2020-08-13 20:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.rotateEncKey) {
|
|
|
|
await this.syncService.fullSync(true);
|
2018-11-13 05:26:00 +01:00
|
|
|
}
|
2018-11-13 17:06:16 +01:00
|
|
|
|
2018-11-14 22:22:57 +01:00
|
|
|
return super.setupSubmitActions();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 22:22:57 +01:00
|
|
|
async performSubmitActions(
|
|
|
|
newMasterPasswordHash: string,
|
2020-08-13 20:32:07 +02:00
|
|
|
newKey: SymmetricCryptoKey,
|
2018-11-14 22:22:57 +01:00
|
|
|
newEncKey: [SymmetricCryptoKey, EncString]
|
2021-12-17 15:57:11 +01:00
|
|
|
) {
|
2020-08-13 20:32:07 +02:00
|
|
|
const request = new PasswordRequest();
|
|
|
|
request.masterPasswordHash = await this.cryptoService.hashPassword(
|
|
|
|
this.currentMasterPassword,
|
2021-12-17 15:57:11 +01:00
|
|
|
null
|
|
|
|
);
|
2022-07-18 23:47:53 +02:00
|
|
|
request.masterPasswordHint = this.masterPasswordHint;
|
2020-08-13 20:32:07 +02:00
|
|
|
request.newMasterPasswordHash = newMasterPasswordHash;
|
|
|
|
request.key = newEncKey[1].encryptedString;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
|
|
|
try {
|
2018-11-14 22:22:57 +01:00
|
|
|
if (this.rotateEncKey) {
|
|
|
|
this.formPromise = this.apiService.postPassword(request).then(() => {
|
|
|
|
return this.updateKey(newKey, request.newMasterPasswordHash);
|
2018-11-13 17:06:16 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.formPromise = this.apiService.postPassword(request);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-13 17:06:16 +01:00
|
|
|
await this.formPromise;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-11-13 17:06:16 +01:00
|
|
|
this.platformUtilsService.showToast(
|
2021-12-17 15:57:11 +01:00
|
|
|
"success",
|
2018-11-13 17:06:16 +01:00
|
|
|
this.i18nService.t("masterPasswordChanged"),
|
2020-08-13 20:32:07 +02:00
|
|
|
this.i18nService.t("logBackIn")
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
2020-08-13 20:32:07 +02:00
|
|
|
this.messagingService.send("logout");
|
2018-11-13 17:06:16 +01:00
|
|
|
} catch {
|
2021-04-21 21:20:20 +02:00
|
|
|
this.platformUtilsService.showToast("error", null, this.i18nService.t("errorOccurred"));
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 21:20:20 +02:00
|
|
|
private async updateKey(key: SymmetricCryptoKey, masterPasswordHash: string) {
|
|
|
|
const encKey = await this.cryptoService.makeEncKey(key);
|
2020-12-22 16:57:44 +01:00
|
|
|
const privateKey = await this.cryptoService.getPrivateKey();
|
2021-04-21 21:20:20 +02:00
|
|
|
let encPrivateKey: EncString = null;
|
2018-11-13 17:06:16 +01:00
|
|
|
if (privateKey != null) {
|
|
|
|
encPrivateKey = await this.cryptoService.encrypt(privateKey, encKey[0]);
|
|
|
|
}
|
|
|
|
const request = new UpdateKeyRequest();
|
|
|
|
request.privateKey = encPrivateKey != null ? encPrivateKey.encryptedString : null;
|
|
|
|
request.key = encKey[1].encryptedString;
|
|
|
|
request.masterPasswordHash = masterPasswordHash;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2022-07-12 20:25:18 +02:00
|
|
|
const folders = await firstValueFrom(this.folderService.folderViews$);
|
2018-11-13 17:06:16 +01:00
|
|
|
for (let i = 0; i < folders.length; i++) {
|
|
|
|
if (folders[i].id == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const folder = await this.folderService.encrypt(folders[i], encKey[0]);
|
|
|
|
request.folders.push(new FolderWithIdRequest(folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ciphers = await this.cipherService.getAllDecrypted();
|
|
|
|
for (let i = 0; i < ciphers.length; i++) {
|
|
|
|
if (ciphers[i].organizationId != null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cipher = await this.cipherService.encrypt(ciphers[i], encKey[0]);
|
|
|
|
request.ciphers.push(new CipherWithIdRequest(cipher));
|
|
|
|
}
|
|
|
|
|
2021-07-19 12:14:24 +02:00
|
|
|
const sends = await this.sendService.getAll();
|
|
|
|
await Promise.all(
|
|
|
|
sends.map(async (send) => {
|
|
|
|
const cryptoKey = await this.cryptoService.decryptToBytes(send.key, null);
|
|
|
|
send.key = (await this.cryptoService.encrypt(cryptoKey, encKey[0])) ?? send.key;
|
|
|
|
request.sends.push(new SendWithIdRequest(send));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2018-11-14 22:22:57 +01:00
|
|
|
await this.apiService.postAccountKey(request);
|
2020-12-22 16:57:44 +01:00
|
|
|
|
|
|
|
await this.updateEmergencyAccesses(encKey[0]);
|
2021-04-08 18:09:06 +02:00
|
|
|
|
2022-07-06 21:19:58 +02:00
|
|
|
await this.updateAllResetPasswordKeys(encKey[0], masterPasswordHash);
|
2020-12-22 16:57:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private async updateEmergencyAccesses(encKey: SymmetricCryptoKey) {
|
|
|
|
const emergencyAccess = await this.apiService.getEmergencyAccessTrusted();
|
|
|
|
const allowedStatuses = [
|
|
|
|
EmergencyAccessStatusType.Confirmed,
|
|
|
|
EmergencyAccessStatusType.RecoveryInitiated,
|
|
|
|
EmergencyAccessStatusType.RecoveryApproved,
|
|
|
|
];
|
|
|
|
|
|
|
|
const filteredAccesses = emergencyAccess.data.filter((d) => allowedStatuses.includes(d.status));
|
|
|
|
|
|
|
|
for (const details of filteredAccesses) {
|
|
|
|
const publicKeyResponse = await this.apiService.getUserPublicKey(details.granteeId);
|
|
|
|
const publicKey = Utils.fromB64ToArray(publicKeyResponse.publicKey);
|
|
|
|
|
|
|
|
const encryptedKey = await this.cryptoService.rsaEncrypt(encKey.key, publicKey.buffer);
|
|
|
|
|
|
|
|
const updateRequest = new EmergencyAccessUpdateRequest();
|
|
|
|
updateRequest.type = details.type;
|
|
|
|
updateRequest.waitTimeDays = details.waitTimeDays;
|
|
|
|
updateRequest.keyEncrypted = encryptedKey.encryptedString;
|
|
|
|
|
|
|
|
await this.apiService.putEmergencyAccess(details.id, updateRequest);
|
2018-11-13 17:06:16 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-04-08 18:09:06 +02:00
|
|
|
|
2022-07-06 21:19:58 +02:00
|
|
|
private async updateAllResetPasswordKeys(encKey: SymmetricCryptoKey, masterPasswordHash: string) {
|
2021-12-14 17:10:26 +01:00
|
|
|
const orgs = await this.organizationService.getAll();
|
2021-04-08 18:09:06 +02:00
|
|
|
|
|
|
|
for (const org of orgs) {
|
|
|
|
// If not already enrolled, skip
|
2021-05-25 19:24:09 +02:00
|
|
|
if (!org.resetPasswordEnrolled) {
|
2021-04-08 18:09:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
// Retrieve public key
|
2022-08-24 18:33:05 +02:00
|
|
|
const response = await this.organizationApiService.getKeys(org.id);
|
2021-06-02 18:35:49 +02:00
|
|
|
const publicKey = Utils.fromB64ToArray(response?.publicKey);
|
|
|
|
|
|
|
|
// Re-enroll - encrpyt user's encKey.key with organization public key
|
|
|
|
const encryptedKey = await this.cryptoService.rsaEncrypt(encKey.key, publicKey.buffer);
|
2021-04-08 18:09:06 +02:00
|
|
|
|
|
|
|
// Create/Execute request
|
|
|
|
const request = new OrganizationUserResetPasswordEnrollmentRequest();
|
2022-07-06 21:19:58 +02:00
|
|
|
request.masterPasswordHash = masterPasswordHash;
|
2021-04-08 18:09:06 +02:00
|
|
|
request.resetPasswordKey = encryptedKey.encryptedString;
|
|
|
|
|
2022-12-19 19:56:16 +01:00
|
|
|
await this.organizationUserService.putOrganizationUserResetPasswordEnrollment(
|
|
|
|
org.id,
|
|
|
|
org.userId,
|
|
|
|
request
|
|
|
|
);
|
2021-04-08 18:09:06 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-21 17:47:23 +02:00
|
|
|
}
|