From 472156529dcd73caf87e216231b5a5bb869ba417 Mon Sep 17 00:00:00 2001 From: Sarah Vaupel Date: Tue, 28 Jan 2020 18:14:04 +0100 Subject: [PATCH] chore(storage-key): fetch storage key and invalidate storage on mismatch --- .../lib/storage-manager/storage-manager.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/frontend/src/lib/storage-manager/storage-manager.js b/frontend/src/lib/storage-manager/storage-manager.js index 4f491e92c..ed2bad7cc 100644 --- a/frontend/src/lib/storage-manager/storage-manager.js +++ b/frontend/src/lib/storage-manager/storage-manager.js @@ -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) {