1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 16:23:44 +00:00

support for encrypted json export (#216)

* support for encrypted json export

* adjust filename prefix for encrypted formats

* flip if logic

* remove format param from encrypted export

* encryptedFormat getter
This commit is contained in:
Kyle Spearrin
2020-12-03 15:20:38 -05:00
committed by GitHub
parent abb54f0073
commit 93a3053f54
15 changed files with 263 additions and 61 deletions

View File

@@ -1,5 +1,5 @@
export abstract class ExportService { export abstract class ExportService {
getExport: (format?: 'csv' | 'json') => Promise<string>; getExport: (format?: 'csv' | 'json' | 'encrypted_json') => Promise<string>;
getOrganizationExport: (organizationId: string, format?: 'csv' | 'json') => Promise<string>; getOrganizationExport: (organizationId: string, format?: 'csv' | 'json' | 'encrypted_json') => Promise<string>;
getFileName: (prefix?: string, extension?: string) => string; getFileName: (prefix?: string, extension?: string) => string;
} }

View File

@@ -17,13 +17,17 @@ export class ExportComponent {
formPromise: Promise<string>; formPromise: Promise<string>;
masterPassword: string; masterPassword: string;
format: 'json' | 'csv' = 'json'; format: 'json' | 'encrypted_json' | 'csv' = 'json';
showPassword = false; showPassword = false;
constructor(protected cryptoService: CryptoService, protected i18nService: I18nService, constructor(protected cryptoService: CryptoService, protected i18nService: I18nService,
protected platformUtilsService: PlatformUtilsService, protected exportService: ExportService, protected platformUtilsService: PlatformUtilsService, protected exportService: ExportService,
protected eventService: EventService, protected win: Window) { } protected eventService: EventService, protected win: Window) { }
get encryptedFormat() {
return this.format === 'encrypted_json';
}
async submit() { async submit() {
if (this.masterPassword == null || this.masterPassword === '') { if (this.masterPassword == null || this.masterPassword === '') {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
@@ -63,7 +67,16 @@ export class ExportComponent {
} }
protected getFileName(prefix?: string) { protected getFileName(prefix?: string) {
return this.exportService.getFileName(prefix, this.format); let extension = this.format;
if (this.format === 'encrypted_json') {
if (prefix == null) {
prefix = 'encrypted';
} else {
prefix = 'encrypted_' + prefix;
}
extension = 'json';
}
return this.exportService.getFileName(prefix, extension);
} }
protected async collectEvent(): Promise<any> { protected async collectEvent(): Promise<any> {

View File

@@ -1,5 +1,7 @@
import { CardView } from '../view/cardView'; import { CardView } from '../view/cardView';
import { Card as CardDomain } from '../domain/card';
export class Card { export class Card {
static template(): Card { static template(): Card {
const req = new Card(); const req = new Card();
@@ -29,16 +31,25 @@ export class Card {
expYear: string; expYear: string;
code: string; code: string;
constructor(o?: CardView) { constructor(o?: CardView | CardDomain) {
if (o == null) { if (o == null) {
return; return;
} }
this.cardholderName = o.cardholderName; if (o instanceof CardView) {
this.brand = o.brand; this.cardholderName = o.cardholderName;
this.number = o.number; this.brand = o.brand;
this.expMonth = o.expMonth; this.number = o.number;
this.expYear = o.expYear; this.expMonth = o.expMonth;
this.code = o.code; this.expYear = o.expYear;
this.code = o.code;
} else {
this.cardholderName = o.cardholderName?.encryptedString;
this.brand = o.brand?.encryptedString;
this.number = o.number?.encryptedString;
this.expMonth = o.expMonth?.encryptedString;
this.expYear = o.expYear?.encryptedString;
this.code = o.code?.encryptedString;
}
} }
} }

View File

@@ -2,6 +2,8 @@ import { CipherType } from '../../enums/cipherType';
import { CipherView } from '../view/cipherView'; import { CipherView } from '../view/cipherView';
import { Cipher as CipherDomain } from '../domain/cipher';
import { Card } from './card'; import { Card } from './card';
import { Field } from './field'; import { Field } from './field';
import { Identity } from './identity'; import { Identity } from './identity';
@@ -70,16 +72,27 @@ export class Cipher {
identity: Identity; identity: Identity;
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print // Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: CipherView) { build(o: CipherView | CipherDomain) {
this.organizationId = o.organizationId; this.organizationId = o.organizationId;
this.folderId = o.folderId; this.folderId = o.folderId;
this.type = o.type; this.type = o.type;
this.name = o.name;
this.notes = o.notes; if (o instanceof CipherView) {
this.name = o.name;
this.notes = o.notes;
} else {
this.name = o.name?.encryptedString;
this.notes = o.notes?.encryptedString;
}
this.favorite = o.favorite; this.favorite = o.favorite;
if (o.fields != null) { if (o.fields != null) {
this.fields = o.fields.map((f) => new Field(f)); if (o instanceof CipherView) {
this.fields = o.fields.map((f) => new Field(f));
} else {
this.fields = o.fields.map((f) => new Field(f));
}
} }
switch (o.type) { switch (o.type) {

View File

@@ -2,12 +2,14 @@ import { Cipher } from './cipher';
import { CipherView } from '../view/cipherView'; import { CipherView } from '../view/cipherView';
import { Cipher as CipherDomain } from '../domain/cipher';
export class CipherWithIds extends Cipher { export class CipherWithIds extends Cipher {
id: string; id: string;
collectionIds: string[]; collectionIds: string[];
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print // Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: CipherView) { build(o: CipherView | CipherDomain) {
this.id = o.id; this.id = o.id;
super.build(o); super.build(o);
this.collectionIds = o.collectionIds; this.collectionIds = o.collectionIds;

View File

@@ -1,5 +1,7 @@
import { CollectionView } from '../view/collectionView'; import { CollectionView } from '../view/collectionView';
import { Collection as CollectionDomain } from '../domain/collection';
export class Collection { export class Collection {
static template(): Collection { static template(): Collection {
const req = new Collection(); const req = new Collection();
@@ -23,9 +25,13 @@ export class Collection {
externalId: string; externalId: string;
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print // Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: CollectionView) { build(o: CollectionView | CollectionDomain) {
this.organizationId = o.organizationId; this.organizationId = o.organizationId;
this.name = o.name; if (o instanceof CollectionView) {
this.name = o.name;
} else {
this.name = o.name?.encryptedString;
}
this.externalId = o.externalId; this.externalId = o.externalId;
} }
} }

View File

@@ -2,11 +2,13 @@ import { Collection } from './collection';
import { CollectionView } from '../view/collectionView'; import { CollectionView } from '../view/collectionView';
import { Collection as CollectionDomain } from '../domain/collection';
export class CollectionWithId extends Collection { export class CollectionWithId extends Collection {
id: string; id: string;
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print // Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: CollectionView) { build(o: CollectionView | CollectionDomain) {
this.id = o.id; this.id = o.id;
super.build(o); super.build(o);
} }

View File

@@ -2,6 +2,8 @@ import { FieldType } from '../../enums/fieldType';
import { FieldView } from '../view/fieldView'; import { FieldView } from '../view/fieldView';
import { Field as FieldDomain } from '../domain/field';
export class Field { export class Field {
static template(): Field { static template(): Field {
const req = new Field(); const req = new Field();
@@ -22,13 +24,18 @@ export class Field {
value: string; value: string;
type: FieldType; type: FieldType;
constructor(o?: FieldView) { constructor(o?: FieldView | FieldDomain) {
if (o == null) { if (o == null) {
return; return;
} }
this.name = o.name; if (o instanceof FieldView) {
this.value = o.value; this.name = o.name;
this.value = o.value;
} else {
this.name = o.name?.encryptedString;
this.value = o.value?.encryptedString;
}
this.type = o.type; this.type = o.type;
} }
} }

View File

@@ -1,5 +1,7 @@
import { FolderView } from '../view/folderView'; import { FolderView } from '../view/folderView';
import { Folder as FolderDomain } from '../domain/folder';
export class Folder { export class Folder {
static template(): Folder { static template(): Folder {
const req = new Folder(); const req = new Folder();
@@ -15,7 +17,11 @@ export class Folder {
name: string; name: string;
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print // Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: FolderView) { build(o: FolderView | FolderDomain) {
this.name = o.name; if (o instanceof FolderView) {
this.name = o.name;
} else {
this.name = o.name?.encryptedString;
}
} }
} }

View File

@@ -2,11 +2,13 @@ import { Folder } from './folder';
import { FolderView } from '../view/folderView'; import { FolderView } from '../view/folderView';
import { Folder as FolderDomain } from '../domain/folder';
export class FolderWithId extends Folder { export class FolderWithId extends Folder {
id: string; id: string;
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print // Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: FolderView) { build(o: FolderView | FolderDomain) {
this.id = o.id; this.id = o.id;
super.build(o); super.build(o);
} }

View File

@@ -1,5 +1,7 @@
import { IdentityView } from '../view/identityView'; import { IdentityView } from '../view/identityView';
import { Identity as IdentityDomain } from '../domain/identity';
export class Identity { export class Identity {
static template(): Identity { static template(): Identity {
const req = new Identity(); const req = new Identity();
@@ -65,28 +67,49 @@ export class Identity {
passportNumber: string; passportNumber: string;
licenseNumber: string; licenseNumber: string;
constructor(o?: IdentityView) { constructor(o?: IdentityView | IdentityDomain) {
if (o == null) { if (o == null) {
return; return;
} }
this.title = o.title; if (o instanceof IdentityView) {
this.firstName = o.firstName; this.title = o.title;
this.middleName = o.middleName; this.firstName = o.firstName;
this.lastName = o.lastName; this.middleName = o.middleName;
this.address1 = o.address1; this.lastName = o.lastName;
this.address2 = o.address2; this.address1 = o.address1;
this.address3 = o.address3; this.address2 = o.address2;
this.city = o.city; this.address3 = o.address3;
this.state = o.state; this.city = o.city;
this.postalCode = o.postalCode; this.state = o.state;
this.country = o.country; this.postalCode = o.postalCode;
this.company = o.company; this.country = o.country;
this.email = o.email; this.company = o.company;
this.phone = o.phone; this.email = o.email;
this.ssn = o.ssn; this.phone = o.phone;
this.username = o.username; this.ssn = o.ssn;
this.passportNumber = o.passportNumber; this.username = o.username;
this.licenseNumber = o.licenseNumber; this.passportNumber = o.passportNumber;
this.licenseNumber = o.licenseNumber;
} else {
this.title = o.title?.encryptedString;
this.firstName = o.firstName?.encryptedString;
this.middleName = o.middleName?.encryptedString;
this.lastName = o.lastName?.encryptedString;
this.address1 = o.address1?.encryptedString;
this.address2 = o.address2?.encryptedString;
this.address3 = o.address3?.encryptedString;
this.city = o.city?.encryptedString;
this.state = o.state?.encryptedString;
this.postalCode = o.postalCode?.encryptedString;
this.country = o.country?.encryptedString;
this.company = o.company?.encryptedString;
this.email = o.email?.encryptedString;
this.phone = o.phone?.encryptedString;
this.ssn = o.ssn?.encryptedString;
this.username = o.username?.encryptedString;
this.passportNumber = o.passportNumber?.encryptedString;
this.licenseNumber = o.licenseNumber?.encryptedString;
}
} }
} }

View File

@@ -2,6 +2,8 @@ import { LoginUri } from './loginUri';
import { LoginView } from '../view/loginView'; import { LoginView } from '../view/loginView';
import { Login as LoginDomain } from '../domain/login';
export class Login { export class Login {
static template(): Login { static template(): Login {
const req = new Login(); const req = new Login();
@@ -27,17 +29,27 @@ export class Login {
password: string; password: string;
totp: string; totp: string;
constructor(o?: LoginView) { constructor(o?: LoginView | LoginDomain) {
if (o == null) { if (o == null) {
return; return;
} }
if (o.uris != null) { if (o.uris != null) {
this.uris = o.uris.map((u) => new LoginUri(u)); if (o instanceof LoginView) {
this.uris = o.uris.map((u) => new LoginUri(u));
} else {
this.uris = o.uris.map((u) => new LoginUri(u));
}
} }
this.username = o.username; if (o instanceof LoginView) {
this.password = o.password; this.username = o.username;
this.totp = o.totp; this.password = o.password;
this.totp = o.totp;
} else {
this.username = o.username?.encryptedString;
this.password = o.password?.encryptedString;
this.totp = o.totp?.encryptedString;
}
} }
} }

View File

@@ -2,6 +2,8 @@ import { UriMatchType } from '../../enums/uriMatchType';
import { LoginUriView } from '../view/loginUriView'; import { LoginUriView } from '../view/loginUriView';
import { LoginUri as LoginUriDomain } from '../domain/loginUri';
export class LoginUri { export class LoginUri {
static template(): LoginUri { static template(): LoginUri {
const req = new LoginUri(); const req = new LoginUri();
@@ -19,12 +21,16 @@ export class LoginUri {
uri: string; uri: string;
match: UriMatchType = null; match: UriMatchType = null;
constructor(o?: LoginUriView) { constructor(o?: LoginUriView | LoginUriDomain) {
if (o == null) { if (o == null) {
return; return;
} }
this.uri = o.uri; if (o instanceof LoginUriView) {
this.uri = o.uri;
} else {
this.uri = o.uri?.encryptedString;
}
this.match = o.match; this.match = o.match;
} }
} }

View File

@@ -2,6 +2,8 @@ import { SecureNoteType } from '../../enums/secureNoteType';
import { SecureNoteView } from '../view/secureNoteView'; import { SecureNoteView } from '../view/secureNoteView';
import { SecureNote as SecureNoteDomain } from '../domain/secureNote';
export class SecureNote { export class SecureNote {
static template(): SecureNote { static template(): SecureNote {
const req = new SecureNote(); const req = new SecureNote();
@@ -16,7 +18,7 @@ export class SecureNote {
type: SecureNoteType; type: SecureNoteType;
constructor(o?: SecureNoteView) { constructor(o?: SecureNoteView | SecureNoteDomain) {
if (o == null) { if (o == null) {
return; return;
} }

View File

@@ -13,6 +13,7 @@ import { FolderView } from '../models/view/folderView';
import { Cipher } from '../models/domain/cipher'; import { Cipher } from '../models/domain/cipher';
import { Collection } from '../models/domain/collection'; import { Collection } from '../models/domain/collection';
import { Folder } from '../models/domain/folder';
import { CipherData } from '../models/data/cipherData'; import { CipherData } from '../models/data/cipherData';
import { CollectionData } from '../models/data/collectionData'; import { CollectionData } from '../models/data/collectionData';
@@ -26,7 +27,34 @@ export class ExportService implements ExportServiceAbstraction {
constructor(private folderService: FolderService, private cipherService: CipherService, constructor(private folderService: FolderService, private cipherService: CipherService,
private apiService: ApiService) { } private apiService: ApiService) { }
async getExport(format: 'csv' | 'json' = 'csv'): Promise<string> { async getExport(format: 'csv' | 'json' | 'encrypted_json' = 'csv'): Promise<string> {
if (format === 'encrypted_json') {
return this.getEncryptedExport();
} else {
return this.getDecryptedExport(format);
}
}
async getOrganizationExport(organizationId: string,
format: 'csv' | 'json' | 'encrypted_json' = 'csv'): Promise<string> {
if (format === 'encrypted_json') {
return this.getOrganizationEncryptedExport(organizationId);
} else {
return this.getOrganizationDecryptedExport(organizationId, format);
}
}
getFileName(prefix: string = null, extension: string = 'csv'): string {
const now = new Date();
const dateString =
now.getFullYear() + '' + this.padNumber(now.getMonth() + 1, 2) + '' + this.padNumber(now.getDate(), 2) +
this.padNumber(now.getHours(), 2) + '' + this.padNumber(now.getMinutes(), 2) +
this.padNumber(now.getSeconds(), 2);
return 'bitwarden' + (prefix ? ('_' + prefix) : '') + '_export_' + dateString + '.' + extension;
}
private async getDecryptedExport(format: 'json' | 'csv'): Promise<string> {
let decFolders: FolderView[] = []; let decFolders: FolderView[] = [];
let decCiphers: CipherView[] = []; let decCiphers: CipherView[] = [];
const promises = []; const promises = [];
@@ -97,7 +125,38 @@ export class ExportService implements ExportServiceAbstraction {
} }
} }
async getOrganizationExport(organizationId: string, format: 'csv' | 'json' = 'csv'): Promise<string> { private async getEncryptedExport(): Promise<string> {
const folders = await this.folderService.getAll();
const ciphers = await this.cipherService.getAll();
const jsonDoc: any = {
folders: [],
items: [],
};
folders.forEach((f) => {
if (f.id == null) {
return;
}
const folder = new FolderExport();
folder.build(f);
jsonDoc.folders.push(folder);
});
ciphers.forEach((c) => {
if (c.organizationId != null) {
return;
}
const cipher = new CipherExport();
cipher.build(c);
cipher.collectionIds = null;
jsonDoc.items.push(cipher);
});
return JSON.stringify(jsonDoc, null, ' ');
}
private async getOrganizationDecryptedExport(organizationId: string, format: 'json' | 'csv'): Promise<string> {
const decCollections: CollectionView[] = []; const decCollections: CollectionView[] = [];
const decCiphers: CipherView[] = []; const decCiphers: CipherView[] = [];
const promises = []; const promises = [];
@@ -175,14 +234,52 @@ export class ExportService implements ExportServiceAbstraction {
} }
} }
getFileName(prefix: string = null, extension: string = 'csv'): string { private async getOrganizationEncryptedExport(organizationId: string): Promise<string> {
const now = new Date(); const collections: Collection[] = [];
const dateString = const ciphers: Cipher[] = [];
now.getFullYear() + '' + this.padNumber(now.getMonth() + 1, 2) + '' + this.padNumber(now.getDate(), 2) + const promises = [];
this.padNumber(now.getHours(), 2) + '' + this.padNumber(now.getMinutes(), 2) +
this.padNumber(now.getSeconds(), 2);
return 'bitwarden' + (prefix ? ('_' + prefix) : '') + '_export_' + dateString + '.' + extension; promises.push(this.apiService.getCollections(organizationId).then((c) => {
const collectionPromises: any = [];
if (c != null && c.data != null && c.data.length > 0) {
c.data.forEach((r) => {
const collection = new Collection(new CollectionData(r as CollectionDetailsResponse));
collections.push(collection);
});
}
return Promise.all(collectionPromises);
}));
promises.push(this.apiService.getCiphersOrganization(organizationId).then((c) => {
const cipherPromises: any = [];
if (c != null && c.data != null && c.data.length > 0) {
c.data.forEach((r) => {
const cipher = new Cipher(new CipherData(r));
ciphers.push(cipher);
});
}
return Promise.all(cipherPromises);
}));
await Promise.all(promises);
const jsonDoc: any = {
collections: [],
items: [],
};
collections.forEach((c) => {
const collection = new CollectionExport();
collection.build(c);
jsonDoc.collections.push(collection);
});
ciphers.forEach((c) => {
const cipher = new CipherExport();
cipher.build(c);
jsonDoc.items.push(cipher);
});
return JSON.stringify(jsonDoc, null, ' ');
} }
private padNumber(num: number, width: number, padCharacter: string = '0'): string { private padNumber(num: number, width: number, padCharacter: string = '0'): string {