From 3869dcaf7b4933d113181284d25bcf8f215695e7 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 25 Jan 2018 14:57:42 -0500 Subject: [PATCH] apply collection and folder views --- src/abstractions/collection.service.ts | 6 ++++-- src/abstractions/crypto.service.ts | 3 ++- src/abstractions/folder.service.ts | 8 +++++--- src/models/domain/collection.ts | 11 ++++------- src/models/domain/folder.ts | 10 ++++------ src/models/view/collectionView.ts | 18 ++++++++++++++++++ src/models/view/folderView.ts | 16 ++++++++++++++++ src/models/view/secureNoteView.ts | 6 +++++- src/services/collection.service.ts | 18 +++++++++--------- src/services/folder.service.ts | 14 +++++++------- 10 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 src/models/view/collectionView.ts create mode 100644 src/models/view/folderView.ts diff --git a/src/abstractions/collection.service.ts b/src/abstractions/collection.service.ts index 0f7f25e7ab..6c51478555 100644 --- a/src/abstractions/collection.service.ts +++ b/src/abstractions/collection.service.ts @@ -2,13 +2,15 @@ import { CollectionData } from '../models/data/collectionData'; import { Collection } from '../models/domain/collection'; +import { CollectionView } from '../models/view/collectionView'; + export abstract class CollectionService { - decryptedCollectionCache: any[]; + decryptedCollectionCache: CollectionView[]; clearCache: () => void; get: (id: string) => Promise; getAll: () => Promise; - getAllDecrypted: () => Promise; + getAllDecrypted: () => Promise; upsert: (collection: CollectionData | CollectionData[]) => Promise; replace: (collections: { [id: string]: CollectionData; }) => Promise; clear: (userId: string) => Promise; diff --git a/src/abstractions/crypto.service.ts b/src/abstractions/crypto.service.ts index b3fb5340b7..dbf5f0da75 100644 --- a/src/abstractions/crypto.service.ts +++ b/src/abstractions/crypto.service.ts @@ -25,7 +25,8 @@ export abstract class CryptoService { makeKey: (password: string, salt: string) => SymmetricCryptoKey; hashPassword: (password: string, key: SymmetricCryptoKey) => Promise; makeEncKey: (key: SymmetricCryptoKey) => Promise; - encrypt: (plainValue: string | Uint8Array, key?: SymmetricCryptoKey, plainValueEncoding?: string) => Promise; + encrypt: (plainValue: string | Uint8Array, key?: SymmetricCryptoKey, + plainValueEncoding?: string) => Promise; encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise; decrypt: (cipherString: CipherString, key?: SymmetricCryptoKey, outputEncoding?: string) => Promise; decryptFromBytes: (encBuf: ArrayBuffer, key: SymmetricCryptoKey) => Promise; diff --git a/src/abstractions/folder.service.ts b/src/abstractions/folder.service.ts index 7b75f6aebc..381bc01317 100644 --- a/src/abstractions/folder.service.ts +++ b/src/abstractions/folder.service.ts @@ -2,14 +2,16 @@ import { FolderData } from '../models/data/folderData'; import { Folder } from '../models/domain/folder'; +import { FolderView } from '../models/view/folderView'; + export abstract class FolderService { - decryptedFolderCache: any[]; + decryptedFolderCache: FolderView[]; clearCache: () => void; - encrypt: (model: any) => Promise; + encrypt: (model: FolderView) => Promise; get: (id: string) => Promise; getAll: () => Promise; - getAllDecrypted: () => Promise; + getAllDecrypted: () => Promise; saveWithServer: (folder: Folder) => Promise; upsert: (folder: FolderData | FolderData[]) => Promise; replace: (folders: { [id: string]: FolderData; }) => Promise; diff --git a/src/models/domain/collection.ts b/src/models/domain/collection.ts index 4857ec2c44..45fd54cd8c 100644 --- a/src/models/domain/collection.ts +++ b/src/models/domain/collection.ts @@ -1,5 +1,7 @@ import { CollectionData } from '../data/collectionData'; +import { CollectionView } from '../view/collectionView'; + import { CipherString } from './cipherString'; import Domain from './domain'; @@ -21,13 +23,8 @@ export class Collection extends Domain { }, alreadyEncrypted, ['id', 'organizationId']); } - decrypt(): Promise { - const model = { - id: this.id, - organizationId: this.organizationId, - }; - - return this.decryptObj(model, { + decrypt(): Promise { + return this.decryptObj(new CollectionView(this), { name: null, }, this.organizationId); } diff --git a/src/models/domain/folder.ts b/src/models/domain/folder.ts index f25d1f8314..77b9fb310d 100644 --- a/src/models/domain/folder.ts +++ b/src/models/domain/folder.ts @@ -1,5 +1,7 @@ import { FolderData } from '../data/folderData'; +import { FolderView } from '../view/folderView'; + import { CipherString } from './cipherString'; import Domain from './domain'; @@ -19,12 +21,8 @@ export class Folder extends Domain { }, alreadyEncrypted, ['id']); } - decrypt(): Promise { - const model = { - id: this.id, - }; - - return this.decryptObj(model, { + decrypt(): Promise { + return this.decryptObj(new FolderView(this), { name: null, }, null); } diff --git a/src/models/view/collectionView.ts b/src/models/view/collectionView.ts new file mode 100644 index 0000000000..ab184a2c35 --- /dev/null +++ b/src/models/view/collectionView.ts @@ -0,0 +1,18 @@ +import { View } from './view'; + +import { Collection } from '../domain/collection'; + +export class CollectionView implements View { + id: string; + organizationId: string; + name: string; + + constructor(c?: Collection) { + if (!c) { + return; + } + + this.id = c.id; + this.organizationId = c.organizationId; + } +} diff --git a/src/models/view/folderView.ts b/src/models/view/folderView.ts new file mode 100644 index 0000000000..fc4bb219b3 --- /dev/null +++ b/src/models/view/folderView.ts @@ -0,0 +1,16 @@ +import { View } from './view'; + +import { Folder } from '../domain/folder'; + +export class FolderView implements View { + id: string; + name: string; + + constructor(f?: Folder) { + if (!f) { + return; + } + + this.id = f.id; + } +} diff --git a/src/models/view/secureNoteView.ts b/src/models/view/secureNoteView.ts index 4c131b884f..1047c41086 100644 --- a/src/models/view/secureNoteView.ts +++ b/src/models/view/secureNoteView.ts @@ -7,7 +7,11 @@ import { SecureNote } from '../domain/secureNote'; export class SecureNoteView implements View { type: SecureNoteType; - constructor(n: SecureNote) { + constructor(n?: SecureNote) { + if (!n) { + return; + } + this.type = n.type; } diff --git a/src/services/collection.service.ts b/src/services/collection.service.ts index f85dfcf8b1..5cdd393261 100644 --- a/src/services/collection.service.ts +++ b/src/services/collection.service.ts @@ -2,6 +2,8 @@ import { CollectionData } from '../models/data/collectionData'; import { Collection } from '../models/domain/collection'; +import { CollectionView } from '../models/view/collectionView'; + import { CollectionService as CollectionServiceAbstraction } from '../abstractions/collection.service'; import { CryptoService } from '../abstractions/crypto.service'; import { StorageService } from '../abstractions/storage.service'; @@ -12,7 +14,7 @@ const Keys = { }; export class CollectionService implements CollectionServiceAbstraction { - decryptedCollectionCache: any[]; + decryptedCollectionCache: CollectionView[]; constructor(private cryptoService: CryptoService, private userService: UserService, private storageService: StorageService) { @@ -46,7 +48,7 @@ export class CollectionService implements CollectionServiceAbstraction { return response; } - async getAllDecrypted(): Promise { + async getAllDecrypted(): Promise { if (this.decryptedCollectionCache != null) { return this.decryptedCollectionCache; } @@ -56,17 +58,15 @@ export class CollectionService implements CollectionServiceAbstraction { throw new Error('No key.'); } - const decFolders: any[] = []; + const decCollections: CollectionView[] = []; const promises: Array> = []; - const folders = await this.getAll(); - folders.forEach((folder) => { - promises.push(folder.decrypt().then((f: any) => { - decFolders.push(f); - })); + const collections = await this.getAll(); + collections.forEach((collection) => { + promises.push(collection.decrypt().then((c) => decCollections.push(c))); }); await Promise.all(promises); - this.decryptedCollectionCache = decFolders; + this.decryptedCollectionCache = decCollections; return this.decryptedCollectionCache; } diff --git a/src/services/folder.service.ts b/src/services/folder.service.ts index 0f8096849a..c185e20f14 100644 --- a/src/services/folder.service.ts +++ b/src/services/folder.service.ts @@ -6,6 +6,8 @@ import { FolderRequest } from '../models/request/folderRequest'; import { FolderResponse } from '../models/response/folderResponse'; +import { FolderView } from '../models/view/folderView'; + import { ApiService } from '../abstractions/api.service'; import { CryptoService } from '../abstractions/crypto.service'; import { FolderService as FolderServiceAbstraction } from '../abstractions/folder.service'; @@ -17,7 +19,7 @@ const Keys = { }; export class FolderService implements FolderServiceAbstraction { - decryptedFolderCache: any[]; + decryptedFolderCache: FolderView[]; constructor(private cryptoService: CryptoService, private userService: UserService, private noneFolder: () => string, private apiService: ApiService, @@ -28,7 +30,7 @@ export class FolderService implements FolderServiceAbstraction { this.decryptedFolderCache = null; } - async encrypt(model: any): Promise { + async encrypt(model: FolderView): Promise { const folder = new Folder(); folder.id = model.id; folder.name = await this.cryptoService.encrypt(model.name); @@ -59,12 +61,12 @@ export class FolderService implements FolderServiceAbstraction { return response; } - async getAllDecrypted(): Promise { + async getAllDecrypted(): Promise { if (this.decryptedFolderCache != null) { return this.decryptedFolderCache; } - const decFolders: any[] = [{ + const decFolders: FolderView[] = [{ id: null, name: this.noneFolder(), }]; @@ -77,9 +79,7 @@ export class FolderService implements FolderServiceAbstraction { const promises: Array> = []; const folders = await this.getAll(); folders.forEach((folder) => { - promises.push(folder.decrypt().then((f: any) => { - decFolders.push(f); - })); + promises.push(folder.decrypt().then((f) => decFolders.push(f))); }); await Promise.all(promises);