From 2d81466d2571cd4a6c6584f508ee43dba68ef35b Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 22 Nov 2017 13:17:59 -0500 Subject: [PATCH] add support for syncing collections --- src/background.js | 5 +- src/models/data/cipherData.ts | 2 + src/models/data/collectionData.ts | 16 +++ src/models/domain/cipher.ts | 3 + src/models/domain/collection.ts | 37 +++++++ src/models/response/cipherResponse.ts | 8 ++ src/models/response/collectionResponse.ts | 14 +++ src/models/response/syncResponse.ts | 8 ++ src/services/collection.service.ts | 126 ++++++++++++++++++++++ src/services/folder.service.ts | 1 - src/services/sync.service.ts | 14 ++- 11 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 src/models/data/collectionData.ts create mode 100644 src/models/domain/collection.ts create mode 100644 src/models/response/collectionResponse.ts create mode 100644 src/services/collection.service.ts diff --git a/src/background.js b/src/background.js index 5cee7a678a..6a6ba511d5 100644 --- a/src/background.js +++ b/src/background.js @@ -6,6 +6,7 @@ import ApiService from './services/api.service'; import AppIdService from './services/appId.service'; import AutofillService from './services/autofill.service'; import CipherService from './services/cipher.service'; +import CollectionService from './services/collection.service'; import ConstantsService from './services/constants.service'; import CryptoService from './services/crypto.service'; import EnvironmentService from './services/environment.service'; @@ -77,6 +78,7 @@ var bg_isBackground = true, bg_settingsService, bg_cipherService, bg_folderService, + bg_collectionService, bg_lockService, bg_syncService, bg_passwordGenerationService, @@ -106,8 +108,9 @@ var bg_isBackground = true, window.bg_settingsService = bg_settingsService = new SettingsService(bg_userService); window.bg_cipherService = bg_cipherService = new CipherService(bg_cryptoService, bg_userService, bg_settingsService, bg_apiService); window.bg_folderService = bg_folderService = new FolderService(bg_cryptoService, bg_userService, bg_i18nService, bg_apiService); + window.bg_collectionService = bg_collectionService = new CollectionService(bg_cryptoService, bg_userService, bg_i18nService, bg_apiService); window.bg_lockService = bg_lockService = new LockService(bg_cipherService, bg_folderService, bg_cryptoService, bg_utilsService, setIcon, refreshBadgeAndMenu); - window.bg_syncService = bg_syncService = new SyncService(bg_userService, bg_apiService, bg_settingsService, bg_folderService, bg_cipherService, bg_cryptoService, logout); + window.bg_syncService = bg_syncService = new SyncService(bg_userService, bg_apiService, bg_settingsService, bg_folderService, bg_cipherService, bg_cryptoService, bg_collectionService, logout); window.bg_passwordGenerationService = bg_passwordGenerationService = new PasswordGenerationService(bg_cryptoService); window.bg_totpService = bg_totpService = new TotpService(); window.bg_autofillService = bg_autofillService = new AutofillService(bg_cipherService, bg_tokenService, bg_totpService, bg_utilsService); diff --git a/src/models/data/cipherData.ts b/src/models/data/cipherData.ts index 94cc046a59..be5a0d8ee7 100644 --- a/src/models/data/cipherData.ts +++ b/src/models/data/cipherData.ts @@ -28,6 +28,7 @@ class CipherData { identity?: IdentityData; fields?: FieldData[]; attachments?: AttachmentData[]; + collectionIds?: string[]; constructor(response: CipherResponse, userId: string) { this.id = response.id; @@ -39,6 +40,7 @@ class CipherData { this.favorite = response.favorite; this.revisionDate = response.revisionDate; this.type = response.type; + this.collectionIds = response.collectionIds; this.name = response.data.Name; this.notes = response.data.Notes; diff --git a/src/models/data/collectionData.ts b/src/models/data/collectionData.ts new file mode 100644 index 0000000000..f2d5fc9f03 --- /dev/null +++ b/src/models/data/collectionData.ts @@ -0,0 +1,16 @@ +import { CollectionResponse } from '../response/collectionResponse'; + +class CollectionData { + id: string; + organizationId: string; + name: string; + + constructor(response: CollectionResponse) { + this.id = response.id; + this.organizationId = response.organizationId; + this.name = response.name; + } +} + +export { CollectionData }; +(window as any).CollectionData = CollectionData; diff --git a/src/models/domain/cipher.ts b/src/models/domain/cipher.ts index b79ebdcaa6..1369c9967d 100644 --- a/src/models/domain/cipher.ts +++ b/src/models/domain/cipher.ts @@ -30,6 +30,7 @@ class Cipher extends Domain { secureNote: SecureNote; attachments: Attachment[]; fields: Field[]; + collectionIds: string[]; private utilsService: UtilsService; @@ -51,6 +52,7 @@ class Cipher extends Domain { this.favorite = obj.favorite; this.organizationUseTotp = obj.organizationUseTotp; this.edit = obj.edit; + this.collectionIds = obj.collectionIds; this.localData = localData; switch (this.type) { @@ -104,6 +106,7 @@ class Cipher extends Domain { subTitle: null as string, attachments: null as any[], fields: null as any[], + collectionIds: this.collectionIds, }; await this.decryptObj(model, { diff --git a/src/models/domain/collection.ts b/src/models/domain/collection.ts new file mode 100644 index 0000000000..0a5079e53a --- /dev/null +++ b/src/models/domain/collection.ts @@ -0,0 +1,37 @@ +import { CollectionData } from '../data/collectionData'; + +import { CipherString } from './cipherString'; +import Domain from './domain'; + +class Collection extends Domain { + id: string; + organizationId: string; + name: CipherString; + + constructor(obj?: CollectionData, alreadyEncrypted: boolean = false) { + super(); + if (obj == null) { + return; + } + + this.buildDomainModel(this, obj, { + id: null, + organizationId: null, + name: null, + }, alreadyEncrypted, ['id', 'organizationId']); + } + + decrypt(): Promise { + const model = { + id: this.id, + organizationId: this.organizationId, + }; + + return this.decryptObj(model, { + name: null, + }, this.organizationId); + } +} + +export { Collection }; +(window as any).Collection = Collection; diff --git a/src/models/response/cipherResponse.ts b/src/models/response/cipherResponse.ts index be7b65da97..b2e597e23a 100644 --- a/src/models/response/cipherResponse.ts +++ b/src/models/response/cipherResponse.ts @@ -11,6 +11,7 @@ class CipherResponse { data: any; revisionDate: string; attachments: AttachmentResponse[]; + collectionIds: string[]; constructor(response: any) { this.id = response.Id; @@ -29,6 +30,13 @@ class CipherResponse { this.attachments.push(new AttachmentResponse(attachment)); }); } + + if (response.CollectionIds) { + this.collectionIds = []; + response.CollectionIds.forEach((id: string) => { + this.collectionIds.push(id); + }); + } } } diff --git a/src/models/response/collectionResponse.ts b/src/models/response/collectionResponse.ts new file mode 100644 index 0000000000..8b0247720d --- /dev/null +++ b/src/models/response/collectionResponse.ts @@ -0,0 +1,14 @@ +class CollectionResponse { + id: string; + organizationId: string; + name: string; + + constructor(response: any) { + this.id = response.Id; + this.organizationId = response.OrganizationId; + this.name = response.Name; + } +} + +export { CollectionResponse }; +(window as any).CollectionResponse = CollectionResponse; diff --git a/src/models/response/syncResponse.ts b/src/models/response/syncResponse.ts index 4fa2c0b12f..8acdc5202d 100644 --- a/src/models/response/syncResponse.ts +++ b/src/models/response/syncResponse.ts @@ -1,4 +1,5 @@ import { CipherResponse } from './cipherResponse'; +import { CollectionResponse } from './collectionResponse'; import { DomainsResponse } from './domainsResponse'; import { FolderResponse } from './folderResponse'; import { ProfileResponse } from './profileResponse'; @@ -6,6 +7,7 @@ import { ProfileResponse } from './profileResponse'; class SyncResponse { profile?: ProfileResponse; folders: FolderResponse[] = []; + collections: CollectionResponse[] = []; ciphers: CipherResponse[] = []; domains?: DomainsResponse; @@ -20,6 +22,12 @@ class SyncResponse { }); } + if (response.Collections) { + response.Collections.forEach((collection: any) => { + this.collections.push(new CollectionResponse(collection)); + }); + } + if (response.Ciphers) { response.Ciphers.forEach((cipher: any) => { this.ciphers.push(new CipherResponse(cipher)); diff --git a/src/services/collection.service.ts b/src/services/collection.service.ts new file mode 100644 index 0000000000..185338abb0 --- /dev/null +++ b/src/services/collection.service.ts @@ -0,0 +1,126 @@ +import { CipherString } from '../models/domain/cipherString'; +import { Collection } from '../models/domain/collection'; + +import { CollectionData } from '../models/data/collectionData'; + +import ApiService from './api.service'; +import CryptoService from './crypto.service'; +import UserService from './user.service'; +import UtilsService from './utils.service'; + +const Keys = { + collectionsPrefix: 'collections_', +}; + +export default class CollectionService { + decryptedCollectionCache: any[]; + + constructor(private cryptoService: CryptoService, private userService: UserService, + private apiService: ApiService) { + } + + clearCache(): void { + this.decryptedCollectionCache = null; + } + + async get(id: string): Promise { + const userId = await this.userService.getUserId(); + const collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>( + Keys.collectionsPrefix + userId); + if (collections == null || !collections.hasOwnProperty(id)) { + return null; + } + + return new Collection(collections[id]); + } + + async getAll(): Promise { + const userId = await this.userService.getUserId(); + const collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>( + Keys.collectionsPrefix + userId); + const response: Collection[] = []; + for (const id in collections) { + if (collections.hasOwnProperty(id)) { + response.push(new Collection(collections[id])); + } + } + return response; + } + + async getAllDecrypted(): Promise { + if (this.decryptedCollectionCache != null) { + return this.decryptedCollectionCache; + } + + const key = await this.cryptoService.getKey(); + if (key == null) { + throw new Error('No key.'); + } + + const decFolders: any[] = []; + const promises: Array> = []; + const folders = await this.getAll(); + folders.forEach((folder) => { + promises.push(folder.decrypt().then((f: any) => { + decFolders.push(f); + })); + }); + + await Promise.all(promises); + this.decryptedCollectionCache = decFolders; + return this.decryptedCollectionCache; + } + + async upsert(collection: CollectionData | CollectionData[]): Promise { + const userId = await this.userService.getUserId(); + let collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>( + Keys.collectionsPrefix + userId); + if (collections == null) { + collections = {}; + } + + if (collection instanceof CollectionData) { + const c = collection as CollectionData; + collections[c.id] = c; + } else { + (collection as CollectionData[]).forEach((c) => { + collections[c.id] = c; + }); + } + + await UtilsService.saveObjToStorage(Keys.collectionsPrefix + userId, collections); + this.decryptedCollectionCache = null; + } + + async replace(collections: { [id: string]: CollectionData; }): Promise { + const userId = await this.userService.getUserId(); + await UtilsService.saveObjToStorage(Keys.collectionsPrefix + userId, collections); + this.decryptedCollectionCache = null; + } + + async clear(userId: string): Promise { + await UtilsService.removeFromStorage(Keys.collectionsPrefix + userId); + this.decryptedCollectionCache = null; + } + + async delete(id: string | string[]): Promise { + const userId = await this.userService.getUserId(); + const collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>( + Keys.collectionsPrefix + userId); + if (collections == null) { + return; + } + + if (typeof id === 'string') { + const i = id as string; + delete collections[id]; + } else { + (id as string[]).forEach((i) => { + delete collections[i]; + }); + } + + await UtilsService.saveObjToStorage(Keys.collectionsPrefix + userId, collections); + this.decryptedCollectionCache = null; + } +} diff --git a/src/services/folder.service.ts b/src/services/folder.service.ts index b6c58cf88e..3337c1b0bc 100644 --- a/src/services/folder.service.ts +++ b/src/services/folder.service.ts @@ -7,7 +7,6 @@ import { FolderRequest } from '../models/request/folderRequest'; import { FolderResponse } from '../models/response/folderResponse'; import ApiService from './api.service'; -import ConstantsService from './constants.service'; import CryptoService from './crypto.service'; import UserService from './user.service'; import UtilsService from './utils.service'; diff --git a/src/services/sync.service.ts b/src/services/sync.service.ts index c84ebcf82e..a28c401f19 100644 --- a/src/services/sync.service.ts +++ b/src/services/sync.service.ts @@ -1,7 +1,9 @@ import { CipherData } from '../models/data/cipherData'; +import { CollectionData } from '../models/data/collectionData'; import { FolderData } from '../models/data/folderData'; import { CipherResponse } from '../models/response/cipherResponse'; +import { CollectionResponse } from '../models/response/collectionResponse'; import { DomainsResponse } from '../models/response/domainsResponse'; import { FolderResponse } from '../models/response/folderResponse'; import { ProfileResponse } from '../models/response/profileResponse'; @@ -9,6 +11,7 @@ import { SyncResponse } from '../models/response/syncResponse'; import ApiService from './api.service'; import CipherService from './cipher.service'; +import CollectionService from './collection.service'; import CryptoService from './crypto.service'; import FolderService from './folder.service'; import SettingsService from './settings.service'; @@ -25,7 +28,7 @@ export default class SyncService { constructor(private userService: UserService, private apiService: ApiService, private settingsService: SettingsService, private folderService: FolderService, private cipherService: CipherService, private cryptoService: CryptoService, - private logoutCallback: Function) { + private collectionService: CollectionService, private logoutCallback: Function) { } async getLastSync() { @@ -84,6 +87,7 @@ export default class SyncService { await this.syncProfile(response.profile); await this.syncFolders(userId, response.folders); + await this.syncCollections(response.collections); await this.syncCiphers(userId, response.ciphers); await this.syncSettings(userId, response.domains); @@ -141,6 +145,14 @@ export default class SyncService { return await this.folderService.replace(folders); } + private async syncCollections(response: CollectionResponse[]) { + const collections: { [id: string]: CollectionData; } = {}; + response.forEach((c) => { + collections[c.id] = new CollectionData(c); + }); + return await this.collectionService.replace(collections); + } + private async syncCiphers(userId: string, response: CipherResponse[]) { const ciphers: { [id: string]: CipherData; } = {}; response.forEach((c) => {