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

64 lines
2.5 KiB
TypeScript
Raw Normal View History

2018-04-10 01:05:13 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import {
EventEmitter,
Output,
} from '@angular/core';
import { CryptoService } from '../../abstractions/crypto.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';
import { UserService } from '../../abstractions/user.service';
export class ExportComponent {
@Output() onSaved = new EventEmitter();
masterPassword: string;
showPassword = false;
constructor(protected analytics: Angulartics2, protected toasterService: ToasterService,
protected cryptoService: CryptoService, protected userService: UserService,
protected i18nService: I18nService, protected platformUtilsService: PlatformUtilsService,
2018-05-17 16:52:06 +02:00
protected exportService: ExportService, protected win: Window) { }
2018-04-10 01:05:13 +02:00
async submit() {
if (this.masterPassword == null || this.masterPassword === '') {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('invalidMasterPassword'));
return;
}
const email = await this.userService.getEmail();
const key = await this.cryptoService.makeKey(this.masterPassword, email);
2018-04-10 01:05:13 +02:00
const keyHash = await this.cryptoService.hashPassword(this.masterPassword, key);
const storedKeyHash = await this.cryptoService.getKeyHash();
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
2018-05-17 16:52:06 +02:00
const csv = await this.exportService.getCsv();
2018-04-10 01:05:13 +02:00
this.analytics.eventTrack.next({ action: 'Exported Data' });
this.downloadFile(csv);
this.saved();
} else {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('invalidMasterPassword'));
}
}
togglePassword() {
this.analytics.eventTrack.next({ action: 'Toggled Master Password on Export' });
this.showPassword = !this.showPassword;
document.getElementById('masterPassword').focus();
}
protected saved() {
this.onSaved.emit();
}
private downloadFile(csv: string): void {
2018-05-17 16:52:06 +02:00
const fileName = this.exportService.getFileName();
2018-04-10 01:05:13 +02:00
this.platformUtilsService.saveFile(this.win, csv, { type: 'text/plain' }, fileName);
}
}