From fef4171070e3eccb9822bfada0c07296f1213abc Mon Sep 17 00:00:00 2001 From: Juan Carlos Ruvalcaba Date: Mon, 3 May 2021 19:08:51 -0700 Subject: [PATCH] -Updates: -Remove 'state' on unneeded actions -Encrypt IndexedDb for security reasons, must be HTTPS or localhost --- .gitignore | 1 + package-lock.json | 5 +++++ package.json | 1 + src/js/api/config.js | 6 ++---- src/js/api/encrypt.js | 49 ++++++++++++++++++++++++++++++++++++++++++ src/js/stores/theme.js | 6 +++--- src/js/stores/user.js | 4 ++-- 7 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 src/js/api/encrypt.js diff --git a/.gitignore b/.gitignore index 880f6de..a39b3fc 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,4 @@ www/ #Hide connection file /src/js/api/ConnectionMode.js /src/js/api/key.js +/src/_to-ignore/ diff --git a/package-lock.json b/package-lock.json index 21d8506..f11639e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3090,6 +3090,11 @@ "which": "^2.0.1" } }, + "crypto-js": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.0.0.tgz", + "integrity": "sha512-bzHZN8Pn+gS7DQA6n+iUmBfl0hO5DJq++QP3U6uTucDtk/0iGpXd/Gg7CGR0p8tJhofJyaKoWBuJI4eAO00BBg==" + }, "crypto-random-string": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz", diff --git a/package.json b/package.json index efc3c3c..4bdb19e 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ ], "dependencies": { "axios": "^0.21.1", + "crypto-js": "^4.0.0", "dom7": "^3.0.0", "framework7": "^6.0.16", "framework7-icons": "^4.0.2", diff --git a/src/js/api/config.js b/src/js/api/config.js index 723a020..302808b 100644 --- a/src/js/api/config.js +++ b/src/js/api/config.js @@ -1,13 +1,11 @@ -import { url, dbConfig } from './key'; +import { url } from './key'; +import { localForage } from './encrypt'; import axios from 'axios'; -import * as localForage from "localforage"; const http = axios.create({ baseURL: `${url}`, }); -localForage.config(dbConfig); - async function checkToken (){ let token = await localForage.getItem('token'); if (token != null){ diff --git a/src/js/api/encrypt.js b/src/js/api/encrypt.js new file mode 100644 index 0000000..a161ab2 --- /dev/null +++ b/src/js/api/encrypt.js @@ -0,0 +1,49 @@ +import { dbConfig } from './key'; +import * as localforage from "localforage"; +import CryptoJS from 'crypto-js'; + +localforage.config(dbConfig); + +const localForage = { + /** + * Set object in storage + * @param {string} key object identifier + * @param {any} data object to store + * @return {void} + */ + async setItem(key, data) { + data = CryptoJS.AES.encrypt(JSON.stringify(data), dbConfig.key).toString(); + return localforage.setItem(key, data); + }, + + /** + * Get object from storage + * @param {string} key object identifier + * @return {any} + */ + async getItem(key) { + const data = await localforage.getItem(key); + if (data == null){ + return data; + } + const bytes = CryptoJS.AES.decrypt(data, dbConfig.key); + return JSON.parse(bytes.toString(CryptoJS.enc.Utf8)); + }, + + /** + * Delete object from storage + * @param {string} key object identifier + */ + async deleteItem(key) { + return localforage.removeItem(key); + }, + + /** + * Clear storage + */ + async deleteAll() { + return localforage.clear(); + }, +}; + +export { localForage }; diff --git a/src/js/stores/theme.js b/src/js/stores/theme.js index b2307d2..631e21e 100644 --- a/src/js/stores/theme.js +++ b/src/js/stores/theme.js @@ -12,18 +12,18 @@ const theme = createStore({ state.name = name; }, //End mutations - async checkTheme({ state, dispatch }, selector){ + async checkTheme({ dispatch }, selector){ let theme = await localForage.getItem('theme'); if (theme != 'theme-dark'){ $(selector).removeClass('theme-dark'); } dispatch('changeTheme', theme); }, - async initTheme({state, dispatch}, selector){ + async initTheme({ dispatch }, selector){ let item = await localForage.getItem('theme'); if (item != null){ await dispatch('checkTheme', selector); } }, - async setTheme({ state, dispatch }, name){ + async setTheme({ dispatch }, name){ await localForage.setItem('theme', name); dispatch('changeTheme', name); }, diff --git a/src/js/stores/user.js b/src/js/stores/user.js index 3402f64..c99f839 100644 --- a/src/js/stores/user.js +++ b/src/js/stores/user.js @@ -34,7 +34,7 @@ const user = createStore({ await dispatch('getBasicData'); } }, - async logIn({ state, dispatch }, data){ + async logIn({ dispatch }, data){ dispatch('auth_request'); try { const response = await http.post('/user/login', data); @@ -52,7 +52,7 @@ const user = createStore({ throw new error; } }, - async logOut({ state, dispatch }){ + async logOut({ dispatch }){ dispatch('logout'); await dispatch('clearBasicData'); delete http.defaults.headers.common['Authorization'];