ServerStorage.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <div></div>
  3. </template>
  4. <script>
  5. //-----------------------------------------------------------------------------
  6. import Vue from 'vue';
  7. import Component from 'vue-class-component';
  8. import _ from 'lodash';
  9. import bookManager from '../share/bookManager';
  10. import readerApi from '../../../api/reader';
  11. import * as utils from '../../../share/utils';
  12. import * as cryptoUtils from '../../../share/cryptoUtils';
  13. export default @Component({
  14. })
  15. class ServerStorage extends Vue {
  16. created() {
  17. this.commit = this.$store.commit;
  18. }
  19. async init() {
  20. if (!this.serverStorageKey) {
  21. //генерируем новый ключ
  22. this.generateNewServerStorageKey();
  23. }
  24. this.hashedStorageKey = utils.toBase58(await cryptoUtils.sha256(this.serverStorageKey));
  25. //console.log(await this.storageSet({'id1': {rev: 1, data: {test: 123}}}));
  26. //console.log(await this.storageGet({'id1': {}}));
  27. //console.log(await this.storageCheck({'id1': {rev: 1, data: {test: 123}}}));
  28. }
  29. get settings() {
  30. return this.$store.state.reader.settings;
  31. }
  32. get serverStorageKey() {
  33. return this.$store.state.reader.serverStorageKey;
  34. }
  35. generateNewServerStorageKey() {
  36. const key = utils.toBase58(utils.randomArray(32));
  37. this.commit('reader/setServerStorageKey', key);
  38. }
  39. async storageCheck(items) {
  40. return await this.storageApi('check', items);
  41. }
  42. async storageGet(items) {
  43. return await this.storageApi('get', items);
  44. }
  45. async storageSet(items, force) {
  46. return await this.storageApi('set', items, force);
  47. }
  48. async storageApi(action, items, force) {
  49. const request = {action, items};
  50. if (force)
  51. request.force = true;
  52. const encodedRequest = await this.encodeStorageItems(request);
  53. return await this.decodeStorageItems(await readerApi.storage(encodedRequest));
  54. }
  55. async encodeStorageItems(request) {
  56. if (!this.hashedStorageKey)
  57. throw new Error('hashedStorageKey is empty');
  58. if (!_.isObject(request.items))
  59. throw new Error('items is not an object');
  60. let result = Object.assign({}, request);
  61. let items = {};
  62. for (const id of Object.keys(request.items)) {
  63. const item = request.items[id];
  64. if (request.action == 'set' && !_.isObject(item.data))
  65. throw new Error('encodeStorageItems: data is not an object');
  66. let encoded = Object.assign({}, item);
  67. if (item.data) {
  68. const comp = utils.pako.deflate(JSON.stringify(item.data), {level: 1});
  69. let encrypted = null;
  70. try {
  71. encrypted = await cryptoUtils.aesEncrypt(comp, this.serverStorageKey);
  72. } catch (e) {
  73. throw new Error('encrypt failed');
  74. }
  75. encoded.data = '1' + utils.toBase64(encrypted);
  76. }
  77. items[`${this.hashedStorageKey}.${utils.toBase58(id)}`] = encoded;
  78. }
  79. result.items = items;
  80. return result;
  81. }
  82. async decodeStorageItems(response) {
  83. if (!this.hashedStorageKey)
  84. throw new Error('hashedStorageKey is empty');
  85. let result = Object.assign({}, response);
  86. let items = {};
  87. if (response.items) {
  88. if (!_.isObject(response.items))
  89. throw new Error('items is not an object');
  90. for (const id of Object.keys(response.items)) {
  91. const item = response.items[id];
  92. let decoded = Object.assign({}, item);
  93. if (item.data) {
  94. if (!_.isString(item.data) || !item.data.length)
  95. throw new Error('decodeStorageItems: data is not a string');
  96. if (item.data[0] !== '1')
  97. throw new Error('decodeStorageItems: unknown data format');
  98. const a = utils.fromBase64(item.data.substr(1));
  99. let decrypted = null;
  100. try {
  101. decrypted = await cryptoUtils.aesDecrypt(a, this.serverStorageKey);
  102. } catch (e) {
  103. throw new Error('decrypt failed');
  104. }
  105. decoded.data = JSON.parse(utils.pako.inflate(decrypted, {to: 'string'}));
  106. }
  107. const ids = id.split('.');
  108. if (!(ids.length == 2) || !(ids[0] == this.hashedStorageKey))
  109. throw new Error(`decodeStorageItems: bad id - ${id}`);
  110. items[utils.fromBase58(ids[1])] = decoded;
  111. }
  112. }
  113. result.items = items;
  114. return result;
  115. }
  116. }
  117. //-----------------------------------------------------------------------------
  118. </script>