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"
type="password"
/>
<Button>
<Button :click="restoreBackup">
<template v-slot:text>Restore</template>
<template v-slot:icon><IconRestore /></template>
</Button>
@ -64,7 +64,7 @@ import Button from '@/components/FormElements/Button';
import Input from '@/components/FormElements/Input';
import IconBackup from '@/assets/interface-icons/config-backup.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';
export default {
@ -87,6 +87,14 @@ export default {
IconRestore,
},
methods: {
restoreBackup() {
restore(this.restoreCode, this.restorePassword)
.then((response) => {
this.restoreFromBackup(response, this.restoreCode);
}).catch((msg) => {
this.showErrorMsg(msg);
});
},
checkPass() {
const savedHash = localStorage[localStorageKeys.BACKUP_HASH] || undefined;
if (!savedHash) {
@ -121,10 +129,19 @@ export default {
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) {
const hash = this.makeHash(this.backupPassword);
localStorage.setItem(localStorageKeys.BACKUP_ID, backupId);
localStorage.setItem(localStorageKeys.BACKUP_HASH, hash);
this.setBackupIdLocally(backupId, this.backupPassword);
this.showSuccessMsg(`${isUpdate ? 'Update' : 'Backup'} Completed Succesfully`);
this.backupPassword = '';
},
@ -137,6 +154,12 @@ export default {
makeHash(pass) {
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>

View File

@ -1,10 +1,9 @@
/* eslint-disable */
import sha256 from 'crypto-js/sha256';
import aes from 'crypto-js/aes';
import Utf8 from 'crypto-js/enc-utf8';
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 */
const encryptData = (data, password) => {
@ -14,34 +13,38 @@ const encryptData = (data, password) => {
};
/* Decrypt, decode and parse received data */
const decryptData = (data, password) => {
return aes.decrypt(data, password).toString(Utf8);
};
const decryptData = (data, password) => aes.decrypt(data, password).toString(Utf8);
/* Returns a splice of the hash of the users password */
const makeSubHash = (pass) => sha256(pass).toString().slice(0, 14);
/* Makes the backup */
export const backup = (data, password) => {
return axios.post(ENDPOINT, {
userData: encryptData(data, password),
subHash: makeSubHash(password),
});
};
export const backup = (data, password) => axios.post(ENDPOINT, {
userData: encryptData(data, password),
subHash: makeSubHash(password),
});
/* Updates and existing backup */
export const update = (data, password, backupId) => {
return axios.put(ENDPOINT, {
backupId,
userData: encryptData(data, password),
subHash: makeSubHash(password),
});
};
export const update = (data, password, backupId) => axios.put(ENDPOINT, {
backupId,
userData: encryptData(data, password),
subHash: makeSubHash(password),
});
const encodeGetParams = p => Object.entries(p).map(kv => kv.map(encodeURIComponent).join('=')).join('&');
/* Restores the backup */
export const restore = (backupId, password) => {
// return axios.get(ENDPOINT, {
// backupId,
// subHash: makeSubHash(password),
// });
const params = encodeGetParams({ backupId, subHash: makeSubHash(password) });
const url = `${ENDPOINT}/?${params}`;
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); }
}
});
});
};