feat(storage-manager): store encryption info per location

This commit is contained in:
Sarah Vaupel 2020-01-29 10:53:42 +01:00
parent 8bee033efa
commit 8122ab10b0
2 changed files with 39 additions and 25 deletions

View File

@ -19,7 +19,8 @@ export class StorageManager {
_options;
_global;
_encryptionKey;
_encryptionKey = {};
constructor(namespace, version, options) {
this.namespace = namespace;
@ -43,30 +44,11 @@ export class StorageManager {
else
throw new Error('Cannot setup StorageManager without window or global');
// TODO handle salt and timestamp separately per location
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', { salt: response.salt, timestamp: response.timestamp });
this._encryptionKey = response.key;
}).catch(console.error);
Object.values(LOCATION).forEach((location) => {
const encryption = this._options.encryption.all || this._options.encryption[location];
if (encryption) this._requestStorageKey({ location: location, encryption: encryption });
});
}
}
@ -149,6 +131,7 @@ export class StorageManager {
return this._saveToLocalStorage(val);
}
// TODO add LOCATION.SESSION
case LOCATION.WINDOW: {
let val = this._getFromWindow();
@ -173,6 +156,7 @@ export class StorageManager {
switch (location) {
case LOCATION.LOCAL:
return this._clearLocalStorage();
// TODO add LOCATION.SESSION
case LOCATION.WINDOW:
return this._clearWindow();
default:
@ -302,4 +286,34 @@ export class StorageManager {
_clearSessionStorage() {
window.sessionStorage.removeItem(this.namespace);
}
_requestStorageKey(options=this._options) {
if (!(options && options.location && options.encryption))
throw new Error('Storage Manager cannot request storage key with unsupported options!');
const requestBody = {
type : options.encryption,
length : 42,
...this.load('encryption', options),
};
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, 'with options:', options);
if (response.salt !== requestBody.salt || response.timestamp !== requestBody.timestamp) {
this.clear(options);
}
this.save('encryption', { salt: response.salt, timestamp: response.timestamp }, options);
this._encryptionKey[options.location] = response.key;
}).catch(console.error);
}
}

View File

@ -64,7 +64,7 @@ export class ExamCorrect {
this._element = element;
this._app = app;
this._storageManager = new StorageManager('EXAM_CORRECT', '0.0.0', { location: LOCATION.SESSION, encryption: { tag: 'exam-correct', exam: this._element.getAttribute('uw-exam-correct') } });
this._storageManager = new StorageManager('EXAM_CORRECT', '0.0.0', { location: LOCATION.SESSION, encryption: { all: { tag: 'exam-correct', exam: this._element.getAttribute('uw-exam-correct') } } });
this._sendBtn = document.getElementById(EXAM_CORRECT_SEND_BTN_ID);
this._userInput = document.getElementById(EXAM_CORRECT_USER_INPUT_ID);