@botpress/webchat 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,8 @@ import { Config } from '../typings';
3
3
  export default class BpSocket {
4
4
  socket: MessagingSocket;
5
5
  private chatId;
6
+ private encryptionKey;
7
+ private clientId;
6
8
  private waitingForUser?;
7
9
  onClear: (event: any) => void;
8
10
  onMessage: (event: any) => void;
@@ -8,8 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  const messaging_socket_1 = require("@botpress/messaging-socket");
16
+ const aes_1 = __importDefault(require("crypto-js/aes"));
17
+ const enc_utf8_1 = __importDefault(require("crypto-js/enc-utf8"));
18
+ const sha256_1 = __importDefault(require("crypto-js/sha256"));
13
19
  class BpSocket {
14
20
  constructor(config) {
15
21
  this.postToParent = (_type, payload) => {
@@ -18,6 +24,8 @@ class BpSocket {
18
24
  (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage(Object.assign(Object.assign({}, payload), { chatId: this.chatId }), '*');
19
25
  };
20
26
  this.chatId = config.chatId;
27
+ this.encryptionKey = config.encryptionKey;
28
+ this.clientId = config.clientId;
21
29
  this.socket = new messaging_socket_1.MessagingSocket({ url: config.messagingUrl, clientId: config.clientId });
22
30
  }
23
31
  setup() {
@@ -64,23 +72,39 @@ class BpSocket {
64
72
  });
65
73
  }
66
74
  getStorage(key) {
67
- const stored = localStorage.getItem(this.getStorageKey(key));
75
+ var _a;
76
+ let stored = localStorage.getItem(this.getStorageKey(key));
68
77
  if (!stored) {
69
78
  return undefined;
70
79
  }
80
+ if ((_a = this.encryptionKey) === null || _a === void 0 ? void 0 : _a.length) {
81
+ stored = aes_1.default.decrypt(stored, this.encryptionKey).toString(enc_utf8_1.default);
82
+ }
71
83
  try {
72
84
  const val = JSON.parse(stored);
73
85
  return val;
74
86
  }
75
- catch (_a) {
87
+ catch (_b) {
76
88
  return undefined;
77
89
  }
78
90
  }
79
91
  setStorage(key, object) {
80
- localStorage.setItem(this.getStorageKey(key), JSON.stringify(object));
92
+ var _a;
93
+ let string = JSON.stringify(object);
94
+ if ((_a = this.encryptionKey) === null || _a === void 0 ? void 0 : _a.length) {
95
+ string = aes_1.default.encrypt(string, this.encryptionKey).toString();
96
+ }
97
+ localStorage.setItem(this.getStorageKey(key), string);
81
98
  }
82
99
  getStorageKey(key) {
83
- return `bp-chat-${key}`;
100
+ var _a;
101
+ const rawKey = `bp-chat-${key}`;
102
+ if ((_a = this.encryptionKey) === null || _a === void 0 ? void 0 : _a.length) {
103
+ return `${rawKey}-${(0, sha256_1.default)(`${this.clientId}-${this.encryptionKey}`).toString()}`;
104
+ }
105
+ else {
106
+ return `${rawKey}-${this.clientId}`;
107
+ }
84
108
  }
85
109
  }
86
110
  exports.default = BpSocket;
package/dist/typings.d.ts CHANGED
@@ -206,6 +206,8 @@ export interface Config {
206
206
  chatId?: string;
207
207
  /** CSS class to be applied to iframe */
208
208
  className?: string;
209
+ /** Key used to encrypt data in the localStorage */
210
+ encryptionKey?: string;
209
211
  }
210
212
  export declare type OverridableComponents = 'below_conversation' | 'before_container' | 'composer';
211
213
  export interface Overrides {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botpress/webchat",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "source": "src/index.tsx",
@@ -14,6 +14,7 @@
14
14
  "dist"
15
15
  ],
16
16
  "devDependencies": {
17
+ "@types/crypto-js": "^4.1.1",
17
18
  "@types/lodash": "^4.14.178",
18
19
  "@types/mime": "^2.0.3",
19
20
  "@types/node": "^16.11.13",
@@ -31,6 +32,7 @@
31
32
  "@juggle/resize-observer": "^3.0.2",
32
33
  "axios": "^0.25.0",
33
34
  "classnames": "^2.3.1",
35
+ "crypto-js": "^4.1.1",
34
36
  "date-fns": "^1.30.1",
35
37
  "lodash": "^4.17.21",
36
38
  "mime": "^3.0.0",