bitwarden-estensione-browser/src/angular/components/export.component.ts

111 lines
4.0 KiB
TypeScript
Raw Normal View History

2018-04-10 01:05:13 +02:00
import {
Directive,
2018-04-10 01:05:13 +02:00
EventEmitter,
Output,
} from '@angular/core';
import { CryptoService } from '../../abstractions/crypto.service';
2019-07-12 23:11:36 +02:00
import { EventService } from '../../abstractions/event.service';
2018-05-17 16:52:06 +02:00
import { ExportService } from '../../abstractions/export.service';
2018-04-10 01:05:13 +02:00
import { I18nService } from '../../abstractions/i18n.service';
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
2019-07-12 23:11:36 +02:00
import { EventType } from '../../enums/eventType';
2018-04-10 01:05:13 +02:00
@Directive()
2018-04-10 01:05:13 +02:00
export class ExportComponent {
@Output() onSaved = new EventEmitter();
2018-07-05 20:39:58 +02:00
formPromise: Promise<string>;
2018-04-10 01:05:13 +02:00
masterPassword: string;
format: 'json' | 'encrypted_json' | 'csv' = 'json';
2018-04-10 01:05:13 +02:00
showPassword = false;
constructor(protected cryptoService: CryptoService, protected i18nService: I18nService,
2018-08-14 21:12:10 +02:00
protected platformUtilsService: PlatformUtilsService, protected exportService: ExportService,
2019-07-12 23:11:36 +02:00
protected eventService: EventService, protected win: Window) { }
2018-04-10 01:05:13 +02:00
get encryptedFormat() {
return this.format === 'encrypted_json';
}
2018-04-10 01:05:13 +02:00
async submit() {
let acceptedWarning;
2018-04-10 01:05:13 +02:00
if (this.masterPassword == null || this.masterPassword === '') {
2018-10-03 05:09:19 +02:00
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
2018-04-10 01:05:13 +02:00
this.i18nService.t('invalidMasterPassword'));
return;
}
if (this.encryptedFormat) {
acceptedWarning = await this.platformUtilsService.showDialog(
'<p>' + this.i18nService.t('encExportKeyWarningDesc') +
'<p>' + this.i18nService.t('encExportAccountWarningDesc'),
this.i18nService.t('confirmVaultExport'), this.i18nService.t('exportVault'),
this.i18nService.t('cancel'), 'warning',
true);
} else {
acceptedWarning = await this.platformUtilsService.showDialog(
this.i18nService.t('exportWarningDesc'),
this.i18nService.t('confirmVaultExport'), this.i18nService.t('exportVault'),
this.i18nService.t('cancel'), 'warning');
}
if (!acceptedWarning) {
return;
}
2018-08-14 21:12:10 +02:00
const keyHash = await this.cryptoService.hashPassword(this.masterPassword, null);
2018-04-10 01:05:13 +02:00
const storedKeyHash = await this.cryptoService.getKeyHash();
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
2018-07-05 20:39:58 +02:00
try {
this.formPromise = this.getExportData();
const data = await this.formPromise;
this.platformUtilsService.eventTrack('Exported Data');
2018-07-05 20:39:58 +02:00
this.downloadFile(data);
this.saved();
2019-07-12 23:11:36 +02:00
await this.collectEvent();
2018-07-05 20:39:58 +02:00
} catch { }
2018-04-10 01:05:13 +02:00
} else {
2018-10-03 05:09:19 +02:00
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
2018-04-10 01:05:13 +02:00
this.i18nService.t('invalidMasterPassword'));
}
}
togglePassword() {
this.platformUtilsService.eventTrack('Toggled Master Password on Export');
2018-04-10 01:05:13 +02:00
this.showPassword = !this.showPassword;
document.getElementById('masterPassword').focus();
}
protected saved() {
this.onSaved.emit();
}
2018-07-05 20:39:58 +02:00
protected getExportData() {
2018-12-17 16:54:03 +01:00
return this.exportService.getExport(this.format);
2018-07-05 20:39:58 +02:00
}
protected getFileName(prefix?: string) {
let extension = this.format;
if (this.format === 'encrypted_json') {
if (prefix == null) {
prefix = 'encrypted';
} else {
prefix = 'encrypted_' + prefix;
}
extension = 'json';
}
return this.exportService.getFileName(prefix, extension);
2018-07-05 20:39:58 +02:00
}
2019-07-12 23:11:36 +02:00
protected async collectEvent(): Promise<any> {
await this.eventService.collect(EventType.User_ClientExportedVault);
}
2018-04-10 01:05:13 +02:00
private downloadFile(csv: string): void {
2018-07-05 20:39:58 +02:00
const fileName = this.getFileName();
2018-04-10 01:05:13 +02:00
this.platformUtilsService.saveFile(this.win, csv, { type: 'text/plain' }, fileName);
}
}