2022-10-11 14:08:48 +02:00
|
|
|
import { Component, OnDestroy, OnInit } from "@angular/core";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { AddEditComponent as BaseAddEditComponent } from "@bitwarden/angular/components/add-edit.component";
|
|
|
|
import { AuditService } from "@bitwarden/common/abstractions/audit.service";
|
|
|
|
import { CipherService } from "@bitwarden/common/abstractions/cipher.service";
|
|
|
|
import { CollectionService } from "@bitwarden/common/abstractions/collection.service";
|
|
|
|
import { EventService } from "@bitwarden/common/abstractions/event.service";
|
2022-07-08 15:40:31 +02:00
|
|
|
import { FolderService } from "@bitwarden/common/abstractions/folder/folder.service.abstraction";
|
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 { MessagingService } from "@bitwarden/common/abstractions/messaging.service";
|
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 { PasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service";
|
|
|
|
import { PasswordRepromptService } from "@bitwarden/common/abstractions/passwordReprompt.service";
|
|
|
|
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 { StateService } from "@bitwarden/common/abstractions/state.service";
|
|
|
|
import { TotpService } from "@bitwarden/common/abstractions/totp.service";
|
|
|
|
import { CipherType } from "@bitwarden/common/enums/cipherType";
|
|
|
|
import { EventType } from "@bitwarden/common/enums/eventType";
|
2022-10-14 18:25:50 +02:00
|
|
|
import { LoginUriView } from "@bitwarden/common/models/view/login-uri.view";
|
2018-06-06 23:25:57 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-vault-add-edit",
|
|
|
|
templateUrl: "add-edit.component.html",
|
|
|
|
})
|
2022-10-11 14:08:48 +02:00
|
|
|
export class AddEditComponent extends BaseAddEditComponent implements OnInit, OnDestroy {
|
2018-08-29 05:17:58 +02:00
|
|
|
canAccessPremium: boolean;
|
2018-06-20 05:40:51 +02:00
|
|
|
totpCode: string;
|
|
|
|
totpCodeFormatted: string;
|
|
|
|
totpDash: number;
|
|
|
|
totpSec: number;
|
|
|
|
totpLow: boolean;
|
2018-07-31 04:02:01 +02:00
|
|
|
showRevisionDate = false;
|
|
|
|
hasPasswordHistory = false;
|
2018-07-30 14:48:48 +02:00
|
|
|
viewingPasswordHistory = false;
|
2020-12-22 16:57:44 +01:00
|
|
|
viewOnly = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-07-05 15:42:50 +02:00
|
|
|
protected totpInterval: number;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-06-06 23:25:57 +02:00
|
|
|
constructor(
|
|
|
|
cipherService: CipherService,
|
|
|
|
folderService: FolderService,
|
|
|
|
i18nService: I18nService,
|
|
|
|
platformUtilsService: PlatformUtilsService,
|
2018-06-20 05:40:51 +02:00
|
|
|
auditService: AuditService,
|
|
|
|
stateService: StateService,
|
2021-12-14 17:10:26 +01:00
|
|
|
collectionService: CollectionService,
|
|
|
|
protected totpService: TotpService,
|
|
|
|
protected passwordGenerationService: PasswordGenerationService,
|
|
|
|
protected messagingService: MessagingService,
|
|
|
|
eventService: EventService,
|
|
|
|
protected policyService: PolicyService,
|
|
|
|
organizationService: OrganizationService,
|
|
|
|
logService: LogService,
|
|
|
|
passwordRepromptService: PasswordRepromptService
|
|
|
|
) {
|
2018-10-19 18:44:52 +02:00
|
|
|
super(
|
|
|
|
cipherService,
|
|
|
|
folderService,
|
|
|
|
i18nService,
|
|
|
|
platformUtilsService,
|
|
|
|
auditService,
|
|
|
|
stateService,
|
2021-12-14 17:10:26 +01:00
|
|
|
collectionService,
|
|
|
|
messagingService,
|
|
|
|
eventService,
|
|
|
|
policyService,
|
|
|
|
logService,
|
|
|
|
passwordRepromptService,
|
|
|
|
organizationService
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-07 05:23:14 +02:00
|
|
|
async ngOnInit() {
|
2018-10-19 18:44:52 +02:00
|
|
|
await super.ngOnInit();
|
|
|
|
await this.load();
|
2018-07-31 04:02:01 +02:00
|
|
|
this.showRevisionDate = this.cipher.passwordRevisionDisplayDate != null;
|
|
|
|
this.hasPasswordHistory = this.cipher.hasPasswordHistory;
|
2018-06-20 05:40:51 +02:00
|
|
|
this.cleanUp();
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
this.canAccessPremium = await this.stateService.getCanAccessPremium();
|
2021-12-17 15:57:11 +01:00
|
|
|
if (
|
2018-06-20 05:40:51 +02:00
|
|
|
this.cipher.type === CipherType.Login &&
|
|
|
|
this.cipher.login.totp &&
|
2018-08-29 05:17:58 +02:00
|
|
|
(this.cipher.organizationUseTotp || this.canAccessPremium)
|
2021-12-17 15:57:11 +01:00
|
|
|
) {
|
2018-06-20 05:40:51 +02:00
|
|
|
await this.totpUpdateCode();
|
2018-07-31 17:26:04 +02:00
|
|
|
const interval = this.totpService.getTimeInterval(this.cipher.login.totp);
|
|
|
|
await this.totpTick(interval);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-06-20 05:40:51 +02:00
|
|
|
this.totpInterval = window.setInterval(async () => {
|
2021-12-14 17:10:26 +01:00
|
|
|
await this.totpTick(interval);
|
2018-06-20 05:40:51 +02:00
|
|
|
}, 1000);
|
2018-06-06 23:25:57 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-06 23:25:57 +02:00
|
|
|
|
2022-10-11 14:08:48 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
super.ngOnDestroy();
|
|
|
|
}
|
|
|
|
|
2018-06-07 05:23:14 +02:00
|
|
|
toggleFavorite() {
|
2018-06-20 05:40:51 +02:00
|
|
|
this.cipher.favorite = !this.cipher.favorite;
|
|
|
|
}
|
2018-06-07 23:12:11 +02:00
|
|
|
|
|
|
|
launch(uri: LoginUriView) {
|
|
|
|
if (!uri.canLaunch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.platformUtilsService.launchUri(uri.launchUri);
|
|
|
|
}
|
|
|
|
|
2019-01-07 16:33:27 +01:00
|
|
|
copy(value: string, typeI18nKey: string, aType: string) {
|
2018-06-07 23:12:11 +02:00
|
|
|
if (value == null) {
|
2019-01-07 16:33:27 +01:00
|
|
|
return;
|
2018-06-07 23:12:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 18:25:21 +02:00
|
|
|
this.platformUtilsService.copyToClipboard(value, { window: window });
|
2018-10-03 16:33:04 +02:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"info",
|
|
|
|
null,
|
2018-06-07 23:12:11 +02:00
|
|
|
this.i18nService.t("valueCopied", this.i18nService.t(typeI18nKey))
|
|
|
|
);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-07-12 16:41:18 +02:00
|
|
|
if (this.editMode) {
|
|
|
|
if (typeI18nKey === "password") {
|
|
|
|
this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible, this.cipherId);
|
|
|
|
} else if (typeI18nKey === "securityCode") {
|
|
|
|
this.eventService.collect(EventType.Cipher_ClientCopiedCardCode, this.cipherId);
|
|
|
|
} else if (aType === "H_Field") {
|
|
|
|
this.eventService.collect(EventType.Cipher_ClientCopiedHiddenField, this.cipherId);
|
|
|
|
}
|
2018-06-07 23:12:11 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-20 05:40:51 +02:00
|
|
|
|
2018-06-21 00:16:20 +02:00
|
|
|
async generatePassword(): Promise<boolean> {
|
|
|
|
const confirmed = await super.generatePassword();
|
|
|
|
if (confirmed) {
|
2022-10-11 14:08:48 +02:00
|
|
|
const options = (await this.passwordGenerationService.getOptions())?.[0] ?? {};
|
2018-06-21 00:16:20 +02:00
|
|
|
this.cipher.login.password = await this.passwordGenerationService.generatePassword(options);
|
|
|
|
}
|
|
|
|
return confirmed;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-21 00:16:20 +02:00
|
|
|
|
2018-12-14 20:48:12 +01:00
|
|
|
premiumRequired() {
|
2018-08-29 05:17:58 +02:00
|
|
|
if (!this.canAccessPremium) {
|
2018-07-18 15:21:23 +02:00
|
|
|
this.messagingService.send("premiumRequired");
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-07-18 15:21:23 +02:00
|
|
|
|
2018-12-14 20:48:12 +01:00
|
|
|
upgradeOrganization() {
|
2018-07-18 15:21:23 +02:00
|
|
|
this.messagingService.send("upgradeOrganization", {
|
|
|
|
organizationId: this.cipher.organizationId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-09 20:03:02 +02:00
|
|
|
showGetPremium() {
|
|
|
|
if (this.canAccessPremium) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.cipher.organizationUseTotp) {
|
|
|
|
this.upgradeOrganization();
|
|
|
|
} else {
|
|
|
|
this.premiumRequired();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 14:48:48 +02:00
|
|
|
viewHistory() {
|
|
|
|
this.viewingPasswordHistory = !this.viewingPasswordHistory;
|
|
|
|
}
|
|
|
|
|
2018-07-05 15:42:50 +02:00
|
|
|
protected cleanUp() {
|
2018-06-20 05:40:51 +02:00
|
|
|
if (this.totpInterval) {
|
|
|
|
window.clearInterval(this.totpInterval);
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2018-07-05 15:42:50 +02:00
|
|
|
protected async totpUpdateCode() {
|
2018-06-20 05:40:51 +02:00
|
|
|
if (
|
|
|
|
this.cipher == null ||
|
|
|
|
this.cipher.type !== CipherType.Login ||
|
|
|
|
this.cipher.login.totp == null
|
2018-07-31 17:26:04 +02:00
|
|
|
) {
|
2018-06-20 05:40:51 +02:00
|
|
|
if (this.totpInterval) {
|
|
|
|
window.clearInterval(this.totpInterval);
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
return;
|
2018-06-20 05:40:51 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 20:24:59 +01:00
|
|
|
this.totpCode = await this.totpService.getCode(this.cipher.login.totp);
|
|
|
|
if (this.totpCode != null) {
|
|
|
|
if (this.totpCode.length > 4) {
|
|
|
|
const half = Math.floor(this.totpCode.length / 2);
|
2018-06-20 05:40:51 +02:00
|
|
|
this.totpCodeFormatted =
|
2018-07-31 17:26:04 +02:00
|
|
|
this.totpCode.substring(0, half) + " " + this.totpCode.substring(half);
|
2021-12-17 15:57:11 +01:00
|
|
|
} else {
|
2018-07-31 17:26:04 +02:00
|
|
|
this.totpCodeFormatted = this.totpCode;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-08 20:24:59 +01:00
|
|
|
this.totpCodeFormatted = null;
|
2018-06-20 05:40:51 +02:00
|
|
|
if (this.totpInterval) {
|
2020-12-08 20:24:59 +01:00
|
|
|
window.clearInterval(this.totpInterval);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2020-02-12 22:11:38 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 22:11:38 +01:00
|
|
|
protected allowOwnershipAssignment() {
|
2021-12-17 15:57:11 +01:00
|
|
|
return (
|
2020-12-08 20:24:59 +01:00
|
|
|
(!this.editMode || this.cloneMode) &&
|
2018-07-31 04:02:01 +02:00
|
|
|
this.ownershipOptions != null &&
|
2020-12-08 20:24:59 +01:00
|
|
|
(this.ownershipOptions.length > 1 || !this.allowPersonal)
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-31 17:26:04 +02:00
|
|
|
private async totpTick(intervalSeconds: number) {
|
2018-06-20 05:40:51 +02:00
|
|
|
const epoch = Math.round(new Date().getTime() / 1000.0);
|
2018-07-31 17:26:04 +02:00
|
|
|
const mod = epoch % intervalSeconds;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-07-31 17:26:04 +02:00
|
|
|
this.totpSec = intervalSeconds - mod;
|
|
|
|
this.totpDash = +(Math.round(((78.6 / intervalSeconds) * mod + "e+2") as any) + "e-2");
|
2018-06-20 05:40:51 +02:00
|
|
|
this.totpLow = this.totpSec <= 7;
|
|
|
|
if (mod === 0) {
|
|
|
|
await this.totpUpdateCode();
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-06 23:25:57 +02:00
|
|
|
}
|