2018-04-06 04:21:18 +02:00
|
|
|
import {
|
2018-08-20 23:00:49 +02:00
|
|
|
ChangeDetectorRef,
|
2020-08-17 18:14:40 +02:00
|
|
|
Directive,
|
2018-04-06 04:21:18 +02:00
|
|
|
EventEmitter,
|
|
|
|
Input,
|
2018-08-20 23:00:49 +02:00
|
|
|
NgZone,
|
2018-04-06 04:21:18 +02:00
|
|
|
OnDestroy,
|
2018-08-20 23:00:49 +02:00
|
|
|
OnInit,
|
2018-04-06 04:21:18 +02:00
|
|
|
Output,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { CipherType } from '../../enums/cipherType';
|
2019-07-09 16:51:53 +02:00
|
|
|
import { EventType } from '../../enums/eventType';
|
2018-04-06 04:21:18 +02:00
|
|
|
import { FieldType } from '../../enums/fieldType';
|
|
|
|
|
|
|
|
import { AuditService } from '../../abstractions/audit.service';
|
|
|
|
import { CipherService } from '../../abstractions/cipher.service';
|
|
|
|
import { CryptoService } from '../../abstractions/crypto.service';
|
2019-07-09 16:51:53 +02:00
|
|
|
import { EventService } from '../../abstractions/event.service';
|
2018-04-06 04:21:18 +02:00
|
|
|
import { I18nService } from '../../abstractions/i18n.service';
|
|
|
|
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
|
|
|
import { TokenService } from '../../abstractions/token.service';
|
|
|
|
import { TotpService } from '../../abstractions/totp.service';
|
2018-08-29 05:17:30 +02:00
|
|
|
import { UserService } from '../../abstractions/user.service';
|
2018-04-06 04:21:18 +02:00
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
import { PasswordRepromptService } from '../../abstractions/passwordReprompt.service';
|
|
|
|
import { CipherRepromptType } from '../../enums/cipherRepromptType';
|
2018-04-06 04:21:18 +02:00
|
|
|
import { AttachmentView } from '../../models/view/attachmentView';
|
|
|
|
import { CipherView } from '../../models/view/cipherView';
|
|
|
|
import { FieldView } from '../../models/view/fieldView';
|
|
|
|
import { LoginUriView } from '../../models/view/loginUriView';
|
2018-08-20 23:00:49 +02:00
|
|
|
import { BroadcasterService } from '../services/broadcaster.service';
|
2018-04-06 04:21:18 +02:00
|
|
|
|
2018-08-20 23:00:49 +02:00
|
|
|
const BroadcasterSubscriptionId = 'ViewComponent';
|
|
|
|
|
2020-08-17 18:14:40 +02:00
|
|
|
@Directive()
|
2018-08-20 23:00:49 +02:00
|
|
|
export class ViewComponent implements OnDestroy, OnInit {
|
2018-04-06 04:21:18 +02:00
|
|
|
@Input() cipherId: string;
|
|
|
|
@Output() onEditCipher = new EventEmitter<CipherView>();
|
2020-01-28 23:19:50 +01:00
|
|
|
@Output() onCloneCipher = new EventEmitter<CipherView>();
|
2020-12-16 00:49:51 +01:00
|
|
|
@Output() onShareCipher = new EventEmitter<CipherView>();
|
2020-04-10 22:42:34 +02:00
|
|
|
@Output() onDeletedCipher = new EventEmitter<CipherView>();
|
2020-04-10 22:59:39 +02:00
|
|
|
@Output() onRestoredCipher = new EventEmitter<CipherView>();
|
2018-04-06 04:21:18 +02:00
|
|
|
|
|
|
|
cipher: CipherView;
|
|
|
|
showPassword: boolean;
|
2021-04-15 16:14:33 +02:00
|
|
|
showCardNumber: boolean;
|
2018-06-13 18:33:26 +02:00
|
|
|
showCardCode: boolean;
|
2018-08-29 05:17:30 +02:00
|
|
|
canAccessPremium: boolean;
|
2018-04-06 04:21:18 +02:00
|
|
|
totpCode: string;
|
|
|
|
totpCodeFormatted: string;
|
|
|
|
totpDash: number;
|
|
|
|
totpSec: number;
|
|
|
|
totpLow: boolean;
|
|
|
|
fieldType = FieldType;
|
|
|
|
checkPasswordPromise: Promise<number>;
|
|
|
|
|
|
|
|
private totpInterval: any;
|
2019-07-09 19:08:36 +02:00
|
|
|
private previousCipherId: string;
|
2021-04-15 16:14:33 +02:00
|
|
|
private passwordReprompted: boolean = false;
|
2018-04-06 04:21:18 +02:00
|
|
|
|
|
|
|
constructor(protected cipherService: CipherService, protected totpService: TotpService,
|
2018-10-03 06:03:49 +02:00
|
|
|
protected tokenService: TokenService, protected i18nService: I18nService,
|
2018-04-06 04:21:18 +02:00
|
|
|
protected cryptoService: CryptoService, protected platformUtilsService: PlatformUtilsService,
|
2018-08-20 23:00:49 +02:00
|
|
|
protected auditService: AuditService, protected win: Window,
|
|
|
|
protected broadcasterService: BroadcasterService, protected ngZone: NgZone,
|
2019-07-09 16:51:53 +02:00
|
|
|
protected changeDetectorRef: ChangeDetectorRef, protected userService: UserService,
|
2021-04-15 16:14:33 +02:00
|
|
|
protected eventService: EventService, protected passwordRepromptService: PasswordRepromptService) { }
|
2018-08-20 23:00:49 +02:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
|
|
|
|
this.ngZone.run(async () => {
|
|
|
|
switch (message.command) {
|
|
|
|
case 'syncCompleted':
|
|
|
|
if (message.successfully) {
|
|
|
|
await this.load();
|
|
|
|
this.changeDetectorRef.detectChanges();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-04-06 04:21:18 +02:00
|
|
|
|
2018-04-06 05:49:38 +02:00
|
|
|
ngOnDestroy() {
|
2018-08-20 23:00:49 +02:00
|
|
|
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
|
2018-04-06 05:49:38 +02:00
|
|
|
this.cleanUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2018-04-06 04:21:18 +02:00
|
|
|
this.cleanUp();
|
|
|
|
|
|
|
|
const cipher = await this.cipherService.get(this.cipherId);
|
|
|
|
this.cipher = await cipher.decrypt();
|
2018-08-29 05:17:30 +02:00
|
|
|
this.canAccessPremium = await this.userService.canAccessPremium();
|
2018-04-06 04:21:18 +02:00
|
|
|
|
|
|
|
if (this.cipher.type === CipherType.Login && this.cipher.login.totp &&
|
2018-08-29 05:17:30 +02:00
|
|
|
(cipher.organizationUseTotp || this.canAccessPremium)) {
|
2018-04-06 04:21:18 +02:00
|
|
|
await this.totpUpdateCode();
|
2018-07-31 17:25:50 +02:00
|
|
|
const interval = this.totpService.getTimeInterval(this.cipher.login.totp);
|
|
|
|
await this.totpTick(interval);
|
2018-04-06 04:21:18 +02:00
|
|
|
|
|
|
|
this.totpInterval = setInterval(async () => {
|
2018-07-31 17:25:50 +02:00
|
|
|
await this.totpTick(interval);
|
2018-04-06 04:21:18 +02:00
|
|
|
}, 1000);
|
|
|
|
}
|
2019-07-09 19:08:36 +02:00
|
|
|
|
|
|
|
if (this.previousCipherId !== this.cipherId) {
|
|
|
|
this.eventService.collect(EventType.Cipher_ClientViewed, this.cipherId);
|
|
|
|
}
|
|
|
|
this.previousCipherId = this.cipherId;
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
async edit() {
|
|
|
|
if (await this.promptPassword()) {
|
|
|
|
this.onEditCipher.emit(this.cipher);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
async clone() {
|
|
|
|
if (await this.promptPassword()) {
|
|
|
|
this.onCloneCipher.emit(this.cipher);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-01-28 23:19:50 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
async share() {
|
|
|
|
if (await this.promptPassword()) {
|
|
|
|
this.onShareCipher.emit(this.cipher);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-12-16 00:49:51 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 22:42:34 +02:00
|
|
|
async delete(): Promise<boolean> {
|
2021-04-15 16:14:33 +02:00
|
|
|
if (!await this.promptPassword()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-10 22:42:34 +02:00
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t(this.cipher.isDeleted ? 'permanentlyDeleteItemConfirmation' : 'deleteItemConfirmation'),
|
|
|
|
this.i18nService.t('deleteItem'), this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
|
|
|
|
if (!confirmed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.deleteCipher();
|
|
|
|
this.platformUtilsService.showToast('success', null,
|
|
|
|
this.i18nService.t(this.cipher.isDeleted ? 'permanentlyDeletedItem' : 'deletedItem'));
|
|
|
|
this.onDeletedCipher.emit(this.cipher);
|
|
|
|
} catch { }
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async restore(): Promise<boolean> {
|
2020-04-08 22:44:13 +02:00
|
|
|
if (!this.cipher.isDeleted) {
|
2020-04-10 22:42:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t('restoreItemConfirmation'), this.i18nService.t('restoreItem'),
|
|
|
|
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
|
|
|
|
if (!confirmed) {
|
|
|
|
return false;
|
2020-04-08 22:44:13 +02:00
|
|
|
}
|
2020-04-10 22:42:34 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.restoreCipher();
|
|
|
|
this.platformUtilsService.showToast('success', null, this.i18nService.t('restoredItem'));
|
|
|
|
this.onRestoredCipher.emit(this.cipher);
|
|
|
|
} catch { }
|
|
|
|
|
|
|
|
return true;
|
2020-04-08 22:44:13 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
async togglePassword() {
|
|
|
|
if (!await this.promptPassword()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
this.showPassword = !this.showPassword;
|
2019-07-09 16:51:53 +02:00
|
|
|
if (this.showPassword) {
|
2019-07-09 19:08:36 +02:00
|
|
|
this.eventService.collect(EventType.Cipher_ClientToggledPasswordVisible, this.cipherId);
|
2019-07-09 16:51:53 +02:00
|
|
|
}
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
async toggleCardNumber() {
|
|
|
|
if (!await this.promptPassword()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.showCardNumber = !this.showCardNumber;
|
|
|
|
if (this.showCardNumber) {
|
|
|
|
this.eventService.collect(EventType.Cipher_ClientToggledCardCodeVisible, this.cipherId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async toggleCardCode() {
|
|
|
|
if (!await this.promptPassword()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-13 18:33:26 +02:00
|
|
|
this.showCardCode = !this.showCardCode;
|
2019-07-09 16:51:53 +02:00
|
|
|
if (this.showCardCode) {
|
2019-07-09 19:08:36 +02:00
|
|
|
this.eventService.collect(EventType.Cipher_ClientToggledCardCodeVisible, this.cipherId);
|
2019-07-09 16:51:53 +02:00
|
|
|
}
|
2018-06-13 18:33:26 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
async checkPassword() {
|
|
|
|
if (this.cipher.login == null || this.cipher.login.password == null || this.cipher.login.password === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.checkPasswordPromise = this.auditService.passwordLeaked(this.cipher.login.password);
|
|
|
|
const matches = await this.checkPasswordPromise;
|
|
|
|
|
|
|
|
if (matches > 0) {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('warning', null,
|
|
|
|
this.i18nService.t('passwordExposed', matches.toString()));
|
2018-04-06 04:21:18 +02:00
|
|
|
} else {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('success', null, this.i18nService.t('passwordSafe'));
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
async toggleFieldValue(field: FieldView) {
|
|
|
|
if (!await this.promptPassword()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
const f = (field as any);
|
|
|
|
f.showValue = !f.showValue;
|
2019-07-09 16:51:53 +02:00
|
|
|
if (f.showValue) {
|
2019-07-09 19:08:36 +02:00
|
|
|
this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible, this.cipherId);
|
2019-07-09 16:51:53 +02:00
|
|
|
}
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 19:53:43 +02:00
|
|
|
launch(uri: LoginUriView, cipherId?: string) {
|
2018-04-06 04:21:18 +02:00
|
|
|
if (!uri.canLaunch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-23 19:53:43 +02:00
|
|
|
if (cipherId) {
|
|
|
|
this.cipherService.updateLastLaunchedDate(cipherId);
|
|
|
|
}
|
|
|
|
|
2019-01-07 16:33:13 +01:00
|
|
|
this.platformUtilsService.launchUri(uri.launchUri);
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
async copy(value: string, typeI18nKey: string, aType: string) {
|
2018-04-06 04:21:18 +02:00
|
|
|
if (value == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
if (this.passwordRepromptService.protectedFields().includes(aType) && !await this.promptPassword()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-17 18:24:56 +02:00
|
|
|
const copyOptions = this.win != null ? { window: this.win } : null;
|
2018-04-19 14:00:54 +02:00
|
|
|
this.platformUtilsService.copyToClipboard(value, copyOptions);
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('info', null,
|
2018-04-06 04:21:18 +02:00
|
|
|
this.i18nService.t('valueCopied', this.i18nService.t(typeI18nKey)));
|
2019-07-09 16:51:53 +02:00
|
|
|
|
|
|
|
if (typeI18nKey === 'password') {
|
2019-07-09 19:08:36 +02:00
|
|
|
this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible, this.cipherId);
|
2019-07-09 16:51:53 +02:00
|
|
|
} else if (typeI18nKey === 'securityCode') {
|
2019-07-09 19:08:36 +02:00
|
|
|
this.eventService.collect(EventType.Cipher_ClientCopiedCardCode, this.cipherId);
|
2019-07-09 16:51:53 +02:00
|
|
|
} else if (aType === 'H_Field') {
|
2019-07-09 19:08:36 +02:00
|
|
|
this.eventService.collect(EventType.Cipher_ClientCopiedHiddenField, this.cipherId);
|
2019-07-09 16:51:53 +02:00
|
|
|
}
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 03:14:53 +02:00
|
|
|
setTextDataOnDrag(event: DragEvent, data: string) {
|
|
|
|
event.dataTransfer.setData('text', data);
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
async downloadAttachment(attachment: AttachmentView) {
|
|
|
|
const a = (attachment as any);
|
|
|
|
if (a.downloading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-29 15:21:27 +02:00
|
|
|
if (this.cipher.organizationId == null && !this.canAccessPremium) {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('premiumRequired'),
|
2018-04-06 04:21:18 +02:00
|
|
|
this.i18nService.t('premiumRequiredDesc'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a.downloading = true;
|
2020-01-13 13:49:05 +01:00
|
|
|
const response = await fetch(new Request(attachment.url, { cache: 'no-store' }));
|
2018-04-06 04:21:18 +02:00
|
|
|
if (response.status !== 200) {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', null, this.i18nService.t('errorOccurred'));
|
2018-04-06 04:21:18 +02:00
|
|
|
a.downloading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const buf = await response.arrayBuffer();
|
2018-11-14 02:43:45 +01:00
|
|
|
const key = attachment.key != null ? attachment.key :
|
|
|
|
await this.cryptoService.getOrgKey(this.cipher.organizationId);
|
2018-04-06 04:21:18 +02:00
|
|
|
const decBuf = await this.cryptoService.decryptFromBytes(buf, key);
|
2018-04-07 06:18:31 +02:00
|
|
|
this.platformUtilsService.saveFile(this.win, decBuf, null, attachment.fileName);
|
2018-04-06 04:21:18 +02:00
|
|
|
} catch (e) {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', null, this.i18nService.t('errorOccurred'));
|
2018-04-06 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
a.downloading = false;
|
|
|
|
}
|
|
|
|
|
2020-04-10 22:42:34 +02:00
|
|
|
protected deleteCipher() {
|
|
|
|
return this.cipher.isDeleted ? this.cipherService.deleteWithServer(this.cipher.id)
|
|
|
|
: this.cipherService.softDeleteWithServer(this.cipher.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected restoreCipher() {
|
|
|
|
return this.cipherService.restoreWithServer(this.cipher.id);
|
|
|
|
}
|
|
|
|
|
2021-04-15 16:14:33 +02:00
|
|
|
protected async promptPassword() {
|
|
|
|
if (this.cipher.reprompt === CipherRepromptType.None || this.passwordReprompted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.passwordReprompted = await this.passwordRepromptService.showPasswordPrompt();
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:21:18 +02:00
|
|
|
private cleanUp() {
|
2018-05-30 22:08:43 +02:00
|
|
|
this.totpCode = null;
|
2018-04-06 04:21:18 +02:00
|
|
|
this.cipher = null;
|
|
|
|
this.showPassword = false;
|
|
|
|
if (this.totpInterval) {
|
|
|
|
clearInterval(this.totpInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async totpUpdateCode() {
|
|
|
|
if (this.cipher == null || this.cipher.type !== CipherType.Login || this.cipher.login.totp == null) {
|
|
|
|
if (this.totpInterval) {
|
|
|
|
clearInterval(this.totpInterval);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.totpCode = await this.totpService.getCode(this.cipher.login.totp);
|
|
|
|
if (this.totpCode != null) {
|
2018-07-31 17:25:50 +02:00
|
|
|
if (this.totpCode.length > 4) {
|
|
|
|
const half = Math.floor(this.totpCode.length / 2);
|
|
|
|
this.totpCodeFormatted = this.totpCode.substring(0, half) + ' ' + this.totpCode.substring(half);
|
|
|
|
} else {
|
|
|
|
this.totpCodeFormatted = this.totpCode;
|
|
|
|
}
|
2018-04-06 04:21:18 +02:00
|
|
|
} else {
|
|
|
|
this.totpCodeFormatted = null;
|
|
|
|
if (this.totpInterval) {
|
|
|
|
clearInterval(this.totpInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 17:25:50 +02:00
|
|
|
private async totpTick(intervalSeconds: number) {
|
2018-04-06 04:21:18 +02:00
|
|
|
const epoch = Math.round(new Date().getTime() / 1000.0);
|
2018-07-31 17:25:50 +02:00
|
|
|
const mod = epoch % intervalSeconds;
|
2018-04-06 04:21:18 +02:00
|
|
|
|
2018-07-31 17:25:50 +02:00
|
|
|
this.totpSec = intervalSeconds - mod;
|
|
|
|
this.totpDash = +(Math.round((((78.6 / intervalSeconds) * mod) + 'e+2') as any) + 'e-2');
|
2018-04-06 04:21:18 +02:00
|
|
|
this.totpLow = this.totpSec <= 7;
|
|
|
|
if (mod === 0) {
|
|
|
|
await this.totpUpdateCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|