Advertisement
gregfenton

Untitled

May 18th, 2022
877
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.addUserQuery = functions.https.onCall(async (data, context) => {
  2.   try {
  3.     const nummy = admin.firestore.FieldValue.serverTimestamp();
  4.     const db = admin.firestore();
  5.  
  6.     // Getting secret key for user to add to the end of each encryption
  7.     let userSecret;
  8.     const userSecretPath = db
  9.       .collection('users')
  10.       .doc('filler')
  11.       .collection(data.userUID)
  12.       .doc('secKey');
  13.     let doc = await userSecretPath.get();
  14.     userSecret = doc.data().secKeyStr;
  15.     const rawObjectToAdd = data.objectToAdd;
  16.     const randomID = data.randomID;
  17.     const isLink = data.isLink;
  18.     let encryptedObjectToAdd = {};
  19.     const encryptionKey = data.hashedSetMasterPassValue;
  20.  
  21.     const newQueryPath = db
  22.       .collection('users')
  23.       .doc('filler')
  24.       .collection(data.userUID)
  25.       .doc('mpaps')
  26.       .collection('ps')
  27.       .doc();
  28.  
  29.     let infoArray = [];
  30.  
  31.     for (const [key, value] of Object.entries(rawObjectToAdd)) {
  32.       // Encrypting values from object and putting it in a new encrypted object
  33.       infoArray.push(`${key}: ${value}`);
  34.       encryptedObjectToAdd[key] =
  35.         CryptoJS.AES.encrypt(value, encryptionKey).toString() + userSecret;
  36.     }
  37.  
  38.     encryptedObjectToAdd.isLink = isLink;
  39.     encryptedObjectToAdd.random = randomID;
  40.     const stringifiedEncryptedObject = JSON.stringify(encryptedObjectToAdd);
  41.  
  42.     const batch = db.batch();
  43.     batch.create(newQueryPath, {
  44.       combinedQueryInfo: stringifiedEncryptedObject,
  45.       nummy: nummy,
  46.     });
  47.     try {
  48.       await batch.commit();
  49.       return {
  50.         ERROR: 'None',
  51.         data: data,
  52.         encryptedObj: encryptedObjectToAdd,
  53.         stringifiedEncryptedObject: stringifiedEncryptedObject,
  54.         newQueryPath: newQueryPath,
  55.         infoArray: infoArray,
  56.         rawObjectToAdd: rawObjectToAdd,
  57.       };
  58.     } catch (err) {
  59.       return {
  60.         ERROR: err,
  61.         data: data,
  62.         encryptedObj: encryptedObjectToAdd,
  63.         stringifiedEncryptedObject: stringifiedEncryptedObject,
  64.         newQueryPath: newQueryPath,
  65.         infoArray: infoArray,
  66.         rawObjectToAdd: rawObjectToAdd,
  67.       };
  68.     }
  69.   } catch (err) {
  70.     return { ERROR: err };
  71.   }
  72. });
  73.  
Advertisement
RAW Paste Data Copied
Advertisement