chore(storage-key): fetch storage key and invalidate storage on mismatch

This commit is contained in:
Sarah Vaupel 2020-01-28 18:14:04 +01:00 committed by Gregor Kleen
parent c86dce5a11
commit 472156529d

View File

@ -2,6 +2,8 @@
import * as semver from 'semver';
import { HttpClient } from '../../services/http-client/http-client';
export const LOCATION = {
LOCAL: 'local',
SESSION: 'session',
@ -38,6 +40,30 @@ export class StorageManager {
this._global = window;
else
throw new Error('Cannot setup StorageManager without window or global');
if (this._options.encryption) {
const requestBody = {
type : this._options.encryption,
length : 42,
...this.load('encryption'),
};
this._global.App.httpClient.post({
url: '../../../../../../user/storage-key', // TODO use APPROOT instead
headers: {
'Content-Type' : HttpClient.ACCEPT.JSON,
'Accept' : HttpClient.ACCEPT.JSON,
},
body: JSON.stringify(requestBody),
}).then(
(response) => response.json()
).then((response) => {
console.log('storage-manager got key from response:', response);
if (response.salt !== requestBody.salt || response.timestamp !== requestBody.timestamp) {
this.clear();
}
this.save('encryption', response);
}).catch(console.error);
}
}
save(key, value, options=this._options) {