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-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-25 20:25:44 +01:00
|
|
|
import { UtilsService } from 'jslib/abstractions/utils.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-24 23:41:57 +01:00
|
|
|
@Output() onEditCipher = new EventEmitter<string>();
|
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
|
|
|
|
|
|
|
private totpInterval: NodeJS.Timer;
|
2018-01-24 04:21:14 +01:00
|
|
|
|
2018-01-25 05:26:40 +01:00
|
|
|
constructor(private cipherService: CipherService, private totpService: TotpService,
|
2018-01-25 20:25:44 +01:00
|
|
|
private tokenService: TokenService, private utilsService: UtilsService,
|
|
|
|
private cryptoService: CryptoService, private platformUtilsService: PlatformUtilsService,
|
|
|
|
private i18nService: I18nService) {
|
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();
|
|
|
|
|
|
|
|
if (this.cipher.type == CipherType.Login && this.cipher.login.totp &&
|
|
|
|
(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() {
|
|
|
|
this.onEditCipher.emit(this.cipher.id);
|
|
|
|
}
|
2018-01-25 05:26:40 +01:00
|
|
|
|
|
|
|
togglePassword() {
|
|
|
|
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-25 20:25:44 +01:00
|
|
|
if (this.cipher.login.uri == null || this.cipher.login.uri.indexOf('://') === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.platformUtilsService.launchUri(this.cipher.login.uri);
|
2018-01-25 05:26:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
copy(value: string) {
|
2018-01-25 20:25:44 +01:00
|
|
|
if (value == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.utilsService.copyToClipboard(value, window.document);
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadAttachment(attachment: AttachmentView) {
|
|
|
|
const a = (attachment as any);
|
|
|
|
if (a.downloading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.cipher.organizationId && !this.isPremium) {
|
|
|
|
this.platformUtilsService.alertError(this.i18nService.t('premiumRequired'),
|
|
|
|
this.i18nService.t('premiumRequiredDesc'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a.downloading = true;
|
|
|
|
const response = await fetch(new Request(attachment.url, { cache: 'no-cache' }));
|
|
|
|
if (response.status !== 200) {
|
|
|
|
this.platformUtilsService.alertError(null, this.i18nService.t('errorOccurred'));
|
|
|
|
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) {
|
|
|
|
this.platformUtilsService.alertError(null, this.i18nService.t('errorOccurred'));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|