Skip to content

Commit

Permalink
Added sessionStorage support, cleaned code and did some minor testing…
Browse files Browse the repository at this point in the history
… with my own project
  • Loading branch information
Anon0x19 committed Jul 24, 2024
1 parent 1ccd889 commit 04b16d2
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 65 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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");
}, []);

Expand All @@ -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
Expand Down
60 changes: 34 additions & 26 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -65,61 +68,66 @@ 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
*/

}, {
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
*/

}, {
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;
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;
22 changes: 16 additions & 6 deletions dist/localStorageHelpers.js → dist/storageHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
42 changes: 41 additions & 1 deletion dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};
10 changes: 9 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 (
Expand Down
5 changes: 5 additions & 0 deletions src/lib/coreTypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export interface LocalStorageItem {
[key: string]: string | object | number | boolean | null;
}

export interface SessionStorageItem {
[key: string]: string | object | number | boolean | null;
}

46 changes: 25 additions & 21 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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
*/
Expand All @@ -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);
}

/**
Expand All @@ -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 };
Loading

0 comments on commit 04b16d2

Please sign in to comment.