1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | import * as utils from "../../utils";
|
25 | import {logger} from '../../logger';
|
26 |
|
27 | const DEBUG = false;
|
28 | const E2E_PREFIX = "session.e2e.";
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | export function WebStorageSessionStore(webStore) {
|
40 | this.store = webStore;
|
41 | if (!utils.isFunction(webStore.getItem) ||
|
42 | !utils.isFunction(webStore.setItem) ||
|
43 | !utils.isFunction(webStore.removeItem) ||
|
44 | !utils.isFunction(webStore.key) ||
|
45 | typeof(webStore.length) !== 'number'
|
46 | ) {
|
47 | throw new Error(
|
48 | "Supplied webStore does not meet the WebStorage API interface",
|
49 | );
|
50 | }
|
51 | }
|
52 |
|
53 | WebStorageSessionStore.prototype = {
|
54 | |
55 |
|
56 |
|
57 | removeEndToEndAccount: function() {
|
58 | this.store.removeItem(KEY_END_TO_END_ACCOUNT);
|
59 | },
|
60 |
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | getEndToEndAccount: function() {
|
69 | return this.store.getItem(KEY_END_TO_END_ACCOUNT);
|
70 | },
|
71 |
|
72 | |
73 |
|
74 |
|
75 |
|
76 | getAllEndToEndDevices: function() {
|
77 | const prefix = keyEndToEndDevicesForUser('');
|
78 | const devices = {};
|
79 | for (let i = 0; i < this.store.length; ++i) {
|
80 | const key = this.store.key(i);
|
81 | const userId = key.substr(prefix.length);
|
82 | if (key.startsWith(prefix)) devices[userId] = getJsonItem(this.store, key);
|
83 | }
|
84 | return devices;
|
85 | },
|
86 |
|
87 | getEndToEndDeviceTrackingStatus: function() {
|
88 | return getJsonItem(this.store, KEY_END_TO_END_DEVICE_LIST_TRACKING_STATUS);
|
89 | },
|
90 |
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 | getEndToEndDeviceSyncToken: function() {
|
97 | return getJsonItem(this.store, KEY_END_TO_END_DEVICE_SYNC_TOKEN);
|
98 | },
|
99 |
|
100 | |
101 |
|
102 |
|
103 | removeEndToEndDeviceData: function() {
|
104 | removeByPrefix(this.store, keyEndToEndDevicesForUser(''));
|
105 | removeByPrefix(this.store, KEY_END_TO_END_DEVICE_LIST_TRACKING_STATUS);
|
106 | removeByPrefix(this.store, KEY_END_TO_END_DEVICE_SYNC_TOKEN);
|
107 | },
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | getEndToEndSessions: function(deviceKey) {
|
116 | return getJsonItem(this.store, keyEndToEndSessions(deviceKey));
|
117 | },
|
118 |
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 | getAllEndToEndSessions: function() {
|
125 | const deviceKeys = getKeysWithPrefix(this.store, keyEndToEndSessions(''));
|
126 | const results = {};
|
127 | for (const k of deviceKeys) {
|
128 | const unprefixedKey = k.substr(keyEndToEndSessions('').length);
|
129 | results[unprefixedKey] = getJsonItem(this.store, k);
|
130 | }
|
131 | return results;
|
132 | },
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 | removeAllEndToEndSessions: function() {
|
139 | removeByPrefix(this.store, keyEndToEndSessions(''));
|
140 | },
|
141 |
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 | getAllEndToEndInboundGroupSessionKeys: function() {
|
148 | const prefix = E2E_PREFIX + 'inboundgroupsessions/';
|
149 | const result = [];
|
150 | for (let i = 0; i < this.store.length; i++) {
|
151 | const key = this.store.key(i);
|
152 | if (!key.startsWith(prefix)) {
|
153 | continue;
|
154 | }
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | result.push({
|
161 | senderKey: key.substr(prefix.length, 43),
|
162 | sessionId: key.substr(prefix.length + 44),
|
163 | });
|
164 | }
|
165 | return result;
|
166 | },
|
167 |
|
168 | getEndToEndInboundGroupSession: function(senderKey, sessionId) {
|
169 | const key = keyEndToEndInboundGroupSession(senderKey, sessionId);
|
170 | return this.store.getItem(key);
|
171 | },
|
172 |
|
173 | removeAllEndToEndInboundGroupSessions: function() {
|
174 | removeByPrefix(this.store, E2E_PREFIX + 'inboundgroupsessions/');
|
175 | },
|
176 |
|
177 | |
178 |
|
179 |
|
180 |
|
181 | getAllEndToEndRooms: function() {
|
182 | const roomKeys = getKeysWithPrefix(this.store, keyEndToEndRoom(''));
|
183 | const results = {};
|
184 | for (const k of roomKeys) {
|
185 | const unprefixedKey = k.substr(keyEndToEndRoom('').length);
|
186 | results[unprefixedKey] = getJsonItem(this.store, k);
|
187 | }
|
188 | return results;
|
189 | },
|
190 |
|
191 | removeAllEndToEndRooms: function() {
|
192 | removeByPrefix(this.store, keyEndToEndRoom(''));
|
193 | },
|
194 |
|
195 | setLocalTrustedBackupPubKey: function(pubkey) {
|
196 | this.store.setItem(KEY_END_TO_END_TRUSTED_BACKUP_PUBKEY, pubkey);
|
197 | },
|
198 |
|
199 |
|
200 |
|
201 | getLocalTrustedBackupPubKey: function() {
|
202 | return this.store.getItem(KEY_END_TO_END_TRUSTED_BACKUP_PUBKEY);
|
203 | },
|
204 | };
|
205 |
|
206 | const KEY_END_TO_END_ACCOUNT = E2E_PREFIX + "account";
|
207 | const KEY_END_TO_END_DEVICE_SYNC_TOKEN = E2E_PREFIX + "device_sync_token";
|
208 | const KEY_END_TO_END_DEVICE_LIST_TRACKING_STATUS = E2E_PREFIX + "device_tracking";
|
209 | const KEY_END_TO_END_TRUSTED_BACKUP_PUBKEY = E2E_PREFIX + "trusted_backup_pubkey";
|
210 |
|
211 | function keyEndToEndDevicesForUser(userId) {
|
212 | return E2E_PREFIX + "devices/" + userId;
|
213 | }
|
214 |
|
215 | function keyEndToEndSessions(deviceKey) {
|
216 | return E2E_PREFIX + "sessions/" + deviceKey;
|
217 | }
|
218 |
|
219 | function keyEndToEndInboundGroupSession(senderKey, sessionId) {
|
220 | return E2E_PREFIX + "inboundgroupsessions/" + senderKey + "/" + sessionId;
|
221 | }
|
222 |
|
223 | function keyEndToEndRoom(roomId) {
|
224 | return E2E_PREFIX + "rooms/" + roomId;
|
225 | }
|
226 |
|
227 | function getJsonItem(store, key) {
|
228 | try {
|
229 |
|
230 |
|
231 | return JSON.parse(store.getItem(key));
|
232 | } catch (e) {
|
233 | debuglog("Failed to get key %s: %s", key, e);
|
234 | debuglog(e.stack);
|
235 | }
|
236 | return null;
|
237 | }
|
238 |
|
239 | function getKeysWithPrefix(store, prefix) {
|
240 | const results = [];
|
241 | for (let i = 0; i < store.length; ++i) {
|
242 | const key = store.key(i);
|
243 | if (key.startsWith(prefix)) results.push(key);
|
244 | }
|
245 | return results;
|
246 | }
|
247 |
|
248 | function removeByPrefix(store, prefix) {
|
249 | const toRemove = [];
|
250 | for (let i = 0; i < store.length; ++i) {
|
251 | const key = store.key(i);
|
252 | if (key.startsWith(prefix)) toRemove.push(key);
|
253 | }
|
254 | for (const key of toRemove) {
|
255 | store.removeItem(key);
|
256 | }
|
257 | }
|
258 |
|
259 | function debuglog() {
|
260 | if (DEBUG) {
|
261 | logger.log(...arguments);
|
262 | }
|
263 | }
|