diff --git a/README.md b/README.md index 39f939b..364b51f 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ Here is the list of all the supported values `UserAgent|ScreenPrint|Plugins|Font import { useEffect } from "react"; - import secureLocalStorage from "react-secure-storage"; + import { secureLocalStorage, secureSessionStorage } from "react-secure-storage"; const App = () => { @@ -137,6 +137,14 @@ Here is the list of all the supported values `UserAgent|ScreenPrint|Plugins|Font secureLocalStorage.setItem("number", 12); secureLocalStorage.setItem("string", "12"); secureLocalStorage.setItem("boolean", true); + let value = secureLocalStorage.getItem("boolean"); + + secureSessionStorage.setItem("object", { + message: "This is testing of local storage", + }); + secureLocalStorage.setItem("number", 12); + secureLocalStorage.setItem("string", "12"); + secureLocalStorage.setItem("boolean", true); let value = secureLocalStorage.getItem("boolean"); }, []); @@ -158,6 +166,8 @@ Regular bug fixes and https://github.com/sushinpv/react-secure-storage/issues/39 ## Whats new | Previous? +Added secure session storage support + Added support for Vite and Next.js environment variables Now you can disable individual fingerprint generation properties, This is discussed in the following enhancement https://github.com/sushinpv/react-secure-storage/issues/14 diff --git a/dist/index.js b/dist/index.js index d03443a..1e8cf32 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = void 0; +exports.secureStorage = exports.secureSessionStorage = exports.secureLocalStorage = void 0; var _encryption = _interopRequireDefault(require("./encryption")); -var _localStorageHelpers = _interopRequireDefault(require("./localStorageHelpers")); +var _storageHelpers = _interopRequireDefault(require("./storageHelpers")); var _utils = require("./utils"); @@ -34,7 +34,7 @@ var getDataType = function getDataType(value) { return _typeof(value) === "object" ? "j" : typeof value === "boolean" ? "b" : typeof value === "number" ? "n" : "s"; }; /** - * Function to create local storage key + * Function to create storage key * @param key * @param value */ @@ -45,18 +45,21 @@ var getLocalKey = function getLocalKey(key, value) { return KEY_PREFIX + "".concat(keyType, ".") + key; }; /** - * This version of local storage supports the following data types as it is and other data types will be treated as string + * This version of storage supports the following data types as it is and other data types will be treated as string * object, string, number and Boolean */ -var SecureLocalStorage = /*#__PURE__*/function () { - function SecureLocalStorage() { - _classCallCheck(this, SecureLocalStorage); +var secureStorage = /*#__PURE__*/function () { + function secureStorage(storage) { + _classCallCheck(this, secureStorage); - _defineProperty(this, "_localStorageItems", {}); + _defineProperty(this, "_storage", void 0); - this._localStorageItems = (0, _localStorageHelpers.default)(); + _defineProperty(this, "_storageItems", {}); + + this._storage = storage; + this._storageItems = (0, _storageHelpers.default)(storage); } /** * Function to set value to secure local storage @@ -65,20 +68,21 @@ var SecureLocalStorage = /*#__PURE__*/function () { */ - _createClass(SecureLocalStorage, [{ + _createClass(secureStorage, [{ key: "setItem", value: function setItem(key, value) { if (value === null || value === undefined) this.removeItem(key);else { var parsedValue = _typeof(value) === "object" ? JSON.stringify(value) : value + ""; var parsedKeyLocal = getLocalKey(key, value); var parsedKey = KEY_PREFIX + key; - if (key != null) this._localStorageItems[parsedKey] = value; + if (key != null) this._storageItems[parsedKey] = value; var encrypt = new _encryption.default(); - localStorage.setItem(parsedKeyLocal, encrypt.encrypt(parsedValue)); + + this._storage.setItem(parsedKeyLocal, encrypt.encrypt(parsedValue)); } } /** - * Function to get value from secure local storage + * Function to get value from secure storage * @param key to get * @returns */ @@ -86,13 +90,13 @@ var SecureLocalStorage = /*#__PURE__*/function () { }, { key: "getItem", value: function getItem(key) { - var _this$_localStorageIt; + var _this$_storageItems$p; var parsedKey = KEY_PREFIX + key; - return (_this$_localStorageIt = this._localStorageItems[parsedKey]) !== null && _this$_localStorageIt !== void 0 ? _this$_localStorageIt : null; + return (_this$_storageItems$p = this._storageItems[parsedKey]) !== null && _this$_storageItems$p !== void 0 ? _this$_storageItems$p : null; } /** - * Function to remove item from secure local storage + * Function to remove item from secure storage * @param key to be removed */ @@ -100,26 +104,30 @@ var SecureLocalStorage = /*#__PURE__*/function () { key: "removeItem", value: function removeItem(key) { var parsedKey = KEY_PREFIX + key; - var value = this._localStorageItems[parsedKey]; + var value = this._storageItems[parsedKey]; var parsedKeyLocal = getLocalKey(key, value); - if (this._localStorageItems[parsedKey] !== undefined) delete this._localStorageItems[parsedKey]; - localStorage.removeItem(parsedKeyLocal); + if (this._storageItems[parsedKey] !== undefined) delete this._storageItems[parsedKey]; + + this._storage.removeItem(parsedKeyLocal); } /** - * Function to clear secure local storage + * Function to clear secure storage */ }, { key: "clear", value: function clear() { - this._localStorageItems = {}; - localStorage.clear(); + this._storageItems = {}; + + this._storage.clear(); } }]); - return SecureLocalStorage; + return secureStorage; }(); -var secureLocalStorage = new SecureLocalStorage(); -var _default = secureLocalStorage; -exports.default = _default; \ No newline at end of file +exports.secureStorage = secureStorage; +var secureLocalStorage = typeof window !== 'undefined' ? new secureStorage(window.localStorage) : new secureStorage(localStorage); +exports.secureLocalStorage = secureLocalStorage; +var secureSessionStorage = typeof window !== 'undefined' ? new secureStorage(window.sessionStorage) : new secureStorage(sessionStorage); +exports.secureSessionStorage = secureSessionStorage; \ No newline at end of file diff --git a/dist/localStorageHelpers.js b/dist/storageHelpers.js similarity index 84% rename from dist/localStorageHelpers.js rename to dist/storageHelpers.js index bd7d96f..8d2e784 100644 --- a/dist/localStorageHelpers.js +++ b/dist/storageHelpers.js @@ -25,17 +25,18 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var KEY_PREFIX = (0, _utils.getSecurePrefix)(); /** - * Function to preload all the local storage data + * Function to preload all the storage data * @returns */ -var getAllLocalStorageItems = function getAllLocalStorageItems() { +var getAllStorageItems = function getAllStorageItems(storage) { + var sessionStorageItems = {}; var localStorageItems = {}; if (typeof window !== "undefined") { var encrypt = new _encryption.default(); - for (var _i = 0, _Object$entries = Object.entries(localStorage); _i < _Object$entries.length; _i++) { + for (var _i = 0, _Object$entries = Object.entries(storage); _i < _Object$entries.length; _i++) { var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1]; @@ -71,13 +72,22 @@ var getAllLocalStorageItems = function getAllLocalStorageItems() { default: parsedValue = decryptedValue; } - localStorageItems[parsedKey] = parsedValue; + + if (storage === sessionStorage) { + sessionStorageItems[parsedKey] = parsedValue; + } else { + localStorageItems[parsedKey] = parsedValue; + } } } } - return localStorageItems; + if (storage === sessionStorage) { + return sessionStorageItems; + } else { + return localStorageItems; + } }; -var _default = getAllLocalStorageItems; +var _default = getAllStorageItems; exports.default = _default; \ No newline at end of file diff --git a/dist/types.d.ts b/dist/types.d.ts index e0bc8f1..baed497 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -33,9 +33,49 @@ declare class SecureLocalStorage { clear(): void; } +/** + * This version of local storage supports the following data types as it is and other data types will be treated as string + * object, string, number and Boolean + * To change the custom secure key, Please add `SECURE_LOCAL_STORAGE_HASH_KEY` or `REACT_APP_SECURE_LOCAL_STORAGE_HASH_KEY` to .env and change the value + */ +declare class SecureSessionStorage { + // private _localStorageItems; + constructor(); + + /** + * Function to set value to secure local storage + * @param key to be added + * @param value value to be added + */ + setItem(key: string, value: string | object | number | boolean): void; + + /** + * Function to get value from secure local storage + * @param key to get + * @returns + */ + getItem(key: string): string | object | number | boolean | null; + + /** + * Function to remove item from secure local storage + * @param key to be removed + */ + removeItem(key: string): void; + + /** + * Function to clear secure local storage + */ + clear(): void; +} + +/** + * Create an instance of secureLocalStorage + */ +const secureSessionStorage = new SecureSessionStorage(); + /** * Create an instance of secureLocalStorage */ const secureLocalStorage = new SecureLocalStorage(); -export default secureLocalStorage; +export {secureLocalStorage, secureSessionStorage}; diff --git a/src/App.tsx b/src/App.tsx index 3a1df13..149b07c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from "react"; -import secureLocalStorage from "./lib"; +import {secureLocalStorage, secureSessionStorage} from "./lib"; import logo from "./logo.svg"; import "./App.css"; @@ -12,6 +12,14 @@ function App() { secureLocalStorage.setItem("number", 12); secureLocalStorage.setItem("string", "12"); secureLocalStorage.setItem("boolean", true); + + console.log("secureSessionStorage", secureSessionStorage); + secureSessionStorage.setItem("object", { + message: "This is testing of local storage", + }); + secureSessionStorage.setItem("number", 12); + secureSessionStorage.setItem("string", "12"); + secureSessionStorage.setItem("boolean", true); }, []); return ( diff --git a/src/lib/coreTypes.d.ts b/src/lib/coreTypes.d.ts index 35c0920..35f2350 100644 --- a/src/lib/coreTypes.d.ts +++ b/src/lib/coreTypes.d.ts @@ -1,3 +1,8 @@ export interface LocalStorageItem { [key: string]: string | object | number | boolean | null; } + +export interface SessionStorageItem { + [key: string]: string | object | number | boolean | null; +} + diff --git a/src/lib/index.ts b/src/lib/index.ts index dc44728..329c0e0 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,6 +1,6 @@ -import { LocalStorageItem } from "./coreTypes"; +import { LocalStorageItem, SessionStorageItem } from "./coreTypes"; import EncryptionService from "./encryption"; -import getAllLocalStorageItems from "./localStorageHelpers"; +import getAllStorageItems from "./storageHelpers"; import { getSecurePrefix } from "./utils"; const KEY_PREFIX = getSecurePrefix(); @@ -15,7 +15,7 @@ const getDataType = (value: string | object | number | boolean | null) => { }; /** - * Function to create local storage key + * Function to create storage key * @param key * @param value */ @@ -25,14 +25,16 @@ const getLocalKey = (key: string, value: string | object | number | boolean | nu }; /** - * This version of local storage supports the following data types as it is and other data types will be treated as string + * This version of storage supports the following data types as it is and other data types will be treated as string * object, string, number and Boolean */ -class SecureLocalStorage { - private _localStorageItems: LocalStorageItem = {}; +export class secureStorage { + private _storage: Storage; + private _storageItems: LocalStorageItem | SessionStorageItem = {}; - constructor() { - this._localStorageItems = getAllLocalStorageItems(); + constructor(storage: Storage) { + this._storage = storage; + this._storageItems = getAllStorageItems(storage); } /** @@ -46,43 +48,45 @@ class SecureLocalStorage { let parsedValue = typeof value === "object" ? JSON.stringify(value) : value + ""; let parsedKeyLocal = getLocalKey(key, value); let parsedKey = KEY_PREFIX + key; - if (key != null) this._localStorageItems[parsedKey] = value; + if (key != null) this._storageItems[parsedKey] = value; const encrypt = new EncryptionService(); - localStorage.setItem(parsedKeyLocal, encrypt.encrypt(parsedValue)); + this._storage.setItem(parsedKeyLocal, encrypt.encrypt(parsedValue)); } } /** - * Function to get value from secure local storage + * Function to get value from secure storage * @param key to get * @returns */ getItem(key: string): string | object | number | boolean | null { let parsedKey = KEY_PREFIX + key; - return this._localStorageItems[parsedKey] ?? null; + return this._storageItems[parsedKey] ?? null; } /** - * Function to remove item from secure local storage + * Function to remove item from secure storage * @param key to be removed */ removeItem(key: string) { let parsedKey = KEY_PREFIX + key; - let value = this._localStorageItems[parsedKey]; + let value = this._storageItems[parsedKey]; let parsedKeyLocal = getLocalKey(key, value); - if (this._localStorageItems[parsedKey] !== undefined) delete this._localStorageItems[parsedKey]; - localStorage.removeItem(parsedKeyLocal); + + if (this._storageItems[parsedKey] !== undefined) delete this._storageItems[parsedKey]; + this._storage.removeItem(parsedKeyLocal); } /** - * Function to clear secure local storage + * Function to clear secure storage */ clear() { - this._localStorageItems = {}; - localStorage.clear(); + this._storageItems = {}; + this._storage.clear(); } } -const secureLocalStorage = new SecureLocalStorage(); +const secureLocalStorage = typeof window !== 'undefined' ? new secureStorage(window.localStorage) : new secureStorage(localStorage); +const secureSessionStorage = typeof window !== 'undefined' ? new secureStorage(window.sessionStorage) : new secureStorage(sessionStorage); -export default secureLocalStorage; +export { secureLocalStorage, secureSessionStorage }; diff --git a/src/lib/localStorageHelpers.ts b/src/lib/storageHelpers.ts similarity index 63% rename from src/lib/localStorageHelpers.ts rename to src/lib/storageHelpers.ts index 6579202..1c0975a 100644 --- a/src/lib/localStorageHelpers.ts +++ b/src/lib/storageHelpers.ts @@ -1,18 +1,19 @@ -import { LocalStorageItem } from "./coreTypes"; +import { SessionStorageItem, LocalStorageItem } from "./coreTypes"; import EncryptionService from "./encryption"; import { getSecurePrefix } from "./utils"; const KEY_PREFIX = getSecurePrefix(); /** - * Function to preload all the local storage data + * Function to preload all the storage data * @returns */ -const getAllLocalStorageItems = () => { - const localStorageItems: LocalStorageItem = {}; +const getAllStorageItems = (storage : Storage) => { + let sessionStorageItems: SessionStorageItem = {}; + let localStorageItems: LocalStorageItem = {}; if (typeof window !== "undefined") { const encrypt = new EncryptionService(); - for (const [key, value] of Object.entries(localStorage)) { + for (const [key, value] of Object.entries(storage)) { if (key.startsWith(KEY_PREFIX)) { let keyType = key.replace(KEY_PREFIX, "")[0]; let parsedKey = key.replace(/[.][bjns][.]/, "."); @@ -40,11 +41,19 @@ const getAllLocalStorageItems = () => { default: parsedValue = decryptedValue; } - localStorageItems[parsedKey] = parsedValue; + if (storage === sessionStorage) { + sessionStorageItems[parsedKey] = parsedValue; + } else { + localStorageItems[parsedKey] = parsedValue; + } } } } - return localStorageItems; + if (storage === sessionStorage) { + return sessionStorageItems; + } else { + return localStorageItems; + } }; -export default getAllLocalStorageItems; +export default getAllStorageItems; diff --git a/src/lib/types.d.ts b/src/lib/types.d.ts index e0bc8f1..baed497 100644 --- a/src/lib/types.d.ts +++ b/src/lib/types.d.ts @@ -33,9 +33,49 @@ declare class SecureLocalStorage { clear(): void; } +/** + * This version of local storage supports the following data types as it is and other data types will be treated as string + * object, string, number and Boolean + * To change the custom secure key, Please add `SECURE_LOCAL_STORAGE_HASH_KEY` or `REACT_APP_SECURE_LOCAL_STORAGE_HASH_KEY` to .env and change the value + */ +declare class SecureSessionStorage { + // private _localStorageItems; + constructor(); + + /** + * Function to set value to secure local storage + * @param key to be added + * @param value value to be added + */ + setItem(key: string, value: string | object | number | boolean): void; + + /** + * Function to get value from secure local storage + * @param key to get + * @returns + */ + getItem(key: string): string | object | number | boolean | null; + + /** + * Function to remove item from secure local storage + * @param key to be removed + */ + removeItem(key: string): void; + + /** + * Function to clear secure local storage + */ + clear(): void; +} + +/** + * Create an instance of secureLocalStorage + */ +const secureSessionStorage = new SecureSessionStorage(); + /** * Create an instance of secureLocalStorage */ const secureLocalStorage = new SecureLocalStorage(); -export default secureLocalStorage; +export {secureLocalStorage, secureSessionStorage};