2018-05-17 16:52:06 +02:00
|
|
|
import * as papa from "papaparse";
|
2022-07-12 20:25:18 +02:00
|
|
|
import { firstValueFrom } from "rxjs";
|
2018-05-17 16:52:06 +02:00
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
import { ApiService } from "../abstractions/api.service";
|
2018-05-17 16:52:06 +02:00
|
|
|
import { CipherService } from "../abstractions/cipher.service";
|
2021-05-13 02:58:59 +02:00
|
|
|
import { CryptoService } from "../abstractions/crypto.service";
|
2022-02-07 16:33:10 +01:00
|
|
|
import { CryptoFunctionService } from "../abstractions/cryptoFunction.service";
|
2022-02-23 04:02:07 +01:00
|
|
|
import {
|
|
|
|
ExportFormat,
|
|
|
|
ExportService as ExportServiceAbstraction,
|
|
|
|
} from "../abstractions/export.service";
|
2022-07-08 15:40:31 +02:00
|
|
|
import { FolderService } from "../abstractions/folder/folder.service.abstraction";
|
2022-02-22 15:39:11 +01:00
|
|
|
import { CipherType } from "../enums/cipherType";
|
2022-03-24 10:42:11 +01:00
|
|
|
import { DEFAULT_KDF_ITERATIONS, KdfType } from "../enums/kdfType";
|
2022-02-22 15:39:11 +01:00
|
|
|
import { Utils } from "../misc/utils";
|
2022-10-14 18:25:50 +02:00
|
|
|
import { CipherData } from "../models/data/cipher.data";
|
|
|
|
import { CollectionData } from "../models/data/collection.data";
|
2018-07-05 20:39:58 +02:00
|
|
|
import { Cipher } from "../models/domain/cipher";
|
|
|
|
import { Collection } from "../models/domain/collection";
|
2020-12-03 21:20:38 +01:00
|
|
|
import { Folder } from "../models/domain/folder";
|
2022-10-14 18:25:50 +02:00
|
|
|
import { CipherWithIdExport as CipherExport } from "../models/export/cipher-with-ids.export";
|
|
|
|
import { CollectionWithIdExport as CollectionExport } from "../models/export/collection-with-id.export";
|
|
|
|
import { EventExport } from "../models/export/event.export";
|
|
|
|
import { FolderWithIdExport as FolderExport } from "../models/export/folder-with-id.export";
|
2022-10-18 19:01:42 +02:00
|
|
|
import { CollectionDetailsResponse } from "../models/response/collection.response";
|
2022-10-14 18:25:50 +02:00
|
|
|
import { CipherView } from "../models/view/cipher.view";
|
|
|
|
import { CollectionView } from "../models/view/collection.view";
|
|
|
|
import { EventView } from "../models/view/event.view";
|
|
|
|
import { FolderView } from "../models/view/folder.view";
|
2021-05-13 02:58:59 +02:00
|
|
|
|
2018-05-17 16:52:06 +02:00
|
|
|
export class ExportService implements ExportServiceAbstraction {
|
2018-07-05 20:39:58 +02:00
|
|
|
constructor(
|
|
|
|
private folderService: FolderService,
|
|
|
|
private cipherService: CipherService,
|
2021-05-13 02:58:59 +02:00
|
|
|
private apiService: ApiService,
|
2022-02-07 16:33:10 +01:00
|
|
|
private cryptoService: CryptoService,
|
|
|
|
private cryptoFunctionService: CryptoFunctionService
|
2021-05-13 02:58:59 +02:00
|
|
|
) {}
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2022-02-23 04:02:07 +01:00
|
|
|
async getExport(format: ExportFormat = "csv", organizationId?: string): Promise<string> {
|
|
|
|
if (organizationId) {
|
|
|
|
return await this.getOrganizationExport(organizationId, format);
|
|
|
|
}
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
if (format === "encrypted_json") {
|
|
|
|
return this.getEncryptedExport();
|
|
|
|
} else {
|
|
|
|
return this.getDecryptedExport(format);
|
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 04:02:07 +01:00
|
|
|
async getPasswordProtectedExport(password: string, organizationId?: string): Promise<string> {
|
2022-02-07 16:33:10 +01:00
|
|
|
const clearText = organizationId
|
2022-02-23 04:02:07 +01:00
|
|
|
? await this.getOrganizationExport(organizationId, "json")
|
|
|
|
: await this.getExport("json");
|
2022-02-07 16:33:10 +01:00
|
|
|
|
|
|
|
const salt = Utils.fromBufferToB64(await this.cryptoFunctionService.randomBytes(16));
|
2022-03-24 10:42:11 +01:00
|
|
|
const kdfIterations = DEFAULT_KDF_ITERATIONS;
|
2022-02-07 16:33:10 +01:00
|
|
|
const key = await this.cryptoService.makePinKey(
|
|
|
|
password,
|
|
|
|
salt,
|
|
|
|
KdfType.PBKDF2_SHA256,
|
|
|
|
kdfIterations
|
|
|
|
);
|
|
|
|
|
|
|
|
const encKeyValidation = await this.cryptoService.encrypt(Utils.newGuid(), key);
|
|
|
|
const encText = await this.cryptoService.encrypt(clearText, key);
|
|
|
|
|
|
|
|
const jsonDoc: any = {
|
|
|
|
encrypted: true,
|
|
|
|
passwordProtected: true,
|
|
|
|
salt: salt,
|
|
|
|
kdfIterations: kdfIterations,
|
|
|
|
kdfType: KdfType.PBKDF2_SHA256,
|
|
|
|
encKeyValidation_DO_NOT_EDIT: encKeyValidation.encryptedString,
|
|
|
|
data: encText.encryptedString,
|
|
|
|
};
|
|
|
|
|
|
|
|
return JSON.stringify(jsonDoc, null, " ");
|
|
|
|
}
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
async getOrganizationExport(
|
|
|
|
organizationId: string,
|
2022-02-23 04:02:07 +01:00
|
|
|
format: ExportFormat = "csv"
|
2020-12-03 21:20:38 +01:00
|
|
|
): Promise<string> {
|
|
|
|
if (format === "encrypted_json") {
|
|
|
|
return this.getOrganizationEncryptedExport(organizationId);
|
|
|
|
} else {
|
|
|
|
return this.getOrganizationDecryptedExport(organizationId, format);
|
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2021-05-13 20:43:54 +02:00
|
|
|
async getEventExport(events: EventView[]): Promise<string> {
|
2022-04-19 13:03:04 +02:00
|
|
|
return papa.unparse(events.map((e) => new EventExport(e)));
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:11 +01:00
|
|
|
getFileName(prefix: string = null, extension = "csv"): string {
|
2020-12-03 21:20:38 +01:00
|
|
|
const now = new Date();
|
|
|
|
const dateString =
|
|
|
|
now.getFullYear() +
|
2021-12-16 13:36:21 +01:00
|
|
|
"" +
|
2020-12-03 21:20:38 +01:00
|
|
|
this.padNumber(now.getMonth() + 1, 2) +
|
2021-12-16 13:36:21 +01:00
|
|
|
"" +
|
2020-12-03 21:20:38 +01:00
|
|
|
this.padNumber(now.getDate(), 2) +
|
|
|
|
this.padNumber(now.getHours(), 2) +
|
2021-12-16 13:36:21 +01:00
|
|
|
"" +
|
2020-12-03 21:20:38 +01:00
|
|
|
this.padNumber(now.getMinutes(), 2) +
|
|
|
|
this.padNumber(now.getSeconds(), 2);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
return "bitwarden" + (prefix ? "_" + prefix : "") + "_export_" + dateString + "." + extension;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
private async getDecryptedExport(format: "json" | "csv"): Promise<string> {
|
2018-05-17 16:52:06 +02:00
|
|
|
let decFolders: FolderView[] = [];
|
|
|
|
let decCiphers: CipherView[] = [];
|
|
|
|
const promises = [];
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
promises.push(
|
2022-07-12 20:25:18 +02:00
|
|
|
firstValueFrom(this.folderService.folderViews$).then((folders) => {
|
2018-05-17 16:52:06 +02:00
|
|
|
decFolders = folders;
|
2021-12-16 13:36:21 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
promises.push(
|
|
|
|
this.cipherService.getAllDecrypted().then((ciphers) => {
|
2020-12-30 22:08:02 +01:00
|
|
|
decCiphers = ciphers.filter((f) => f.deletedDate == null);
|
2021-12-16 13:36:21 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2018-05-17 16:52:06 +02:00
|
|
|
await Promise.all(promises);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
if (format === "csv") {
|
2018-12-17 16:32:02 +01:00
|
|
|
const foldersMap = new Map<string, FolderView>();
|
2021-02-04 16:49:23 +01:00
|
|
|
decFolders.forEach((f) => {
|
2020-02-10 16:20:45 +01:00
|
|
|
if (f.id != null) {
|
|
|
|
foldersMap.set(f.id, f);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
});
|
2020-12-03 21:20:38 +01:00
|
|
|
|
2021-05-13 20:43:54 +02:00
|
|
|
const exportCiphers: any[] = [];
|
|
|
|
decCiphers.forEach((c) => {
|
|
|
|
// only export logins and secure notes
|
|
|
|
if (c.type !== CipherType.Login && c.type !== CipherType.SecureNote) {
|
|
|
|
return;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-12-17 16:32:02 +01:00
|
|
|
if (c.organizationId != null) {
|
2021-12-16 13:36:21 +01:00
|
|
|
return;
|
2021-05-13 20:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
const cipher: any = {};
|
|
|
|
cipher.folder =
|
|
|
|
c.folderId != null && foldersMap.has(c.folderId) ? foldersMap.get(c.folderId).name : null;
|
2018-12-17 16:32:02 +01:00
|
|
|
cipher.favorite = c.favorite ? 1 : null;
|
2020-12-03 21:20:38 +01:00
|
|
|
this.buildCommonCipher(cipher, c);
|
2018-12-17 16:32:02 +01:00
|
|
|
exportCiphers.push(cipher);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
return papa.unparse(exportCiphers);
|
2021-12-16 13:36:21 +01:00
|
|
|
} else {
|
2020-12-03 21:20:38 +01:00
|
|
|
const jsonDoc: any = {
|
|
|
|
encrypted: false,
|
2018-12-17 16:32:02 +01:00
|
|
|
folders: [],
|
2020-12-03 21:20:38 +01:00
|
|
|
items: [],
|
2021-12-16 13:36:21 +01:00
|
|
|
};
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
decFolders.forEach((f) => {
|
|
|
|
if (f.id == null) {
|
2021-12-16 13:36:21 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-12-03 21:20:38 +01:00
|
|
|
const folder = new FolderExport();
|
2018-12-17 16:32:02 +01:00
|
|
|
folder.build(f);
|
2020-12-03 21:20:38 +01:00
|
|
|
jsonDoc.folders.push(folder);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
decCiphers.forEach((c) => {
|
|
|
|
if (c.organizationId != null) {
|
|
|
|
return;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2020-12-03 21:20:38 +01:00
|
|
|
const cipher = new CipherExport();
|
|
|
|
cipher.build(c);
|
|
|
|
cipher.collectionIds = null;
|
|
|
|
jsonDoc.items.push(cipher);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
2020-12-03 21:20:38 +01:00
|
|
|
|
|
|
|
return JSON.stringify(jsonDoc, null, " ");
|
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
private async getEncryptedExport(): Promise<string> {
|
2018-05-17 16:52:06 +02:00
|
|
|
let folders: Folder[] = [];
|
|
|
|
let ciphers: Cipher[] = [];
|
|
|
|
const promises = [];
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
promises.push(
|
2022-07-12 20:25:18 +02:00
|
|
|
firstValueFrom(this.folderService.folders$).then((f) => {
|
2018-05-17 16:52:06 +02:00
|
|
|
folders = f;
|
|
|
|
})
|
|
|
|
);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
promises.push(
|
|
|
|
this.cipherService.getAll().then((c) => {
|
2020-12-30 22:08:02 +01:00
|
|
|
ciphers = c.filter((f) => f.deletedDate == null);
|
2018-05-17 16:52:06 +02:00
|
|
|
})
|
|
|
|
);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-05-17 16:52:06 +02:00
|
|
|
await Promise.all(promises);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
const encKeyValidation = await this.cryptoService.encrypt(Utils.newGuid());
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
const jsonDoc: any = {
|
2020-12-05 03:05:11 +01:00
|
|
|
encrypted: true,
|
2021-05-13 02:58:59 +02:00
|
|
|
encKeyValidation_DO_NOT_EDIT: encKeyValidation.encryptedString,
|
2020-12-03 21:20:38 +01:00
|
|
|
folders: [],
|
|
|
|
items: [],
|
2021-12-16 13:36:21 +01:00
|
|
|
};
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
folders.forEach((f) => {
|
|
|
|
if (f.id == null) {
|
2021-12-16 13:36:21 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-12-17 16:32:02 +01:00
|
|
|
const folder = new FolderExport();
|
2020-12-03 21:20:38 +01:00
|
|
|
folder.build(f);
|
2021-02-04 16:49:23 +01:00
|
|
|
jsonDoc.folders.push(folder);
|
2018-12-17 16:32:02 +01:00
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
ciphers.forEach((c) => {
|
2018-12-17 16:32:02 +01:00
|
|
|
if (c.organizationId != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const cipher = new CipherExport();
|
|
|
|
cipher.build(c);
|
|
|
|
cipher.collectionIds = null;
|
|
|
|
jsonDoc.items.push(cipher);
|
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
return JSON.stringify(jsonDoc, null, " ");
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
private async getOrganizationDecryptedExport(
|
2020-12-03 21:20:38 +01:00
|
|
|
organizationId: string,
|
|
|
|
format: "json" | "csv"
|
|
|
|
): Promise<string> {
|
2018-07-05 20:39:58 +02:00
|
|
|
const decCollections: CollectionView[] = [];
|
|
|
|
const decCiphers: CipherView[] = [];
|
|
|
|
const promises = [];
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
promises.push(
|
2022-07-25 10:56:03 +02:00
|
|
|
this.apiService.getOrganizationExport(organizationId).then((exportData) => {
|
|
|
|
const exportPromises: any = [];
|
|
|
|
if (exportData != null) {
|
|
|
|
if (
|
|
|
|
exportData.collections != null &&
|
|
|
|
exportData.collections.data != null &&
|
|
|
|
exportData.collections.data.length > 0
|
|
|
|
) {
|
|
|
|
exportData.collections.data.forEach((c) => {
|
|
|
|
const collection = new Collection(new CollectionData(c as CollectionDetailsResponse));
|
|
|
|
exportPromises.push(
|
|
|
|
collection.decrypt().then((decCol) => {
|
|
|
|
decCollections.push(decCol);
|
2021-12-16 13:36:21 +01:00
|
|
|
})
|
|
|
|
);
|
2018-12-17 16:32:02 +01:00
|
|
|
});
|
2022-07-25 10:56:03 +02:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
exportData.ciphers != null &&
|
|
|
|
exportData.ciphers.data != null &&
|
|
|
|
exportData.ciphers.data.length > 0
|
|
|
|
) {
|
|
|
|
exportData.ciphers.data
|
|
|
|
.filter((c) => c.deletedDate === null)
|
|
|
|
.forEach((c) => {
|
|
|
|
const cipher = new Cipher(new CipherData(c));
|
|
|
|
exportPromises.push(
|
|
|
|
cipher.decrypt().then((decCipher) => {
|
|
|
|
decCiphers.push(decCipher);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2018-07-05 20:39:58 +02:00
|
|
|
}
|
2022-07-25 10:56:03 +02:00
|
|
|
return Promise.all(exportPromises);
|
2021-12-16 13:36:21 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
await Promise.all(promises);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
if (format === "csv") {
|
2018-12-17 16:32:02 +01:00
|
|
|
const collectionsMap = new Map<string, CollectionView>();
|
2021-02-04 16:49:23 +01:00
|
|
|
decCollections.forEach((c) => {
|
2018-12-17 16:32:02 +01:00
|
|
|
collectionsMap.set(c.id, c);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2018-12-17 16:32:02 +01:00
|
|
|
const exportCiphers: any[] = [];
|
2021-02-04 16:49:23 +01:00
|
|
|
decCiphers.forEach((c) => {
|
2018-12-17 16:32:02 +01:00
|
|
|
// only export logins and secure notes
|
|
|
|
if (c.type !== CipherType.Login && c.type !== CipherType.SecureNote) {
|
2021-12-16 13:36:21 +01:00
|
|
|
return;
|
2018-07-05 20:39:58 +02:00
|
|
|
}
|
|
|
|
|
2020-12-30 22:08:02 +01:00
|
|
|
const cipher: any = {};
|
|
|
|
cipher.collections = [];
|
2018-12-17 16:32:02 +01:00
|
|
|
if (c.collectionIds != null) {
|
2020-12-30 22:08:02 +01:00
|
|
|
cipher.collections = c.collectionIds
|
2021-02-04 16:49:23 +01:00
|
|
|
.filter((id) => collectionsMap.has(id))
|
|
|
|
.map((id) => collectionsMap.get(id).name);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2021-02-04 16:49:23 +01:00
|
|
|
this.buildCommonCipher(cipher, c);
|
|
|
|
exportCiphers.push(cipher);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
return papa.unparse(exportCiphers);
|
2021-05-13 02:58:59 +02:00
|
|
|
} else {
|
2020-12-03 21:20:38 +01:00
|
|
|
const jsonDoc: any = {
|
2020-12-05 03:05:11 +01:00
|
|
|
encrypted: false,
|
2020-12-03 21:20:38 +01:00
|
|
|
collections: [],
|
|
|
|
items: [],
|
|
|
|
};
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
decCollections.forEach((c) => {
|
2020-12-03 21:20:38 +01:00
|
|
|
const collection = new CollectionExport();
|
|
|
|
collection.build(c);
|
|
|
|
jsonDoc.collections.push(collection);
|
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
decCiphers.forEach((c) => {
|
2020-12-03 21:20:38 +01:00
|
|
|
const cipher = new CipherExport();
|
|
|
|
cipher.build(c);
|
|
|
|
jsonDoc.items.push(cipher);
|
|
|
|
});
|
|
|
|
return JSON.stringify(jsonDoc, null, " ");
|
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
private async getOrganizationEncryptedExport(organizationId: string): Promise<string> {
|
2018-07-05 20:39:58 +02:00
|
|
|
const collections: Collection[] = [];
|
|
|
|
const ciphers: Cipher[] = [];
|
|
|
|
const promises = [];
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
promises.push(
|
|
|
|
this.apiService.getCollections(organizationId).then((c) => {
|
2018-07-05 20:49:48 +02:00
|
|
|
const collectionPromises: any = [];
|
2018-07-05 20:39:58 +02:00
|
|
|
if (c != null && c.data != null && c.data.length > 0) {
|
2021-02-04 16:49:23 +01:00
|
|
|
c.data.forEach((r) => {
|
2018-07-05 20:39:58 +02:00
|
|
|
const collection = new Collection(new CollectionData(r as CollectionDetailsResponse));
|
|
|
|
collections.push(collection);
|
2018-05-17 16:52:06 +02:00
|
|
|
});
|
|
|
|
}
|
2018-07-05 20:39:58 +02:00
|
|
|
return Promise.all(collectionPromises);
|
|
|
|
})
|
|
|
|
);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
promises.push(
|
2021-02-04 16:49:23 +01:00
|
|
|
this.apiService.getCiphersOrganization(organizationId).then((c) => {
|
2018-12-17 16:32:02 +01:00
|
|
|
const cipherPromises: any = [];
|
|
|
|
if (c != null && c.data != null && c.data.length > 0) {
|
2021-02-04 16:49:23 +01:00
|
|
|
c.data
|
|
|
|
.filter((item) => item.deletedDate === null)
|
|
|
|
.forEach((item) => {
|
2018-12-17 16:32:02 +01:00
|
|
|
const cipher = new Cipher(new CipherData(item));
|
|
|
|
ciphers.push(cipher);
|
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-07-05 20:39:58 +02:00
|
|
|
return Promise.all(cipherPromises);
|
2021-12-16 13:36:21 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
await Promise.all(promises);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-05-27 22:48:10 +02:00
|
|
|
const orgKey = await this.cryptoService.getOrgKey(organizationId);
|
|
|
|
const encKeyValidation = await this.cryptoService.encrypt(Utils.newGuid(), orgKey);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
const jsonDoc: any = {
|
2020-12-05 03:05:11 +01:00
|
|
|
encrypted: true,
|
2021-05-27 22:48:10 +02:00
|
|
|
encKeyValidation_DO_NOT_EDIT: encKeyValidation.encryptedString,
|
2020-12-03 21:20:38 +01:00
|
|
|
collections: [],
|
|
|
|
items: [],
|
2021-12-16 13:36:21 +01:00
|
|
|
};
|
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
collections.forEach((c) => {
|
2018-07-05 20:39:58 +02:00
|
|
|
const collection = new CollectionExport();
|
2020-12-03 21:20:38 +01:00
|
|
|
collection.build(c);
|
|
|
|
jsonDoc.collections.push(collection);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
ciphers.forEach((c) => {
|
2020-12-03 21:20:38 +01:00
|
|
|
const cipher = new CipherExport();
|
|
|
|
cipher.build(c);
|
|
|
|
jsonDoc.items.push(cipher);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
2020-12-03 21:20:38 +01:00
|
|
|
return JSON.stringify(jsonDoc, null, " ");
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:11 +01:00
|
|
|
private padNumber(num: number, width: number, padCharacter = "0"): string {
|
2018-05-17 16:52:06 +02:00
|
|
|
const numString = num.toString();
|
|
|
|
return numString.length >= width
|
|
|
|
? numString
|
2018-07-05 20:39:58 +02:00
|
|
|
: new Array(width - numString.length + 1).join(padCharacter) + numString;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
private buildCommonCipher(cipher: any, c: CipherView) {
|
|
|
|
cipher.type = null;
|
|
|
|
cipher.name = c.name;
|
|
|
|
cipher.notes = c.notes;
|
|
|
|
cipher.fields = null;
|
2021-05-05 09:54:17 +02:00
|
|
|
cipher.reprompt = c.reprompt;
|
2018-07-05 20:39:58 +02:00
|
|
|
// Login props
|
|
|
|
cipher.login_uri = null;
|
|
|
|
cipher.login_username = null;
|
|
|
|
cipher.login_password = null;
|
|
|
|
cipher.login_totp = null;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
if (c.fields) {
|
|
|
|
c.fields.forEach((f: any) => {
|
|
|
|
if (!cipher.fields) {
|
|
|
|
cipher.fields = "";
|
|
|
|
} else {
|
2018-12-17 16:32:02 +01:00
|
|
|
cipher.fields += "\n";
|
2018-07-05 20:39:58 +02:00
|
|
|
}
|
2018-05-17 16:52:06 +02:00
|
|
|
|
|
|
|
cipher.fields += (f.name || "") + ": " + f.value;
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
2018-05-17 16:52:06 +02:00
|
|
|
}
|
2018-07-05 20:39:58 +02:00
|
|
|
|
|
|
|
switch (c.type) {
|
|
|
|
case CipherType.Login:
|
|
|
|
cipher.type = "login";
|
|
|
|
cipher.login_username = c.login.username;
|
|
|
|
cipher.login_password = c.login.password;
|
|
|
|
cipher.login_totp = c.login.totp;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
if (c.login.uris) {
|
|
|
|
cipher.login_uri = [];
|
2021-02-04 16:49:23 +01:00
|
|
|
c.login.uris.forEach((u) => {
|
2018-07-05 20:39:58 +02:00
|
|
|
cipher.login_uri.push(u.uri);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CipherType.SecureNote:
|
|
|
|
cipher.type = "note";
|
2021-12-16 13:36:21 +01:00
|
|
|
break;
|
|
|
|
default:
|
2018-07-05 20:39:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
return cipher;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-05-17 16:52:06 +02:00
|
|
|
}
|