From a600c4a5398f8893d4cfea298fcfb66f90c3d975 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 5 Jul 2018 23:38:27 -0400 Subject: [PATCH] org import and collection encrypt function --- src/abstractions/api.service.ts | 2 +- src/abstractions/collection.service.ts | 1 + .../request/importOrganizationCiphersRequest.ts | 6 +++--- src/services/api.service.ts | 4 ++-- src/services/collection.service.ts | 16 ++++++++++++++++ 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/abstractions/api.service.ts b/src/abstractions/api.service.ts index b466f04f455..b9884bb7dd7 100644 --- a/src/abstractions/api.service.ts +++ b/src/abstractions/api.service.ts @@ -97,7 +97,7 @@ export abstract class ApiService { putCipherCollectionsAdmin: (id: string, request: CipherCollectionsRequest) => Promise; postPurgeCiphers: (request: PasswordVerificationRequest) => Promise; postImportCiphers: (request: ImportCiphersRequest) => Promise; - postImportOrganizationCiphers: (request: ImportOrganizationCiphersRequest) => Promise; + postImportOrganizationCiphers: (organizationId: string, request: ImportOrganizationCiphersRequest) => Promise; postCipherAttachment: (id: string, data: FormData) => Promise; postCipherAttachmentAdmin: (id: string, data: FormData) => Promise; deleteCipherAttachment: (id: string, attachmentId: string) => Promise; diff --git a/src/abstractions/collection.service.ts b/src/abstractions/collection.service.ts index 7960dfd7bdf..041db130bcc 100644 --- a/src/abstractions/collection.service.ts +++ b/src/abstractions/collection.service.ts @@ -8,6 +8,7 @@ export abstract class CollectionService { decryptedCollectionCache: CollectionView[]; clearCache: () => void; + encrypt: (model: CollectionView) => Promise; get: (id: string) => Promise; getAll: () => Promise; getAllDecrypted: () => Promise; diff --git a/src/models/request/importOrganizationCiphersRequest.ts b/src/models/request/importOrganizationCiphersRequest.ts index 3a63693235a..c39e66e797c 100644 --- a/src/models/request/importOrganizationCiphersRequest.ts +++ b/src/models/request/importOrganizationCiphersRequest.ts @@ -3,7 +3,7 @@ import { CollectionRequest } from './collectionRequest'; import { KvpRequest } from './kvpRequest'; export class ImportOrganizationCiphersRequest { - ciphers: CipherRequest[]; - collections: CollectionRequest[]; - collectionRelationships: Array>; + ciphers: CipherRequest[] = []; + collections: CollectionRequest[] = []; + collectionRelationships: Array> = []; } diff --git a/src/services/api.service.ts b/src/services/api.service.ts index a8aee569131..7eb3331f450 100644 --- a/src/services/api.service.ts +++ b/src/services/api.service.ts @@ -318,8 +318,8 @@ export class ApiService implements ApiServiceAbstraction { return this.send('POST', '/ciphers/import', request, true, false); } - postImportOrganizationCiphers(request: ImportOrganizationCiphersRequest): Promise { - return this.send('POST', '/ciphers/import-organization', request, true, false); + postImportOrganizationCiphers(organizationId: string, request: ImportOrganizationCiphersRequest): Promise { + return this.send('POST', '/ciphers/import-organization?organizationId=' + organizationId, request, true, false); } // Attachments APIs diff --git a/src/services/collection.service.ts b/src/services/collection.service.ts index c76fbb6299f..8cf1b8ac17a 100644 --- a/src/services/collection.service.ts +++ b/src/services/collection.service.ts @@ -25,6 +25,22 @@ export class CollectionService implements CollectionServiceAbstraction { this.decryptedCollectionCache = null; } + async encrypt(model: CollectionView): Promise { + if (model.organizationId == null) { + throw new Error('Collection has no organization id.'); + } + const key = await this.cryptoService.getOrgKey(model.organizationId); + if (key == null) { + throw new Error('No key for this collection\'s organization.'); + } + const collection = new Collection(); + collection.id = model.id; + collection.organizationId = model.organizationId; + collection.readOnly = model.readOnly; + collection.name = await this.cryptoService.encrypt(model.name, key); + return collection; + } + async get(id: string): Promise { const userId = await this.userService.getUserId(); const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(