mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 03:03:43 +00:00
encrypted import for bitwarden json (#220)
This commit is contained in:
@@ -8,37 +8,106 @@ import { CollectionWithId } from '../models/export/collectionWithId';
|
||||
import { FolderWithId } from '../models/export/folderWithId';
|
||||
|
||||
export class BitwardenJsonImporter extends BaseImporter implements Importer {
|
||||
parse(data: string): ImportResult {
|
||||
const result = new ImportResult();
|
||||
const results = JSON.parse(data);
|
||||
if (results == null || results.items == null || results.items.length === 0) {
|
||||
result.success = false;
|
||||
return result;
|
||||
private results: any;
|
||||
private result: ImportResult;
|
||||
|
||||
async parse(data: string): Promise<ImportResult> {
|
||||
this.result = new ImportResult();
|
||||
this.results = JSON.parse(data);
|
||||
if (this.results == null || this.results.items == null || this.results.items.length === 0) {
|
||||
this.result.success = false;
|
||||
return this.result;
|
||||
}
|
||||
|
||||
if (this.results.encrypted) {
|
||||
await this.parseEncrypted();
|
||||
} else {
|
||||
this.parseDecrypted();
|
||||
}
|
||||
|
||||
this.result.success = true;
|
||||
return this.result;
|
||||
}
|
||||
|
||||
private async parseEncrypted() {
|
||||
const groupingsMap = new Map<string, number>();
|
||||
if (this.organization && results.collections != null) {
|
||||
results.collections.forEach((c: CollectionWithId) => {
|
||||
|
||||
if (this.organization && this.results.collections != null) {
|
||||
for (const c of this.results.collections as CollectionWithId[]) {
|
||||
const collection = CollectionWithId.toDomain(c);
|
||||
if (collection != null) {
|
||||
collection.id = null;
|
||||
collection.organizationId = this.organizationId;
|
||||
const view = await collection.decrypt();
|
||||
groupingsMap.set(c.id, this.result.collections.length);
|
||||
this.result.collections.push(view);
|
||||
}
|
||||
}
|
||||
} else if (!this.organization && this.results.folders != null) {
|
||||
for (const f of this.results.folders as FolderWithId[]) {
|
||||
const folder = FolderWithId.toDomain(f);
|
||||
if (folder != null) {
|
||||
folder.id = null;
|
||||
const view = await folder.decrypt();
|
||||
groupingsMap.set(f.id, this.result.folders.length);
|
||||
this.result.folders.push(view);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const c of this.results.items as CipherWithIds[]) {
|
||||
const cipher = CipherWithIds.toDomain(c);
|
||||
// reset ids incase they were set for some reason
|
||||
cipher.id = null;
|
||||
cipher.folderId = 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)]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const view = await cipher.decrypt();
|
||||
this.cleanupCipher(view);
|
||||
this.result.ciphers.push(view);
|
||||
}
|
||||
}
|
||||
|
||||
private parseDecrypted() {
|
||||
const groupingsMap = new Map<string, number>();
|
||||
if (this.organization && this.results.collections != null) {
|
||||
this.results.collections.forEach((c: CollectionWithId) => {
|
||||
const collection = CollectionWithId.toView(c);
|
||||
if (collection != null) {
|
||||
collection.id = null;
|
||||
collection.organizationId = null;
|
||||
groupingsMap.set(c.id, result.collections.length);
|
||||
result.collections.push(collection);
|
||||
groupingsMap.set(c.id, this.result.collections.length);
|
||||
this.result.collections.push(collection);
|
||||
}
|
||||
});
|
||||
} else if (!this.organization && results.folders != null) {
|
||||
results.folders.forEach((f: FolderWithId) => {
|
||||
} else if (!this.organization && this.results.folders != null) {
|
||||
this.results.folders.forEach((f: FolderWithId) => {
|
||||
const folder = FolderWithId.toView(f);
|
||||
if (folder != null) {
|
||||
folder.id = null;
|
||||
groupingsMap.set(f.id, result.folders.length);
|
||||
result.folders.push(folder);
|
||||
groupingsMap.set(f.id, this.result.folders.length);
|
||||
this.result.folders.push(folder);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
results.items.forEach((c: CipherWithIds) => {
|
||||
this.results.items.forEach((c: CipherWithIds) => {
|
||||
const cipher = CipherWithIds.toView(c);
|
||||
// reset ids incase they were set for some reason
|
||||
cipher.id = null;
|
||||
@@ -52,20 +121,17 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer {
|
||||
}
|
||||
|
||||
if (!this.organization && c.folderId != null && groupingsMap.has(c.folderId)) {
|
||||
result.folderRelationships.push([result.ciphers.length, groupingsMap.get(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)) {
|
||||
result.collectionRelationships.push([result.ciphers.length, groupingsMap.get(cId)]);
|
||||
this.result.collectionRelationships.push([this.result.ciphers.length, groupingsMap.get(cId)]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.cleanupCipher(cipher);
|
||||
result.ciphers.push(cipher);
|
||||
this.result.ciphers.push(cipher);
|
||||
});
|
||||
|
||||
result.success = true;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user