`preStoreInit` doesn't behave as expected
micobarac opened this issue · 6 comments
I am using preStoreInit
and preStorageUpdate
for symmetric encryption of local storage store data.
import { Settings } from '@app/app.settings';
import CryptoJS from 'crypto-js';
export class CryptUtil {
static readonly Key: string = Settings.Key;
static encrypt(value: any): any {
return CryptoJS.AES.encrypt(JSON.stringify(value), CryptUtil.Key).toString();
}
static decrypt(encryptedValue: any): any {
const bytes = CryptoJS.AES.decrypt(encryptedValue, CryptUtil.Key);
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
}
}
import { Injectable } from '@angular/core';
import { Settings } from '@app/app.settings';
import { User } from '@content/administration/user/user.model';
import { StoreValue, createStore, select, withProps } from '@ngneat/elf';
import { localStorageStrategy, persistState } from '@ngneat/elf-persist-state';
import { CryptUtil } from '@shared/helpers/crypt.util';
const name: string = Settings.Prefix + 'account';
interface AccountProps {
user: User | null;
}
const accountStore = createStore({ name }, withProps<AccountProps>({ user: null }));
const preStoreInit = (state: StoreValue<any>) => {
return CryptUtil.decrypt(state);
};
const preStorageUpdate = (storeName: string, state: Partial<AccountProps>) => {
return CryptUtil.encrypt(state);
};
export const persist = persistState(accountStore, {
key: name,
storage: localStorageStrategy,
preStoreInit,
preStorageUpdate
});
@Injectable({
providedIn: 'root'
})
export class AccountRepository {
user$ = accountStore.pipe(select((state) => state.user));
setAccount(user: User) {
accountStore.update((state) => ({ ...state, user }));
}
reset() {
accountStore.reset();
}
}
This is the encrypted value written in local storage:
"U2FsdGVkX1+o9DoUgu7oH0Wkzo5u0hXGrYr39oFJ5gvZ9hZ8GoHuJN8TBBiuKzJkZjmi02mX+dtCs+rJpqIEac5FAwQ9O/3QawbfC21tVVRA657eLhPWDC02mR++UMCFfZySzkvkZBZlLMYp+XnH+5mXrecT/moIcewfHa9AtcQxnmEzd8cKcZK2D/XFlQI1cxQHJAg5jot/Vp8X+kL36MF/qcPuE+TuZVj9a1E3G6vLmTEvhLKaydw/40/n2pUxHbGG/5AdeCp7aFH0OQD2UgepD6bxD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uGIoYnl+ONBJT9L2/pgCWDGioPN+5fiafVHAW58bdelsvUV829a3AFEeuhpkZmaZElbRYPjOSlnCW4ZTpO0LyrX2Ghcr8Qw7C7YI1iSBCOcZ+r3j+CDTZJcrjVuvqHiXS6xs+Tc0VBw6sS0Ri1ukXeN+BY4Uas+wpN0IU5p/V5qnxpyrcbves50SOGNwVPGQnrjPMO4oKiFc8uHL3tmFSSfiS05D9hi1t/oLdPi51i87/LxoI9fQNBovIs06fdft..."
The problem is, when I try to decrypt, using preStoreInit
, the local storage string value is converted into an object, making it impossible to decrypt the value back:
{"0":"U","1":"2","2":"F","3":"s","4":"d","5":"G","6":"V","7":"k","8":"X","9":"1","10":"+","11":"o","12":"9","13":"D","14":"o","15":"U","16":"g","17":"u","18":"7","19":"o","20":"H","21":"0","22":"W","23":"k","24":"z","25":"o","26":"5","27":"u","28":"0","29":"h","30":"X","31":"G","32":"r","33":"Y","34":"r","35":"3","36":"9","37":"o","38":"F","39":"J","40":"5","41":"g","42":"v","43":"Z","44":"9","45":"h","46":"Z","47":"8","48":"G","49":"o","50":"H","51":"u","52":"J","53":"N","54":"8","55":"T","56":"B","57":"B","58":"i","59":"u","60":"K","61":"z","62":"J","63":"k","64":"Z","65":"j","66":"m","67":"i","68":"0","69":"2","70":"m","71":"X","72":"+","73":"d","74":"t","75":"C","76":"s","77":"+","78":"r","79":"J","80":"p","81":"q","82":"I","83":"E","84":"a","85":"c","86":"5","87":"F","88":"A","89":"w","90":"Q","91":"9","92":"O","93":"/","94":"3","95":"Q","96":"a","97":"w","98":"b","99":"f","100":"C","101":"2","102":"1","103":"t","104":"V","105":"V","106":"R","107":"A","108":"6","109":"5","110":"7","111":"e","112":"L","113":"h","114":"P","115":"W","116":"D","117":"C","118":"0","119":"2","120":"m","121":"R","122":"+","123":"+","124":"U","125":"M","126":"C","127":"F","128":"f","129":"Z","130":"y","131":"S","132":"z","133":"k","134":"v","135":"k","136":"Z","137":"B","138":"Z","139":"l","140":"L","141":"M","142":"Y","143":"p","144":"+","145":"X","146":"n","147":"H","148":"+","149":"5","150":"m"...,"user":null}
Btw, do the the following methods declarations support the scenario (StoreValue<S>
)?:
preStoreInit?: (value: StoreValue<S>) => Partial<StoreValue<S>>;
preStorageUpdate?: (storeName: string, state: Partial<StoreValue<S>>) => Partial<StoreValue<S>>;
A store should contain only objects so it's OK. You can stringily it and then decrypt.
If I do:
static decrypt(encryptedValue: string): any {
const bytes = CryptoJS.AES.decrypt(JSON.stringify(encryptedValue), CryptUtil.Key);
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
}
I get Malformed UTF-8 data
.
The stored encryptedValue
should be passed, in that case, in its original format (type), for the encryption/decryption to work. It looks as though the stored local storage value gets formatted into object like Object.assign({}, account)
, thus, passing preStoreInit
object, instead of encrypted string.
So, maybe, the crucial declarations might look like this, to support this scenario:
preStoreInit?: (value: StoreValue<S> | string) => Partial<StoreValue<S>>;
preStorageUpdate?: (storeName: string, state: Partial<StoreValue<S>>) => Partial<StoreValue<S>> | string;
If it's an object, convert it to store value, otherwise, allow these methods' implementation to do the conversion.
I managed to get it working like this, but it's a hacky and ugly solution:
const preStoreInit = (state: StoreValue<any>) => {
const { user, ...encryptedValue } = state;
const value = Object.values(encryptedValue).join('');
return CryptUtil.decrypt(value);
};
Maybe, changing persist-state.ts
like this can allow primitive string values from local storage:
import { isObject } from '../store/utils';
const loadFromStorageSubscription = from(
storage.getItem(merged.key!)
).subscribe((value) => {
if (value) {
store.update((state) => {
return merged.preStoreInit!({
...state,
isObject(value)
? {
...value,
}
: value,
});
});
}
initialized.next(true);
initialized.complete();
});
You're welcome to create a PR that accepts preStoreInitValueParser
option