1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-11-10 09:49:53 +01:00
bitwarden-desktop/src/app/vault/view.component.ts

178 lines
5.6 KiB
TypeScript
Raw Normal View History

2018-01-24 04:21:14 +01:00
import * as template from './view.component.html';
import {
Component,
2018-01-24 23:41:57 +01:00
EventEmitter,
2018-01-24 04:21:14 +01:00
Input,
OnChanges,
2018-01-25 05:26:40 +01:00
OnDestroy,
2018-01-24 23:41:57 +01:00
Output,
2018-01-24 04:21:14 +01:00
} from '@angular/core';
2018-01-26 22:13:58 +01:00
import { ToasterService } from 'angular2-toaster';
2018-02-08 16:37:54 +01:00
import { Angulartics2 } from 'angulartics2';
2018-01-26 18:32:30 +01:00
2018-01-25 05:26:40 +01:00
import { CipherType } from 'jslib/enums/cipherType';
2018-01-25 20:25:44 +01:00
import { FieldType } from 'jslib/enums/fieldType';
2018-01-25 05:26:40 +01:00
2018-01-24 06:06:05 +01:00
import { CipherService } from 'jslib/abstractions/cipher.service';
2018-01-25 20:25:44 +01:00
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-01-25 05:26:40 +01:00
import { TokenService } from 'jslib/abstractions/token.service';
import { TotpService } from 'jslib/abstractions/totp.service';
2018-01-24 06:06:05 +01:00
2018-01-25 20:25:44 +01:00
import { AttachmentView } from 'jslib/models/view/attachmentView';
2018-01-24 18:20:01 +01:00
import { CipherView } from 'jslib/models/view/cipherView';
2018-01-25 20:25:44 +01:00
import { FieldView } from 'jslib/models/view/fieldView';
2018-01-24 18:20:01 +01:00
2018-01-24 04:21:14 +01:00
@Component({
selector: 'app-vault-view',
template: template,
})
2018-01-25 05:26:40 +01:00
export class ViewComponent implements OnChanges, OnDestroy {
2018-01-24 04:21:14 +01:00
@Input() cipherId: string;
2018-01-26 20:56:54 +01:00
@Output() onEditCipher = new EventEmitter<CipherView>();
2018-01-24 18:20:01 +01:00
cipher: CipherView;
2018-01-25 05:26:40 +01:00
showPassword: boolean;
isPremium: boolean;
totpCode: string;
totpCodeFormatted: string;
totpDash: number;
totpSec: number;
totpLow: boolean;
2018-01-25 20:25:44 +01:00
fieldType = FieldType;
2018-01-25 05:26:40 +01:00
2018-01-26 18:32:30 +01:00
private totpInterval: any;
2018-01-24 04:21:14 +01:00
2018-01-25 05:26:40 +01:00
constructor(private cipherService: CipherService, private totpService: TotpService,
private tokenService: TokenService, private toasterService: ToasterService,
2018-01-25 20:25:44 +01:00
private cryptoService: CryptoService, private platformUtilsService: PlatformUtilsService,
private i18nService: I18nService, private analytics: Angulartics2) { }
2018-01-24 04:21:14 +01:00
2018-01-24 06:06:05 +01:00
async ngOnChanges() {
2018-01-25 17:28:52 +01:00
this.cleanUp();
2018-01-25 05:26:40 +01:00
2018-01-24 06:06:05 +01:00
const cipher = await this.cipherService.get(this.cipherId);
this.cipher = await cipher.decrypt();
2018-01-25 05:26:40 +01:00
this.isPremium = this.tokenService.getPremium();
2018-01-25 20:57:49 +01:00
if (this.cipher.type === CipherType.Login && this.cipher.login.totp &&
2018-01-25 05:26:40 +01:00
(cipher.organizationUseTotp || this.isPremium)) {
await this.totpUpdateCode();
await this.totpTick();
this.totpInterval = setInterval(async () => {
await this.totpTick();
}, 1000);
}
}
ngOnDestroy() {
2018-01-25 17:28:52 +01:00
this.cleanUp();
2018-01-24 04:21:14 +01:00
}
2018-01-24 23:41:57 +01:00
edit() {
2018-01-26 20:56:54 +01:00
this.onEditCipher.emit(this.cipher);
2018-01-24 23:41:57 +01:00
}
2018-01-25 05:26:40 +01:00
togglePassword() {
2018-01-26 18:32:30 +01:00
this.analytics.eventTrack.next({ action: 'Toggled Password' });
2018-01-25 05:26:40 +01:00
this.showPassword = !this.showPassword;
}
2018-01-25 20:25:44 +01:00
toggleFieldValue(field: FieldView) {
const f = (field as any);
f.showValue = !f.showValue;
}
2018-01-25 05:26:40 +01:00
launch() {
2018-01-26 18:32:30 +01:00
if (!this.cipher.login.canLaunch) {
2018-01-25 20:25:44 +01:00
return;
}
2018-01-26 18:32:30 +01:00
this.analytics.eventTrack.next({ action: 'Launched Login URI' });
2018-01-25 20:25:44 +01:00
this.platformUtilsService.launchUri(this.cipher.login.uri);
2018-01-25 05:26:40 +01:00
}
2018-02-23 22:31:52 +01:00
copy(value: string, typeI18nKey: string, aType: string) {
2018-01-25 20:25:44 +01:00
if (value == null) {
return;
}
2018-01-26 18:32:30 +01:00
this.analytics.eventTrack.next({ action: 'Copied ' + aType });
this.platformUtilsService.copyToClipboard(value);
2018-02-23 22:31:52 +01:00
this.toasterService.popAsync('info', null,
this.i18nService.t('valueCopied', this.i18nService.t(typeI18nKey)));
2018-01-25 20:25:44 +01:00
}
async downloadAttachment(attachment: AttachmentView) {
const a = (attachment as any);
if (a.downloading) {
return;
}
2018-01-26 18:32:30 +01:00
if (this.cipher.organizationId == null && !this.isPremium) {
2018-01-26 22:13:58 +01:00
this.toasterService.popAsync('error', this.i18nService.t('premiumRequired'),
2018-01-25 20:25:44 +01:00
this.i18nService.t('premiumRequiredDesc'));
return;
}
a.downloading = true;
const response = await fetch(new Request(attachment.url, { cache: 'no-cache' }));
if (response.status !== 200) {
2018-01-26 22:13:58 +01:00
this.toasterService.popAsync('error', null, this.i18nService.t('errorOccurred'));
2018-01-25 20:25:44 +01:00
a.downloading = false;
return;
}
try {
const buf = await response.arrayBuffer();
const key = await this.cryptoService.getOrgKey(this.cipher.organizationId);
const decBuf = await this.cryptoService.decryptFromBytes(buf, key);
this.platformUtilsService.saveFile(window, decBuf, null, attachment.fileName);
} catch (e) {
2018-01-26 22:13:58 +01:00
this.toasterService.popAsync('error', null, this.i18nService.t('errorOccurred'));
2018-01-25 20:25:44 +01:00
}
a.downloading = false;
2018-01-25 05:26:40 +01:00
}
2018-01-25 17:28:52 +01:00
private cleanUp() {
this.cipher = null;
this.showPassword = false;
if (this.totpInterval) {
clearInterval(this.totpInterval);
}
}
2018-01-25 05:26:40 +01:00
private async totpUpdateCode() {
if (this.cipher.type !== CipherType.Login || this.cipher.login.totp == null) {
return;
}
this.totpCode = await this.totpService.getCode(this.cipher.login.totp);
if (this.totpCode != null) {
this.totpCodeFormatted = this.totpCode.substring(0, 3) + ' ' + this.totpCode.substring(3);
} else {
this.totpCodeFormatted = null;
if (this.totpInterval) {
clearInterval(this.totpInterval);
}
}
}
private async totpTick() {
const epoch = Math.round(new Date().getTime() / 1000.0);
const mod = epoch % 30;
this.totpSec = 30 - mod;
this.totpDash = +(Math.round(((2.62 * mod) + 'e+2') as any) + 'e-2');
this.totpLow = this.totpSec <= 7;
if (mod === 0) {
await this.totpUpdateCode();
}
}
2018-01-24 04:21:14 +01:00
}