Browse Source

added collection name encryption and decryption logic

Abhinav-grd 4 years ago
parent
commit
592172917f
1 changed files with 23 additions and 10 deletions
  1. 23 10
      src/services/collectionService.ts

+ 23 - 10
src/services/collectionService.ts

@@ -25,7 +25,9 @@ export interface collection {
     id: string;
     id: string;
     owner: user;
     owner: user;
     key?: string;
     key?: string;
-    name: string;
+    name?: string;
+    encryptedName?: string;
+    nameDecryptionNonce?: string;
     type: string;
     type: string;
     attributes: collectionAttributes
     attributes: collectionAttributes
     sharees: user[];
     sharees: user[];
@@ -46,17 +48,21 @@ export interface collectionLatestFile {
 }
 }
 
 
 
 
-const getCollectionKey = async (collection: collection, masterKey: string) => {
+const getCollectionSecrets = async (collection: collection, masterKey: string) => {
     const worker = await new CryptoWorker();
     const worker = await new CryptoWorker();
     const userID = getData(LS_KEYS.USER).id;
     const userID = getData(LS_KEYS.USER).id;
     let decryptedKey: string;
     let decryptedKey: string;
-
+    let decryptedName: string;
     if (collection.owner.id == userID) {
     if (collection.owner.id == userID) {
         decryptedKey = await worker.decryptB64(
         decryptedKey = await worker.decryptB64(
             collection.encryptedKey,
             collection.encryptedKey,
             collection.keyDecryptionNonce,
             collection.keyDecryptionNonce,
             masterKey
             masterKey
         );
         );
+        decryptedName = collection.name || await worker.decryptString(
+            collection.encryptedName,
+            collection.nameDecryptionNonce,
+            masterKey);
     } else {
     } else {
         const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
         const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
         const secretKey = await worker.decryptB64(
         const secretKey = await worker.decryptB64(
@@ -65,14 +71,19 @@ const getCollectionKey = async (collection: collection, masterKey: string) => {
             masterKey
             masterKey
         );
         );
         decryptedKey = await worker.boxSealOpen(
         decryptedKey = await worker.boxSealOpen(
-            await collection.encryptedKey,
-            await keyAttributes.publicKey,
-            await secretKey
+            collection.encryptedKey,
+            keyAttributes.publicKey,
+            secretKey
         );
         );
+        decryptedName = collection.name || await worker.decryptString(
+            collection.encryptedName,
+            collection.nameDecryptionNonce,
+            secretKey);
     }
     }
     return {
     return {
         ...collection,
         ...collection,
         key: decryptedKey,
         key: decryptedKey,
+        name: decryptedName
     };
     };
 };
 };
 
 
@@ -87,7 +98,7 @@ const getCollections = async (
     });
     });
     const ignore: Set<number> = new Set([206, 208]);
     const ignore: Set<number> = new Set([206, 208]);
     const promises: Promise<collection>[] = resp.data.collections.filter(collection => !ignore.has(collection.id)).map(
     const promises: Promise<collection>[] = resp.data.collections.filter(collection => !ignore.has(collection.id)).map(
-        (collection: collection) => getCollectionKey(collection, key)
+        (collection: collection) => getCollectionSecrets(collection, key)
     );
     );
     return await Promise.all(promises);
     return await Promise.all(promises);
 };
 };
@@ -128,18 +139,20 @@ export const createAlbum = async (albumName: string) => {
 }
 }
 
 
 
 
-export const AddCollection = async (albumName: string, type: CollectionType) => {
+export const AddCollection = async (collectionName: string, type: CollectionType) => {
     const worker = await new CryptoWorker();
     const worker = await new CryptoWorker();
     const encryptionKey = await getActualKey();
     const encryptionKey = await getActualKey();
     const token = getToken();
     const token = getToken();
     const collectionKey: string = await worker.generateMasterKey();
     const collectionKey: string = await worker.generateMasterKey();
     const { encryptedData: encryptedKey, nonce: keyDecryptionNonce }: keyEncryptionResult = await worker.encryptToB64(collectionKey, encryptionKey);
     const { encryptedData: encryptedKey, nonce: keyDecryptionNonce }: keyEncryptionResult = await worker.encryptToB64(collectionKey, encryptionKey);
+    const { encryptedData: encryptedName, nonce: nameDecryptionNonce }: keyEncryptionResult = await worker.encryptToB64(collectionName, encryptionKey);
     const newCollection: collection = {
     const newCollection: collection = {
         id: null,
         id: null,
         owner: null,
         owner: null,
         encryptedKey,
         encryptedKey,
         keyDecryptionNonce,
         keyDecryptionNonce,
-        name: albumName,
+        encryptedName,
+        nameDecryptionNonce,
         type,
         type,
         attributes: {},
         attributes: {},
         sharees: null,
         sharees: null,
@@ -147,7 +160,7 @@ export const AddCollection = async (albumName: string, type: CollectionType) =>
         isDeleted: false
         isDeleted: false
     };
     };
     let createdCollection: collection = await createCollection(newCollection, token);
     let createdCollection: collection = await createCollection(newCollection, token);
-    createdCollection = await getCollectionKey(createdCollection, encryptionKey);
+    createdCollection = await getCollectionSecrets(createdCollection, encryptionKey);
     return createdCollection;
     return createdCollection;
 }
 }