Adds update restore from backup functionality

This commit is contained in:
Alicia Sykes 2021-05-24 18:59:35 +01:00
parent bdb6e310e8
commit 0e43e91d84
2 changed files with 53 additions and 27 deletions

View File

@ -49,7 +49,7 @@
label="Password" label="Password"
type="password" type="password"
/> />
<Button> <Button :click="restoreBackup">
<template v-slot:text>Restore</template> <template v-slot:text>Restore</template>
<template v-slot:icon><IconRestore /></template> <template v-slot:icon><IconRestore /></template>
</Button> </Button>
@ -64,7 +64,7 @@ import Button from '@/components/FormElements/Button';
import Input from '@/components/FormElements/Input'; import Input from '@/components/FormElements/Input';
import IconBackup from '@/assets/interface-icons/config-backup.svg'; import IconBackup from '@/assets/interface-icons/config-backup.svg';
import IconRestore from '@/assets/interface-icons/config-restore.svg'; import IconRestore from '@/assets/interface-icons/config-restore.svg';
import { backup, update } from '@/utils/CloudBackup'; import { backup, update, restore } from '@/utils/CloudBackup';
import { localStorageKeys } from '@/utils/defaults'; import { localStorageKeys } from '@/utils/defaults';
export default { export default {
@ -87,6 +87,14 @@ export default {
IconRestore, IconRestore,
}, },
methods: { methods: {
restoreBackup() {
restore(this.restoreCode, this.restorePassword)
.then((response) => {
this.restoreFromBackup(response, this.restoreCode);
}).catch((msg) => {
this.showErrorMsg(msg);
});
},
checkPass() { checkPass() {
const savedHash = localStorage[localStorageKeys.BACKUP_HASH] || undefined; const savedHash = localStorage[localStorageKeys.BACKUP_HASH] || undefined;
if (!savedHash) { if (!savedHash) {
@ -121,10 +129,19 @@ export default {
this.showErrorMsg('Unable to process request'); this.showErrorMsg('Unable to process request');
}); });
}, },
restoreFromBackup(config, backupId) {
localStorage.setItem(localStorageKeys.CONF_SECTIONS, JSON.stringify(config.sections));
localStorage.setItem(localStorageKeys.APP_CONFIG, JSON.stringify(config.appConfig));
localStorage.setItem(localStorageKeys.PAGE_INFO, JSON.stringify(config.pageInfo));
if (config.appConfig.theme) {
localStorage.setItem(localStorageKeys.THEME, config.appConfig.theme);
}
this.setBackupIdLocally(backupId, this.restorePassword);
this.showSuccessMsg('Config Restored Succesfully');
setTimeout(() => { location.reload(); }, 1500); // eslint-disable-line no-restricted-globals
},
updateUiAfterBackup(backupId, isUpdate = false) { updateUiAfterBackup(backupId, isUpdate = false) {
const hash = this.makeHash(this.backupPassword); this.setBackupIdLocally(backupId, this.backupPassword);
localStorage.setItem(localStorageKeys.BACKUP_ID, backupId);
localStorage.setItem(localStorageKeys.BACKUP_HASH, hash);
this.showSuccessMsg(`${isUpdate ? 'Update' : 'Backup'} Completed Succesfully`); this.showSuccessMsg(`${isUpdate ? 'Update' : 'Backup'} Completed Succesfully`);
this.backupPassword = ''; this.backupPassword = '';
}, },
@ -137,6 +154,12 @@ export default {
makeHash(pass) { makeHash(pass) {
return sha256(pass).toString(); return sha256(pass).toString();
}, },
setBackupIdLocally(backupId, pass) {
this.backupId = backupId;
const hash = this.makeHash(pass);
localStorage.setItem(localStorageKeys.BACKUP_ID, backupId);
localStorage.setItem(localStorageKeys.BACKUP_HASH, hash);
},
}, },
}; };
</script> </script>

View File

@ -1,10 +1,9 @@
/* eslint-disable */
import sha256 from 'crypto-js/sha256'; import sha256 from 'crypto-js/sha256';
import aes from 'crypto-js/aes'; import aes from 'crypto-js/aes';
import Utf8 from 'crypto-js/enc-utf8'; import Utf8 from 'crypto-js/enc-utf8';
import axios from 'axios'; import axios from 'axios';
const ENDPOINT = 'https://dashy-sync-service.as93.net/'; const ENDPOINT = 'https://dashy-sync-service.as93.net';
/* Stringify, encrypt and encode data for transmission */ /* Stringify, encrypt and encode data for transmission */
const encryptData = (data, password) => { const encryptData = (data, password) => {
@ -14,34 +13,38 @@ const encryptData = (data, password) => {
}; };
/* Decrypt, decode and parse received data */ /* Decrypt, decode and parse received data */
const decryptData = (data, password) => { const decryptData = (data, password) => aes.decrypt(data, password).toString(Utf8);
return aes.decrypt(data, password).toString(Utf8);
};
/* Returns a splice of the hash of the users password */ /* Returns a splice of the hash of the users password */
const makeSubHash = (pass) => sha256(pass).toString().slice(0, 14); const makeSubHash = (pass) => sha256(pass).toString().slice(0, 14);
/* Makes the backup */ /* Makes the backup */
export const backup = (data, password) => { export const backup = (data, password) => axios.post(ENDPOINT, {
return axios.post(ENDPOINT, { userData: encryptData(data, password),
userData: encryptData(data, password), subHash: makeSubHash(password),
subHash: makeSubHash(password), });
});
};
/* Updates and existing backup */ /* Updates and existing backup */
export const update = (data, password, backupId) => { export const update = (data, password, backupId) => axios.put(ENDPOINT, {
return axios.put(ENDPOINT, { backupId,
backupId, userData: encryptData(data, password),
userData: encryptData(data, password), subHash: makeSubHash(password),
subHash: makeSubHash(password), });
});
}; const encodeGetParams = p => Object.entries(p).map(kv => kv.map(encodeURIComponent).join('=')).join('&');
/* Restores the backup */ /* Restores the backup */
export const restore = (backupId, password) => { export const restore = (backupId, password) => {
// return axios.get(ENDPOINT, { const params = encodeGetParams({ backupId, subHash: makeSubHash(password) });
// backupId, const url = `${ENDPOINT}/?${params}`;
// subHash: makeSubHash(password), return new Promise((resolve, reject) => {
// }); axios.get(url).then((response) => {
if (!response.data || response.data.errorMsg) {
reject(response.data.errorMsg || 'Error');
} else {
const decryptedData = decryptData(response.data.userData.userData, password);
try { resolve(JSON.parse(decryptedData)); } catch (e) { reject(e); }
}
});
});
}; };