Skip to content

Commit

Permalink
feat: add new parameter to EncryptStorageOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
michelonsouza committed Aug 21, 2022
1 parent b378c39 commit 80acdb2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ The `options` object is optional and consists of the following properties:
|`storageType` |`localStorage` |[StorageType](./src/types.ts#L3) |`false` |
|`encAlgorithm` |`AES` |[EncAlgorithm](./src/types.ts#L1) |`false` |
|`stateManagementUse` |`false` |`boolean` |`false` |
|`doNotEncryptValues` |`false` |`boolean` |`false` |

## Usage

Expand Down Expand Up @@ -158,7 +159,7 @@ default `''` - is optional and is the prefix of all keys used in the selected st
```typescript
import { EncryptStorage } from 'encrypt-storage';

export const encryptStorage = new EncryptStorage('secret-key', {
export const encryptStorage = new EncryptStorage('secret-key-value', {
prefix: '@example',
});
```
Expand All @@ -170,7 +171,7 @@ default `localStorage` - is the type of storage that will be used, at the moment
```typescript
import { EncryptStorage } from 'encrypt-storage';

export const encryptStorage = new EncryptStorage('secret-key', {
export const encryptStorage = new EncryptStorage('secret-key-value', {
storageType: 'sessionStorage',
});
```
Expand All @@ -184,7 +185,7 @@ default `false` - is a `boolean` value that, when true allows the use of it with
```typescript
import { EncryptStorage } from 'encrypt-storage';

export const encryptStorage = new EncryptStorage('secret-key', {
export const encryptStorage = new EncryptStorage('secret-key-value', {
stateManagementUse: true,
});
```
Expand All @@ -196,19 +197,31 @@ default `AES` - Is the selected encryption algorithm.:
```typescript
import { EncryptStorage } from 'encrypt-storage';

export const encryptStorage = new EncryptStorage('secret-key', {
export const encryptStorage = new EncryptStorage('secret-key-value', {
encAlgorithm: 'Rabbit',
});
```

#### *doNotEncryptValues*

default `false` - This option `NOT` encrypt values, but use those options like `prefix` our `multiple-instances`.:

```typescript
import { EncryptStorage } from 'encrypt-storage';

export const encryptStorage = new EncryptStorage('secret-key-value', {
doNotEncryptValues: true,
});
```

### Methods

From here, we will have the following code as the EncryptStorage instance model:

```typescript
import { EncryptStorage } from 'encrypt-storage';

export const encryptStorage = new EncryptStorage('secret-key', {
export const encryptStorage = new EncryptStorage('secret-key-value', {
prefix: '@example',
});
```
Expand Down Expand Up @@ -408,7 +421,7 @@ example:
```typescript
import { AsyncEncryptStorage } from 'encrypt-storage';

export const encryptStorage = new AsyncEncryptStorage('secret-key', options);
export const encryptStorage = new AsyncEncryptStorage('secret-key-value', options);

async function getDecryptedValue('key'): Promise<any | undefined> {
const value = await encryptStorage.getItem('key');
Expand All @@ -423,7 +436,7 @@ In the case of `aws-amplify`, if you want to use the facility of not needing to
import Amplify from 'aws-amplify';
import { EncryptStorage } from 'encrypt-storage';

const encryptStorage = new EncryptStorage('secret_key', {
const encryptStorage = new EncryptStorage('secret-key-value', {
...,
stateManagementUse: true,
});
Expand Down Expand Up @@ -462,7 +475,7 @@ const vuexLocal = new VuexPersistence<RootState>({
// ...
import { AsyncEncryptStorage } from 'encrypt-storage';

export const encryptStorage = new AsyncEncryptStorage('secret-key', options);
export const encryptStorage = new AsyncEncryptStorage('secret-key-value', options);

const persistConfig = {
key: 'root',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "encrypt-storage",
"version": "2.5.2",
"version": "2.6.0",
"description": "Wrapper for encrypted localStorage and sessionStorage in browser",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
19 changes: 13 additions & 6 deletions src/encrypt-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class EncryptStorage implements EncryptStorageInterface {

readonly #stateManagementUse: boolean;

readonly #doNotEncryptValues: boolean;

/**
* EncryptStorage provides a wrapper implementation of `localStorage` and `sessionStorage` for a better security solution in browser data store
*
Expand All @@ -34,13 +36,15 @@ export class EncryptStorage implements EncryptStorageInterface {
prefix = '',
stateManagementUse = false,
encAlgorithm = 'AES',
doNotEncryptValues = false,
} = options || {};

secret.set(this, secretKey);

this.storage = window[storageType];
this.#prefix = prefix;
this.#stateManagementUse = stateManagementUse;
this.#doNotEncryptValues = doNotEncryptValues;
this.#encriptation = getEncriptation(encAlgorithm, secret.get(this));
}

Expand All @@ -52,23 +56,25 @@ export class EncryptStorage implements EncryptStorageInterface {
return this.storage.length || 0;
}

public setItem(key: string, value: any, doNotEncrypt?: boolean): void {
public setItem(key: string, value: any, doNotEncrypt = false): void {
const encryptValues = this.#doNotEncryptValues || doNotEncrypt;
const storageKey = this.#getKey(key);
const valueToString =
typeof value === 'object' ? JSON.stringify(value) : String(value);
const encryptedValue = doNotEncrypt
const encryptedValue = encryptValues
? valueToString
: this.#encriptation.encrypt(valueToString);

this.storage.setItem(storageKey, encryptedValue);
}

public getItem<T = any>(key: string, doNotDecrypt?: boolean): T | undefined {
public getItem<T = any>(key: string, doNotDecrypt = false): T | undefined {
const decryptValues = this.#doNotEncryptValues || doNotDecrypt;
const storageKey = this.#getKey(key);
const item = this.storage.getItem(storageKey);

if (item) {
const decryptedValue = doNotDecrypt
const decryptedValue = decryptValues
? item
: this.#encriptation.decrypt(item);

Expand Down Expand Up @@ -118,7 +124,8 @@ export class EncryptStorage implements EncryptStorageInterface {
pattern: string,
options: GetFromPatternOptions = {} as GetFromPatternOptions,
): Record<string, any> | undefined {
const { multiple = true, exact = false } = options;
const { multiple = true, exact = false, doNotDecrypt = false } = options;
const decryptValues = this.#doNotEncryptValues || doNotDecrypt;
const keys = Object.keys(this.storage).filter(key => {
if (exact) {
return key === this.#getKey(pattern);
Expand All @@ -142,7 +149,7 @@ export class EncryptStorage implements EncryptStorageInterface {
? key.replace(`${this.#prefix}:`, '')
: key;

return this.getItem(formattedKey);
return this.getItem(formattedKey, decryptValues);
}

const value = keys.reduce((accumulator: Record<string, any>, key) => {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface EncryptStorageOptions {
stateManagementUse?: boolean;
storageType?: StorageType;
encAlgorithm?: EncAlgorithm;
doNotEncryptValues?: boolean;
}

export interface RemoveFromPatternOptions {
Expand All @@ -20,6 +21,7 @@ export interface RemoveFromPatternOptions {

export interface GetFromPatternOptions extends RemoveFromPatternOptions {
multiple?: boolean;
doNotDecrypt?: boolean;
}

export interface EncryptStorageInterface extends Storage {
Expand Down

0 comments on commit 80acdb2

Please sign in to comment.