bitwarden-estensione-browser/libs/importer/src/importers/bitwarden/bitwarden-json-importer.ts

172 lines
5.7 KiB
TypeScript
Raw Normal View History

import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { EncString } from "@bitwarden/common/models/domain/enc-string";
import {
CipherWithIdExport,
CollectionWithIdExport,
FolderWithIdExport,
} from "@bitwarden/common/models/export";
2018-12-17 19:21:06 +01:00
import { ImportResult } from "../../models/import-result";
import { BaseImporter } from "../base-importer";
import { Importer } from "../importer";
2018-12-17 19:21:06 +01:00
export class BitwardenJsonImporter extends BaseImporter implements Importer {
2021-12-16 13:36:21 +01:00
private results: any;
private result: ImportResult;
protected constructor(
protected cryptoService: CryptoService,
protected i18nService: I18nService
) {
2021-12-16 13:36:21 +01:00
super();
}
async parse(data: string): Promise<ImportResult> {
this.result = new ImportResult();
this.results = JSON.parse(data);
if (this.results == null || this.results.items == null) {
2021-12-16 13:36:21 +01:00
this.result.success = false;
return this.result;
}
2021-12-16 13:36:21 +01:00
if (this.results.encrypted) {
await this.parseEncrypted();
} else {
this.parseDecrypted();
}
2018-12-17 19:21:06 +01:00
2021-12-16 13:36:21 +01:00
return this.result;
}
private async parseEncrypted() {
if (this.results.encKeyValidation_DO_NOT_EDIT != null) {
const orgKey = await this.cryptoService.getOrgKey(this.organizationId);
const encKeyValidation = new EncString(this.results.encKeyValidation_DO_NOT_EDIT);
const encKeyValidationDecrypt = await this.cryptoService.decryptToUtf8(
encKeyValidation,
orgKey
);
if (encKeyValidationDecrypt === null) {
this.result.success = false;
this.result.errorMessage = this.i18nService.t("importEncKeyError");
return;
}
}
2021-12-16 13:36:21 +01:00
const groupingsMap = new Map<string, number>();
if (this.organization && this.results.collections != null) {
2022-04-19 13:03:04 +02:00
for (const c of this.results.collections as CollectionWithIdExport[]) {
const collection = CollectionWithIdExport.toDomain(c);
2021-12-16 13:36:21 +01:00
if (collection != null) {
collection.organizationId = this.organizationId;
const view = await collection.decrypt();
groupingsMap.set(c.id, this.result.collections.length);
this.result.collections.push(view);
}
2021-12-16 13:36:21 +01:00
}
} else if (!this.organization && this.results.folders != null) {
2022-04-19 13:03:04 +02:00
for (const f of this.results.folders as FolderWithIdExport[]) {
const folder = FolderWithIdExport.toDomain(f);
2021-12-16 13:36:21 +01:00
if (folder != null) {
const view = await folder.decrypt();
groupingsMap.set(f.id, this.result.folders.length);
this.result.folders.push(view);
}
2021-12-16 13:36:21 +01:00
}
}
2022-04-19 13:03:04 +02:00
for (const c of this.results.items as CipherWithIdExport[]) {
const cipher = CipherWithIdExport.toDomain(c);
2021-12-16 13:36:21 +01:00
// reset ids incase they were set for some reason
cipher.id = null;
cipher.organizationId = this.organizationId;
cipher.collectionIds = null;
// make sure password history is limited
if (cipher.passwordHistory != null && cipher.passwordHistory.length > 5) {
cipher.passwordHistory = cipher.passwordHistory.slice(0, 5);
}
if (!this.organization && c.folderId != null && groupingsMap.has(c.folderId)) {
this.result.folderRelationships.push([
this.result.ciphers.length,
groupingsMap.get(c.folderId),
]);
} else if (this.organization && c.collectionIds != null) {
c.collectionIds.forEach((cId) => {
if (groupingsMap.has(cId)) {
this.result.collectionRelationships.push([
this.result.ciphers.length,
groupingsMap.get(cId),
]);
}
});
}
2021-12-16 13:36:21 +01:00
const view = await cipher.decrypt();
this.cleanupCipher(view);
this.result.ciphers.push(view);
}
2021-12-16 13:36:21 +01:00
this.result.success = true;
}
private parseDecrypted() {
const groupingsMap = new Map<string, number>();
if (this.organization && this.results.collections != null) {
2022-04-19 13:03:04 +02:00
this.results.collections.forEach((c: CollectionWithIdExport) => {
const collection = CollectionWithIdExport.toView(c);
2021-12-16 13:36:21 +01:00
if (collection != null) {
collection.organizationId = null;
groupingsMap.set(c.id, this.result.collections.length);
this.result.collections.push(collection);
}
});
} else if (!this.organization && this.results.folders != null) {
2022-04-19 13:03:04 +02:00
this.results.folders.forEach((f: FolderWithIdExport) => {
const folder = FolderWithIdExport.toView(f);
2021-12-16 13:36:21 +01:00
if (folder != null) {
groupingsMap.set(f.id, this.result.folders.length);
this.result.folders.push(folder);
2018-12-17 19:21:06 +01:00
}
2021-12-16 13:36:21 +01:00
});
}
2018-12-17 19:21:06 +01:00
2022-04-19 13:03:04 +02:00
this.results.items.forEach((c: CipherWithIdExport) => {
const cipher = CipherWithIdExport.toView(c);
2021-12-16 13:36:21 +01:00
// reset ids incase they were set for some reason
cipher.id = null;
cipher.organizationId = null;
cipher.collectionIds = null;
// make sure password history is limited
if (cipher.passwordHistory != null && cipher.passwordHistory.length > 5) {
cipher.passwordHistory = cipher.passwordHistory.slice(0, 5);
}
if (!this.organization && c.folderId != null && groupingsMap.has(c.folderId)) {
this.result.folderRelationships.push([
this.result.ciphers.length,
groupingsMap.get(c.folderId),
]);
} else if (this.organization && c.collectionIds != null) {
c.collectionIds.forEach((cId) => {
if (groupingsMap.has(cId)) {
this.result.collectionRelationships.push([
this.result.ciphers.length,
groupingsMap.get(cId),
]);
}
2018-12-17 19:21:06 +01:00
});
2021-12-16 13:36:21 +01:00
}
2021-12-16 13:36:21 +01:00
this.cleanupCipher(cipher);
this.result.ciphers.push(cipher);
});
this.result.success = true;
}
2018-12-17 19:21:06 +01:00
}