1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-12 10:14:10 +01:00
bitwarden-browser/src/services/import.service.ts

312 lines
15 KiB
TypeScript
Raw Normal View History

import { ApiService } from '../abstractions/api.service';
import { CipherService } from '../abstractions/cipher.service';
2018-08-06 17:39:55 +02:00
import { CollectionService } from '../abstractions/collection.service';
import { FolderService } from '../abstractions/folder.service';
import { I18nService } from '../abstractions/i18n.service';
import {
2018-08-06 16:37:57 +02:00
ImportOption,
ImportService as ImportServiceAbstraction,
} from '../abstractions/import.service';
import { ImportResult } from '../models/domain/importResult';
2019-01-25 15:30:21 +01:00
import { CipherType } from '../enums/cipherType';
import { Utils } from '../misc/utils';
import { CipherRequest } from '../models/request/cipherRequest';
2018-08-06 17:39:55 +02:00
import { CollectionRequest } from '../models/request/collectionRequest';
import { FolderRequest } from '../models/request/folderRequest';
import { ImportCiphersRequest } from '../models/request/importCiphersRequest';
2018-08-06 17:39:55 +02:00
import { ImportOrganizationCiphersRequest } from '../models/request/importOrganizationCiphersRequest';
import { KvpRequest } from '../models/request/kvpRequest';
import { CipherView } from '../models/view/cipherView';
import { AscendoCsvImporter } from '../importers/ascendoCsvImporter';
2018-12-31 18:39:59 +01:00
import { AvastCsvImporter } from '../importers/avastCsvImporter';
import { AviraCsvImporter } from '../importers/aviraCsvImporter';
import { BitwardenCsvImporter } from '../importers/bitwardenCsvImporter';
2018-12-17 19:21:06 +01:00
import { BitwardenJsonImporter } from '../importers/bitwardenJsonImporter';
import { BlurCsvImporter } from '../importers/blurCsvImporter';
import { ChromeCsvImporter } from '../importers/chromeCsvImporter';
import { ClipperzHtmlImporter } from '../importers/clipperzHtmlImporter';
2018-12-12 23:06:13 +01:00
import { DashlaneJsonImporter } from '../importers/dashlaneJsonImporter';
import { EnpassCsvImporter } from '../importers/enpassCsvImporter';
2018-12-13 20:34:37 +01:00
import { EnpassJsonImporter } from '../importers/enpassJsonImporter';
import { FirefoxCsvImporter } from '../importers/firefoxCsvImporter';
import { FSecureFskImporter } from '../importers/fsecureFskImporter';
import { GnomeJsonImporter } from '../importers/gnomeJsonImporter';
import { Importer } from '../importers/importer';
2019-01-28 15:20:51 +01:00
import { KasperskyTxtImporter } from '../importers/kasperskyTxtImporter';
import { KeePass2XmlImporter } from '../importers/keepass2XmlImporter';
import { KeePassXCsvImporter } from '../importers/keepassxCsvImporter';
import { KeeperCsvImporter } from '../importers/keeperCsvImporter';
import { LastPassCsvImporter } from '../importers/lastpassCsvImporter';
2019-08-26 16:06:20 +02:00
import { LogMeOnceCsvImporter } from '../importers/logMeOnceCsvImporter';
import { MeldiumCsvImporter } from '../importers/meldiumCsvImporter';
import { MSecureCsvImporter } from '../importers/msecureCsvImporter';
2019-06-29 05:17:13 +02:00
import { MykiCsvImporter } from '../importers/mykiCsvImporter';
import { OnePassword1PifImporter } from '../importers/onepassword1PifImporter';
import { OnePasswordWinCsvImporter } from '../importers/onepasswordWinCsvImporter';
import { PadlockCsvImporter } from '../importers/padlockCsvImporter';
import { PassKeepCsvImporter } from '../importers/passkeepCsvImporter';
import { PassmanJsonImporter } from '../importers/passmanJsonImporter';
2018-10-08 23:54:54 +02:00
import { PasspackCsvImporter } from '../importers/passpackCsvImporter';
import { PasswordAgentCsvImporter } from '../importers/passwordAgentCsvImporter';
import { PasswordBossJsonImporter } from '../importers/passwordBossJsonImporter';
import { PasswordDragonXmlImporter } from '../importers/passwordDragonXmlImporter';
import { PasswordSafeXmlImporter } from '../importers/passwordSafeXmlImporter';
2019-04-27 02:51:53 +02:00
import { PasswordWalletTxtImporter } from '../importers/passwordWalletTxtImporter';
2019-02-13 21:32:25 +01:00
import { RememBearCsvImporter } from '../importers/rememBearCsvImporter';
import { RoboFormCsvImporter } from '../importers/roboformCsvImporter';
import { SafeInCloudXmlImporter } from '../importers/safeInCloudXmlImporter';
import { SaferPassCsvImporter } from '../importers/saferpassCsvImport';
2019-08-22 05:13:08 +02:00
import { SecureSafeCsvImporter } from '../importers/secureSafeCsvImporter';
import { SplashIdCsvImporter } from '../importers/splashIdCsvImporter';
import { StickyPasswordXmlImporter } from '../importers/stickyPasswordXmlImporter';
import { TrueKeyCsvImporter } from '../importers/truekeyCsvImporter';
import { UpmCsvImporter } from '../importers/upmCsvImporter';
import { ZohoVaultCsvImporter } from '../importers/zohoVaultCsvImporter';
export class ImportService implements ImportServiceAbstraction {
2018-08-06 17:39:55 +02:00
featuredImportOptions = [
2018-12-17 19:21:06 +01:00
{ id: 'bitwardenjson', name: 'Bitwarden (json)' },
{ id: 'bitwardencsv', name: 'Bitwarden (csv)' },
{ id: 'lastpasscsv', name: 'LastPass (csv)' },
{ id: 'chromecsv', name: 'Chrome (csv)' },
{ id: 'firefoxcsv', name: 'Firefox (csv)' },
{ id: 'keepass2xml', name: 'KeePass 2 (xml)' },
{ id: '1password1pif', name: '1Password (1pif)' },
2018-12-12 23:06:13 +01:00
{ id: 'dashlanejson', name: 'Dashlane (json)' },
2018-08-06 17:39:55 +02:00
];
regularImportOptions: ImportOption[] = [
{ id: 'keepassxcsv', name: 'KeePassX (csv)' },
{ id: '1passwordwincsv', name: '1Password 6 and 7 Windows (csv)' },
{ id: 'roboformcsv', name: 'RoboForm (csv)' },
{ id: 'keepercsv', name: 'Keeper (csv)' },
{ id: 'enpasscsv', name: 'Enpass (csv)' },
2018-12-13 20:34:37 +01:00
{ id: 'enpassjson', name: 'Enpass (json)' },
{ id: 'safeincloudxml', name: 'SafeInCloud (xml)' },
{ id: 'pwsafexml', name: 'Password Safe (xml)' },
{ id: 'stickypasswordxml', name: 'Sticky Password (xml)' },
{ id: 'msecurecsv', name: 'mSecure (csv)' },
{ id: 'truekeycsv', name: 'True Key (csv)' },
{ id: 'passwordbossjson', name: 'Password Boss (json)' },
{ id: 'zohovaultcsv', name: 'Zoho Vault (csv)' },
{ id: 'splashidcsv', name: 'SplashID (csv)' },
{ id: 'passworddragonxml', name: 'Password Dragon (xml)' },
{ id: 'padlockcsv', name: 'Padlock (csv)' },
{ id: 'passboltcsv', name: 'Passbolt (csv)' },
{ id: 'clipperzhtml', name: 'Clipperz (html)' },
{ id: 'aviracsv', name: 'Avira (csv)' },
{ id: 'saferpasscsv', name: 'SaferPass (csv)' },
{ id: 'upmcsv', name: 'Universal Password Manager (csv)' },
{ id: 'ascendocsv', name: 'Ascendo DataVault (csv)' },
{ id: 'meldiumcsv', name: 'Meldium (csv)' },
{ id: 'passkeepcsv', name: 'PassKeep (csv)' },
2018-08-06 17:39:55 +02:00
{ id: 'operacsv', name: 'Opera (csv)' },
{ id: 'vivaldicsv', name: 'Vivaldi (csv)' },
{ id: 'gnomejson', name: 'GNOME Passwords and Keys/Seahorse (json)' },
{ id: 'blurcsv', name: 'Blur (csv)' },
{ id: 'passwordagentcsv', name: 'Password Agent (csv)' },
2018-09-21 19:54:06 +02:00
{ id: 'passpackcsv', name: 'Passpack (csv)' },
{ id: 'passmanjson', name: 'Passman (json)' },
2018-12-31 18:39:59 +01:00
{ id: 'avastcsv', name: 'Avast Passwords (csv)' },
{ id: 'fsecurefsk', name: 'F-Secure KEY (fsk)' },
2019-01-28 15:20:51 +01:00
{ id: 'kasperskytxt', name: 'Kaspersky Password Manager (txt)' },
2019-02-13 21:32:25 +01:00
{ id: 'remembearcsv', name: 'RememBear (csv)' },
2019-04-27 02:51:53 +02:00
{ id: 'passwordwallettxt', name: 'PasswordWallet (txt)' },
2019-06-29 05:17:13 +02:00
{ id: 'mykicsv', name: 'Myki (csv)' },
2019-08-22 05:13:08 +02:00
{ id: 'securesafecsv', name: 'SecureSafe (csv)' },
2019-08-26 16:06:20 +02:00
{ id: 'logmeoncecsv', name: 'LogMeOnce (csv)' },
];
2018-08-06 17:39:55 +02:00
constructor(private cipherService: CipherService, private folderService: FolderService,
private apiService: ApiService, private i18nService: I18nService,
private collectionService: CollectionService) { }
2018-08-06 17:39:55 +02:00
getImportOptions(): ImportOption[] {
return this.featuredImportOptions.concat(this.regularImportOptions);
}
async import(importer: Importer, fileContents: string, organizationId: string = null): Promise<Error> {
const importResult = await importer.parse(fileContents);
if (importResult.success) {
if (importResult.folders.length === 0 && importResult.ciphers.length === 0) {
2018-08-06 17:39:55 +02:00
return new Error(this.i18nService.t('importNothingError'));
} else if (importResult.ciphers.length > 0) {
const halfway = Math.floor(importResult.ciphers.length / 2);
const last = importResult.ciphers.length - 1;
if (this.badData(importResult.ciphers[0]) &&
this.badData(importResult.ciphers[halfway]) &&
this.badData(importResult.ciphers[last])) {
2018-08-06 17:39:55 +02:00
return new Error(this.i18nService.t('importFormatError'));
}
}
2018-08-06 17:39:55 +02:00
await this.postImport(importResult, organizationId);
return null;
} else {
2018-08-06 17:39:55 +02:00
return new Error(this.i18nService.t('importFormatError'));
}
}
2018-08-06 17:39:55 +02:00
getImporter(format: string, organization = false): Importer {
const importer = this.getImporterInstance(format);
if (importer == null) {
return null;
}
importer.organization = organization;
return importer;
}
private getImporterInstance(format: string) {
if (format == null || format === '') {
return null;
}
switch (format) {
case 'bitwardencsv':
return new BitwardenCsvImporter();
2018-12-17 19:21:06 +01:00
case 'bitwardenjson':
return new BitwardenJsonImporter();
case 'lastpasscsv':
case 'passboltcsv':
return new LastPassCsvImporter();
case 'keepassxcsv':
return new KeePassXCsvImporter();
case 'aviracsv':
return new AviraCsvImporter();
case 'blurcsv':
return new BlurCsvImporter();
case 'safeincloudxml':
return new SafeInCloudXmlImporter();
case 'padlockcsv':
return new PadlockCsvImporter();
case 'keepass2xml':
return new KeePass2XmlImporter();
case 'chromecsv':
case 'operacsv':
case 'vivaldicsv':
return new ChromeCsvImporter();
case 'firefoxcsv':
return new FirefoxCsvImporter();
case 'upmcsv':
return new UpmCsvImporter();
case 'saferpasscsv':
return new SaferPassCsvImporter();
case 'meldiumcsv':
return new MeldiumCsvImporter();
case '1password1pif':
return new OnePassword1PifImporter();
case '1passwordwincsv':
return new OnePasswordWinCsvImporter();
case 'keepercsv':
return new KeeperCsvImporter();
case 'passworddragonxml':
return new PasswordDragonXmlImporter();
case 'enpasscsv':
return new EnpassCsvImporter();
2018-12-13 20:34:37 +01:00
case 'enpassjson':
return new EnpassJsonImporter();
case 'pwsafexml':
return new PasswordSafeXmlImporter();
2018-12-12 23:06:13 +01:00
case 'dashlanejson':
return new DashlaneJsonImporter();
case 'msecurecsv':
return new MSecureCsvImporter();
case 'stickypasswordxml':
return new StickyPasswordXmlImporter();
case 'truekeycsv':
return new TrueKeyCsvImporter();
case 'clipperzhtml':
return new ClipperzHtmlImporter();
case 'roboformcsv':
return new RoboFormCsvImporter();
case 'ascendocsv':
return new AscendoCsvImporter();
case 'passwordbossjson':
return new PasswordBossJsonImporter();
case 'zohovaultcsv':
return new ZohoVaultCsvImporter();
case 'splashidcsv':
return new SplashIdCsvImporter();
case 'passkeepcsv':
return new PassKeepCsvImporter();
case 'gnomejson':
return new GnomeJsonImporter();
case 'passwordagentcsv':
return new PasswordAgentCsvImporter();
2018-09-21 19:54:06 +02:00
case 'passpackcsv':
return new PasspackCsvImporter();
case 'passmanjson':
return new PassmanJsonImporter();
2018-12-31 18:39:59 +01:00
case 'avastcsv':
return new AvastCsvImporter();
case 'fsecurefsk':
return new FSecureFskImporter();
2019-01-28 15:20:51 +01:00
case 'kasperskytxt':
return new KasperskyTxtImporter();
2019-02-13 21:32:25 +01:00
case 'remembearcsv':
return new RememBearCsvImporter();
2019-04-27 02:51:53 +02:00
case 'passwordwallettxt':
return new PasswordWalletTxtImporter();
2019-06-29 05:17:13 +02:00
case 'mykicsv':
return new MykiCsvImporter();
2019-08-22 05:13:08 +02:00
case 'securesafecsv':
return new SecureSafeCsvImporter();
2019-08-26 16:06:20 +02:00
case 'logmeoncecsv':
return new LogMeOnceCsvImporter();
default:
return null;
}
}
2018-08-06 17:39:55 +02:00
private async postImport(importResult: ImportResult, organizationId: string = null) {
if (organizationId == null) {
const request = new ImportCiphersRequest();
for (let i = 0; i < importResult.ciphers.length; i++) {
const c = await this.cipherService.encrypt(importResult.ciphers[i]);
request.ciphers.push(new CipherRequest(c));
}
2018-08-06 17:39:55 +02:00
if (importResult.folders != null) {
for (let i = 0; i < importResult.folders.length; i++) {
const f = await this.folderService.encrypt(importResult.folders[i]);
request.folders.push(new FolderRequest(f));
}
}
if (importResult.folderRelationships != null) {
importResult.folderRelationships.forEach((r) =>
request.folderRelationships.push(new KvpRequest(r[0], r[1])));
}
return await this.apiService.postImportCiphers(request);
} else {
const request = new ImportOrganizationCiphersRequest();
for (let i = 0; i < importResult.ciphers.length; i++) {
importResult.ciphers[i].organizationId = organizationId;
const c = await this.cipherService.encrypt(importResult.ciphers[i]);
request.ciphers.push(new CipherRequest(c));
}
if (importResult.collections != null) {
for (let i = 0; i < importResult.collections.length; i++) {
importResult.collections[i].organizationId = organizationId;
const c = await this.collectionService.encrypt(importResult.collections[i]);
request.collections.push(new CollectionRequest(c));
}
}
if (importResult.collectionRelationships != null) {
importResult.collectionRelationships.forEach((r) =>
request.collectionRelationships.push(new KvpRequest(r[0], r[1])));
}
return await this.apiService.postImportOrganizationCiphers(organizationId, request);
}
}
private badData(c: CipherView) {
return (c.name == null || c.name === '--') &&
2019-01-25 15:30:21 +01:00
(c.type === CipherType.Login && c.login != null && Utils.isNullOrWhitespace(c.login.password));
}
}