Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
support for encrypted json export (#216)
Browse files Browse the repository at this point in the history
* support for encrypted json export

* adjust filename prefix for encrypted formats

* flip if logic

* remove format param from encrypted export

* encryptedFormat getter
  • Loading branch information
kspearrin committed Dec 3, 2020
1 parent abb54f0 commit 93a3053
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/abstractions/export.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export abstract class ExportService {
getExport: (format?: 'csv' | 'json') => Promise<string>;
getOrganizationExport: (organizationId: string, format?: 'csv' | 'json') => Promise<string>;
getExport: (format?: 'csv' | 'json' | 'encrypted_json') => Promise<string>;
getOrganizationExport: (organizationId: string, format?: 'csv' | 'json' | 'encrypted_json') => Promise<string>;
getFileName: (prefix?: string, extension?: string) => string;
}
17 changes: 15 additions & 2 deletions src/angular/components/export.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ export class ExportComponent {

formPromise: Promise<string>;
masterPassword: string;
format: 'json' | 'csv' = 'json';
format: 'json' | 'encrypted_json' | 'csv' = 'json';
showPassword = false;

constructor(protected cryptoService: CryptoService, protected i18nService: I18nService,
protected platformUtilsService: PlatformUtilsService, protected exportService: ExportService,
protected eventService: EventService, protected win: Window) { }

get encryptedFormat() {
return this.format === 'encrypted_json';
}

async submit() {
if (this.masterPassword == null || this.masterPassword === '') {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
Expand Down Expand Up @@ -63,7 +67,16 @@ export class ExportComponent {
}

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> {
Expand Down
25 changes: 18 additions & 7 deletions src/models/export/card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CardView } from '../view/cardView';

import { Card as CardDomain } from '../domain/card';

export class Card {
static template(): Card {
const req = new Card();
Expand Down Expand Up @@ -29,16 +31,25 @@ export class Card {
expYear: string;
code: string;

constructor(o?: CardView) {
constructor(o?: CardView | CardDomain) {
if (o == null) {
return;
}

this.cardholderName = o.cardholderName;
this.brand = o.brand;
this.number = o.number;
this.expMonth = o.expMonth;
this.expYear = o.expYear;
this.code = o.code;
if (o instanceof CardView) {
this.cardholderName = o.cardholderName;
this.brand = o.brand;
this.number = o.number;
this.expMonth = o.expMonth;
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;
}
}
}
21 changes: 17 additions & 4 deletions src/models/export/cipher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { CipherType } from '../../enums/cipherType';

import { CipherView } from '../view/cipherView';

import { Cipher as CipherDomain } from '../domain/cipher';

import { Card } from './card';
import { Field } from './field';
import { Identity } from './identity';
Expand Down Expand Up @@ -70,16 +72,27 @@ export class Cipher {
identity: Identity;

// 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.folderId = o.folderId;
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;

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) {
Expand Down
4 changes: 3 additions & 1 deletion src/models/export/cipherWithIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Cipher } from './cipher';

import { CipherView } from '../view/cipherView';

import { Cipher as CipherDomain } from '../domain/cipher';

export class CipherWithIds extends Cipher {
id: string;
collectionIds: string[];

// 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;
super.build(o);
this.collectionIds = o.collectionIds;
Expand Down
10 changes: 8 additions & 2 deletions src/models/export/collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CollectionView } from '../view/collectionView';

import { Collection as CollectionDomain } from '../domain/collection';

export class Collection {
static template(): Collection {
const req = new Collection();
Expand All @@ -23,9 +25,13 @@ export class Collection {
externalId: string;

// 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.name = o.name;
if (o instanceof CollectionView) {
this.name = o.name;
} else {
this.name = o.name?.encryptedString;
}
this.externalId = o.externalId;
}
}
4 changes: 3 additions & 1 deletion src/models/export/collectionWithId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Collection } from './collection';

import { CollectionView } from '../view/collectionView';

import { Collection as CollectionDomain } from '../domain/collection';

export class CollectionWithId extends Collection {
id: string;

// 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;
super.build(o);
}
Expand Down
13 changes: 10 additions & 3 deletions src/models/export/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { FieldType } from '../../enums/fieldType';

import { FieldView } from '../view/fieldView';

import { Field as FieldDomain } from '../domain/field';

export class Field {
static template(): Field {
const req = new Field();
Expand All @@ -22,13 +24,18 @@ export class Field {
value: string;
type: FieldType;

constructor(o?: FieldView) {
constructor(o?: FieldView | FieldDomain) {
if (o == null) {
return;
}

this.name = o.name;
this.value = o.value;
if (o instanceof FieldView) {
this.name = o.name;
this.value = o.value;
} else {
this.name = o.name?.encryptedString;
this.value = o.value?.encryptedString;
}
this.type = o.type;
}
}
10 changes: 8 additions & 2 deletions src/models/export/folder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FolderView } from '../view/folderView';

import { Folder as FolderDomain } from '../domain/folder';

export class Folder {
static template(): Folder {
const req = new Folder();
Expand All @@ -15,7 +17,11 @@ export class Folder {
name: string;

// Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: FolderView) {
this.name = o.name;
build(o: FolderView | FolderDomain) {
if (o instanceof FolderView) {
this.name = o.name;
} else {
this.name = o.name?.encryptedString;
}
}
}
4 changes: 3 additions & 1 deletion src/models/export/folderWithId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Folder } from './folder';

import { FolderView } from '../view/folderView';

import { Folder as FolderDomain } from '../domain/folder';

export class FolderWithId extends Folder {
id: string;

// 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;
super.build(o);
}
Expand Down
61 changes: 42 additions & 19 deletions src/models/export/identity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { IdentityView } from '../view/identityView';

import { Identity as IdentityDomain } from '../domain/identity';

export class Identity {
static template(): Identity {
const req = new Identity();
Expand Down Expand Up @@ -65,28 +67,49 @@ export class Identity {
passportNumber: string;
licenseNumber: string;

constructor(o?: IdentityView) {
constructor(o?: IdentityView | IdentityDomain) {
if (o == null) {
return;
}

this.title = o.title;
this.firstName = o.firstName;
this.middleName = o.middleName;
this.lastName = o.lastName;
this.address1 = o.address1;
this.address2 = o.address2;
this.address3 = o.address3;
this.city = o.city;
this.state = o.state;
this.postalCode = o.postalCode;
this.country = o.country;
this.company = o.company;
this.email = o.email;
this.phone = o.phone;
this.ssn = o.ssn;
this.username = o.username;
this.passportNumber = o.passportNumber;
this.licenseNumber = o.licenseNumber;
if (o instanceof IdentityView) {
this.title = o.title;
this.firstName = o.firstName;
this.middleName = o.middleName;
this.lastName = o.lastName;
this.address1 = o.address1;
this.address2 = o.address2;
this.address3 = o.address3;
this.city = o.city;
this.state = o.state;
this.postalCode = o.postalCode;
this.country = o.country;
this.company = o.company;
this.email = o.email;
this.phone = o.phone;
this.ssn = o.ssn;
this.username = o.username;
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;
}
}
}
22 changes: 17 additions & 5 deletions src/models/export/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { LoginUri } from './loginUri';

import { LoginView } from '../view/loginView';

import { Login as LoginDomain } from '../domain/login';

export class Login {
static template(): Login {
const req = new Login();
Expand All @@ -27,17 +29,27 @@ export class Login {
password: string;
totp: string;

constructor(o?: LoginView) {
constructor(o?: LoginView | LoginDomain) {
if (o == null) {
return;
}

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;
this.password = o.password;
this.totp = o.totp;
if (o instanceof LoginView) {
this.username = o.username;
this.password = o.password;
this.totp = o.totp;
} else {
this.username = o.username?.encryptedString;
this.password = o.password?.encryptedString;
this.totp = o.totp?.encryptedString;
}
}
}
10 changes: 8 additions & 2 deletions src/models/export/loginUri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { UriMatchType } from '../../enums/uriMatchType';

import { LoginUriView } from '../view/loginUriView';

import { LoginUri as LoginUriDomain } from '../domain/loginUri';

export class LoginUri {
static template(): LoginUri {
const req = new LoginUri();
Expand All @@ -19,12 +21,16 @@ export class LoginUri {
uri: string;
match: UriMatchType = null;

constructor(o?: LoginUriView) {
constructor(o?: LoginUriView | LoginUriDomain) {
if (o == null) {
return;
}

this.uri = o.uri;
if (o instanceof LoginUriView) {
this.uri = o.uri;
} else {
this.uri = o.uri?.encryptedString;
}
this.match = o.match;
}
}
Loading

0 comments on commit 93a3053

Please sign in to comment.