@metamask/profile-sync-controller 9.0.0 → 10.1.0

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.
Files changed (154) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/dist/controllers/authentication/AuthenticationController.cjs +53 -90
  3. package/dist/controllers/authentication/AuthenticationController.cjs.map +1 -1
  4. package/dist/controllers/authentication/AuthenticationController.d.cts +11 -23
  5. package/dist/controllers/authentication/AuthenticationController.d.cts.map +1 -1
  6. package/dist/controllers/authentication/AuthenticationController.d.mts +11 -23
  7. package/dist/controllers/authentication/AuthenticationController.d.mts.map +1 -1
  8. package/dist/controllers/authentication/AuthenticationController.mjs +53 -90
  9. package/dist/controllers/authentication/AuthenticationController.mjs.map +1 -1
  10. package/dist/controllers/authentication/mocks/mockResponses.cjs +10 -23
  11. package/dist/controllers/authentication/mocks/mockResponses.cjs.map +1 -1
  12. package/dist/controllers/authentication/mocks/mockResponses.d.cts +42 -10
  13. package/dist/controllers/authentication/mocks/mockResponses.d.cts.map +1 -1
  14. package/dist/controllers/authentication/mocks/mockResponses.d.mts +42 -10
  15. package/dist/controllers/authentication/mocks/mockResponses.d.mts.map +1 -1
  16. package/dist/controllers/authentication/mocks/mockResponses.mjs +9 -22
  17. package/dist/controllers/authentication/mocks/mockResponses.mjs.map +1 -1
  18. package/dist/controllers/user-storage/UserStorageController.cjs +76 -127
  19. package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
  20. package/dist/controllers/user-storage/UserStorageController.d.cts +10 -4
  21. package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
  22. package/dist/controllers/user-storage/UserStorageController.d.mts +10 -4
  23. package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
  24. package/dist/controllers/user-storage/UserStorageController.mjs +76 -127
  25. package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
  26. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs +1 -1
  27. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs.map +1 -1
  28. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs +1 -1
  29. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs.map +1 -1
  30. package/dist/controllers/user-storage/account-syncing/sync-utils.cjs +1 -0
  31. package/dist/controllers/user-storage/account-syncing/sync-utils.cjs.map +1 -1
  32. package/dist/controllers/user-storage/account-syncing/sync-utils.d.cts.map +1 -1
  33. package/dist/controllers/user-storage/account-syncing/sync-utils.d.mts.map +1 -1
  34. package/dist/controllers/user-storage/account-syncing/sync-utils.mjs +1 -0
  35. package/dist/controllers/user-storage/account-syncing/sync-utils.mjs.map +1 -1
  36. package/dist/controllers/user-storage/mocks/mockResponses.cjs +3 -3
  37. package/dist/controllers/user-storage/mocks/mockResponses.cjs.map +1 -1
  38. package/dist/controllers/user-storage/mocks/mockResponses.d.cts +1 -1
  39. package/dist/controllers/user-storage/mocks/mockResponses.d.cts.map +1 -1
  40. package/dist/controllers/user-storage/mocks/mockResponses.d.mts +1 -1
  41. package/dist/controllers/user-storage/mocks/mockResponses.d.mts.map +1 -1
  42. package/dist/controllers/user-storage/mocks/mockResponses.mjs +3 -3
  43. package/dist/controllers/user-storage/mocks/mockResponses.mjs.map +1 -1
  44. package/dist/controllers/user-storage/network-syncing/controller-integration.cjs +14 -14
  45. package/dist/controllers/user-storage/network-syncing/controller-integration.cjs.map +1 -1
  46. package/dist/controllers/user-storage/network-syncing/controller-integration.d.cts +3 -3
  47. package/dist/controllers/user-storage/network-syncing/controller-integration.d.cts.map +1 -1
  48. package/dist/controllers/user-storage/network-syncing/controller-integration.d.mts +3 -3
  49. package/dist/controllers/user-storage/network-syncing/controller-integration.d.mts.map +1 -1
  50. package/dist/controllers/user-storage/network-syncing/controller-integration.mjs +14 -14
  51. package/dist/controllers/user-storage/network-syncing/controller-integration.mjs.map +1 -1
  52. package/dist/controllers/user-storage/network-syncing/services.cjs +27 -23
  53. package/dist/controllers/user-storage/network-syncing/services.cjs.map +1 -1
  54. package/dist/controllers/user-storage/network-syncing/services.d.cts +16 -7
  55. package/dist/controllers/user-storage/network-syncing/services.d.cts.map +1 -1
  56. package/dist/controllers/user-storage/network-syncing/services.d.mts +16 -7
  57. package/dist/controllers/user-storage/network-syncing/services.d.mts.map +1 -1
  58. package/dist/controllers/user-storage/network-syncing/services.mjs +27 -23
  59. package/dist/controllers/user-storage/network-syncing/services.mjs.map +1 -1
  60. package/dist/controllers/user-storage/network-syncing/sync-mutations.cjs.map +1 -1
  61. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.cts +13 -5
  62. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.cts.map +1 -1
  63. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.mts +13 -5
  64. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.mts.map +1 -1
  65. package/dist/controllers/user-storage/network-syncing/sync-mutations.mjs.map +1 -1
  66. package/dist/controllers/user-storage/types.cjs +3 -0
  67. package/dist/controllers/user-storage/types.cjs.map +1 -0
  68. package/dist/controllers/user-storage/types.d.cts +23 -0
  69. package/dist/controllers/user-storage/types.d.cts.map +1 -0
  70. package/dist/controllers/user-storage/types.d.mts +23 -0
  71. package/dist/controllers/user-storage/types.d.mts.map +1 -0
  72. package/dist/controllers/user-storage/types.mjs +2 -0
  73. package/dist/controllers/user-storage/types.mjs.map +1 -0
  74. package/dist/sdk/authentication-jwt-bearer/flow-srp.cjs +7 -8
  75. package/dist/sdk/authentication-jwt-bearer/flow-srp.cjs.map +1 -1
  76. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.cts +2 -0
  77. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.cts.map +1 -1
  78. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.mts +2 -0
  79. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.mts.map +1 -1
  80. package/dist/sdk/authentication-jwt-bearer/flow-srp.mjs +8 -9
  81. package/dist/sdk/authentication-jwt-bearer/flow-srp.mjs.map +1 -1
  82. package/dist/sdk/authentication-jwt-bearer/services.cjs +10 -2
  83. package/dist/sdk/authentication-jwt-bearer/services.cjs.map +1 -1
  84. package/dist/sdk/authentication-jwt-bearer/services.d.cts +3 -1
  85. package/dist/sdk/authentication-jwt-bearer/services.d.cts.map +1 -1
  86. package/dist/sdk/authentication-jwt-bearer/services.d.mts +3 -1
  87. package/dist/sdk/authentication-jwt-bearer/services.d.mts.map +1 -1
  88. package/dist/sdk/authentication-jwt-bearer/services.mjs +10 -2
  89. package/dist/sdk/authentication-jwt-bearer/services.mjs.map +1 -1
  90. package/dist/sdk/authentication-jwt-bearer/types.cjs.map +1 -1
  91. package/dist/sdk/authentication-jwt-bearer/types.mjs.map +1 -1
  92. package/dist/sdk/authentication.cjs.map +1 -1
  93. package/dist/sdk/authentication.d.cts +1 -1
  94. package/dist/sdk/authentication.d.cts.map +1 -1
  95. package/dist/sdk/authentication.d.mts +1 -1
  96. package/dist/sdk/authentication.d.mts.map +1 -1
  97. package/dist/sdk/authentication.mjs.map +1 -1
  98. package/dist/sdk/mocks/auth.cjs +44 -0
  99. package/dist/sdk/mocks/auth.cjs.map +1 -0
  100. package/dist/sdk/mocks/auth.d.cts +39 -0
  101. package/dist/sdk/mocks/auth.d.cts.map +1 -0
  102. package/dist/sdk/mocks/auth.d.mts +39 -0
  103. package/dist/sdk/mocks/auth.d.mts.map +1 -0
  104. package/dist/sdk/mocks/auth.mjs +41 -0
  105. package/dist/sdk/mocks/auth.mjs.map +1 -0
  106. package/dist/sdk/mocks/userstorage.cjs +43 -0
  107. package/dist/sdk/mocks/userstorage.cjs.map +1 -0
  108. package/dist/sdk/mocks/userstorage.d.cts +10 -0
  109. package/dist/sdk/mocks/userstorage.d.cts.map +1 -0
  110. package/dist/sdk/mocks/userstorage.d.mts +10 -0
  111. package/dist/sdk/mocks/userstorage.d.mts.map +1 -0
  112. package/dist/sdk/mocks/userstorage.mjs +15 -0
  113. package/dist/sdk/mocks/userstorage.mjs.map +1 -0
  114. package/dist/sdk/user-storage.cjs +39 -43
  115. package/dist/sdk/user-storage.cjs.map +1 -1
  116. package/dist/sdk/user-storage.d.cts +13 -8
  117. package/dist/sdk/user-storage.d.cts.map +1 -1
  118. package/dist/sdk/user-storage.d.mts +13 -8
  119. package/dist/sdk/user-storage.d.mts.map +1 -1
  120. package/dist/sdk/user-storage.mjs +39 -43
  121. package/dist/sdk/user-storage.mjs.map +1 -1
  122. package/dist/sdk/utils/messaging-signing-snap-requests.cjs +12 -1
  123. package/dist/sdk/utils/messaging-signing-snap-requests.cjs.map +1 -1
  124. package/dist/sdk/utils/messaging-signing-snap-requests.d.cts +6 -0
  125. package/dist/sdk/utils/messaging-signing-snap-requests.d.cts.map +1 -1
  126. package/dist/sdk/utils/messaging-signing-snap-requests.d.mts +6 -0
  127. package/dist/sdk/utils/messaging-signing-snap-requests.d.mts.map +1 -1
  128. package/dist/sdk/utils/messaging-signing-snap-requests.mjs +10 -0
  129. package/dist/sdk/utils/messaging-signing-snap-requests.mjs.map +1 -1
  130. package/dist/shared/types/services.cjs +3 -0
  131. package/dist/shared/types/services.cjs.map +1 -0
  132. package/dist/shared/types/services.d.cts +10 -0
  133. package/dist/shared/types/services.d.cts.map +1 -0
  134. package/dist/shared/types/services.d.mts +10 -0
  135. package/dist/shared/types/services.d.mts.map +1 -0
  136. package/dist/shared/types/services.mjs +2 -0
  137. package/dist/shared/types/services.mjs.map +1 -0
  138. package/package.json +6 -6
  139. package/dist/controllers/authentication/services.cjs +0 -132
  140. package/dist/controllers/authentication/services.cjs.map +0 -1
  141. package/dist/controllers/authentication/services.d.cts +0 -72
  142. package/dist/controllers/authentication/services.d.cts.map +0 -1
  143. package/dist/controllers/authentication/services.d.mts +0 -72
  144. package/dist/controllers/authentication/services.d.mts.map +0 -1
  145. package/dist/controllers/authentication/services.mjs +0 -129
  146. package/dist/controllers/authentication/services.mjs.map +0 -1
  147. package/dist/controllers/user-storage/services.cjs +0 -306
  148. package/dist/controllers/user-storage/services.cjs.map +0 -1
  149. package/dist/controllers/user-storage/services.d.cts +0 -85
  150. package/dist/controllers/user-storage/services.d.cts.map +0 -1
  151. package/dist/controllers/user-storage/services.d.mts +0 -85
  152. package/dist/controllers/user-storage/services.d.mts.map +0 -1
  153. package/dist/controllers/user-storage/services.mjs +0 -276
  154. package/dist/controllers/user-storage/services.mjs.map +0 -1
@@ -1,276 +0,0 @@
1
- function $importDefault(module) {
2
- if (module?.__esModule) {
3
- return module.default;
4
- }
5
- return module;
6
- }
7
- import $log from "loglevel";
8
- const log = $importDefault($log);
9
- import encryption, { createSHA256Hash } from "../../shared/encryption/index.mjs";
10
- import { SHARED_SALT } from "../../shared/encryption/constants.mjs";
11
- import { Env, getEnvUrls } from "../../shared/env.mjs";
12
- import { createEntryPath } from "../../shared/storage-schema.mjs";
13
- const ENV_URLS = getEnvUrls(Env.PRD);
14
- export const USER_STORAGE_API = ENV_URLS.userStorageApiUrl;
15
- export const USER_STORAGE_ENDPOINT = `${USER_STORAGE_API}/api/v1/userstorage`;
16
- /**
17
- * User Storage Service - Get Storage Entry.
18
- *
19
- * @param opts - User Storage Options
20
- * @returns The storage entry, or null if fails to find entry
21
- */
22
- export async function getUserStorage(opts) {
23
- try {
24
- const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;
25
- const encryptedPath = createEntryPath(path, storageKey);
26
- const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);
27
- const userStorageResponse = await fetch(url.toString(), {
28
- headers: {
29
- 'Content-Type': 'application/json',
30
- Authorization: `Bearer ${bearerToken}`,
31
- },
32
- });
33
- // Acceptable error - since indicates entry does not exist.
34
- if (userStorageResponse.status === 404) {
35
- return null;
36
- }
37
- if (userStorageResponse.status !== 200) {
38
- throw new Error(`Unable to get User Storage - HTTP ${userStorageResponse.status}`);
39
- }
40
- const userStorage = await userStorageResponse.json();
41
- const encryptedData = userStorage?.Data ?? null;
42
- /* istanbul ignore if - this is an edge case where our endpoint returns invalid JSON payload */
43
- if (!encryptedData) {
44
- return null;
45
- }
46
- const decryptedData = await encryption.decryptString(encryptedData, opts.storageKey, nativeScryptCrypto);
47
- // Re-encrypt and re-upload the entry if the salt is random
48
- const salt = encryption.getSalt(encryptedData);
49
- if (salt.toString() !== SHARED_SALT.toString()) {
50
- await upsertUserStorage(decryptedData, opts);
51
- }
52
- return decryptedData;
53
- }
54
- catch (e) {
55
- log.error('Failed to get user storage', e);
56
- return null;
57
- }
58
- }
59
- /**
60
- * User Storage Service - Get all storage entries for a specific feature.
61
- *
62
- * @param opts - User Storage Options
63
- * @returns The storage entry, or null if fails to find entry
64
- */
65
- export async function getUserStorageAllFeatureEntries(opts) {
66
- try {
67
- const { bearerToken, path, nativeScryptCrypto } = opts;
68
- const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);
69
- const userStorageResponse = await fetch(url.toString(), {
70
- headers: {
71
- 'Content-Type': 'application/json',
72
- Authorization: `Bearer ${bearerToken}`,
73
- },
74
- });
75
- // Acceptable error - since indicates feature does not exist.
76
- if (userStorageResponse.status === 404) {
77
- return null;
78
- }
79
- if (userStorageResponse.status !== 200) {
80
- throw new Error(`Unable to get User Storage - HTTP ${userStorageResponse.status}`);
81
- }
82
- const userStorage = await userStorageResponse.json();
83
- if (!Array.isArray(userStorage)) {
84
- return null;
85
- }
86
- const decryptedData = [];
87
- const reEncryptedEntries = [];
88
- for (const entry of userStorage) {
89
- /* istanbul ignore if - unreachable if statement, but kept as edge case */
90
- if (!entry.Data) {
91
- continue;
92
- }
93
- try {
94
- const data = await encryption.decryptString(entry.Data, opts.storageKey, nativeScryptCrypto);
95
- decryptedData.push(data);
96
- // Re-encrypt the entry if the salt is different from the shared one
97
- const salt = encryption.getSalt(entry.Data);
98
- if (salt.toString() !== SHARED_SALT.toString()) {
99
- reEncryptedEntries.push([
100
- entry.HashedKey,
101
- await encryption.encryptString(data, opts.storageKey, nativeScryptCrypto),
102
- ]);
103
- }
104
- }
105
- catch {
106
- // do nothing
107
- }
108
- }
109
- // Re-upload the re-encrypted entries
110
- if (reEncryptedEntries.length) {
111
- await batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries(reEncryptedEntries, opts);
112
- }
113
- return decryptedData;
114
- }
115
- catch (e) {
116
- log.error('Failed to get user storage', e);
117
- return null;
118
- }
119
- }
120
- /**
121
- * User Storage Service - Set Storage Entry.
122
- *
123
- * @param data - data to store
124
- * @param opts - storage options
125
- */
126
- export async function upsertUserStorage(data, opts) {
127
- const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;
128
- const encryptedData = await encryption.encryptString(data, opts.storageKey, nativeScryptCrypto);
129
- const encryptedPath = createEntryPath(path, storageKey);
130
- const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);
131
- const res = await fetch(url.toString(), {
132
- method: 'PUT',
133
- headers: {
134
- 'Content-Type': 'application/json',
135
- Authorization: `Bearer ${bearerToken}`,
136
- },
137
- body: JSON.stringify({ data: encryptedData }),
138
- });
139
- if (!res.ok) {
140
- throw new Error(`user-storage - unable to upsert data - HTTP ${res.status}`);
141
- }
142
- }
143
- /**
144
- * User Storage Service - Set multiple storage entries for one specific feature.
145
- * You cannot use this method to set multiple features at once.
146
- *
147
- * @param data - data to store, in the form of an array of [entryKey, entryValue] pairs
148
- * @param opts - storage options
149
- */
150
- export async function batchUpsertUserStorage(data, opts) {
151
- if (!data.length) {
152
- return;
153
- }
154
- const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;
155
- const encryptedData = [];
156
- for (const d of data) {
157
- encryptedData.push([
158
- createSHA256Hash(d[0] + storageKey),
159
- await encryption.encryptString(d[1], opts.storageKey, nativeScryptCrypto),
160
- ]);
161
- }
162
- const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);
163
- const formattedData = Object.fromEntries(encryptedData);
164
- const res = await fetch(url.toString(), {
165
- method: 'PUT',
166
- headers: {
167
- 'Content-Type': 'application/json',
168
- Authorization: `Bearer ${bearerToken}`,
169
- },
170
- body: JSON.stringify({ data: formattedData }),
171
- });
172
- if (!res.ok) {
173
- throw new Error(`user-storage - unable to batch upsert data - HTTP ${res.status}`);
174
- }
175
- }
176
- /**
177
- * User Storage Service - Set multiple storage entries for one specific feature.
178
- * You cannot use this method to set multiple features at once.
179
- *
180
- * @param encryptedData - data to store, in the form of an array of [hashedKey, encryptedData] pairs
181
- * @param opts - storage options
182
- */
183
- export async function batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries(encryptedData, opts) {
184
- if (!encryptedData.length) {
185
- return;
186
- }
187
- const { bearerToken, path } = opts;
188
- const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);
189
- const formattedData = Object.fromEntries(encryptedData);
190
- const res = await fetch(url.toString(), {
191
- method: 'PUT',
192
- headers: {
193
- 'Content-Type': 'application/json',
194
- Authorization: `Bearer ${bearerToken}`,
195
- },
196
- body: JSON.stringify({ data: formattedData }),
197
- });
198
- if (!res.ok) {
199
- throw new Error(`user-storage - unable to batch upsert data - HTTP ${res.status}`);
200
- }
201
- }
202
- /**
203
- * User Storage Service - Delete Storage Entry.
204
- *
205
- * @param opts - User Storage Options
206
- */
207
- export async function deleteUserStorage(opts) {
208
- const { bearerToken, path, storageKey } = opts;
209
- const encryptedPath = createEntryPath(path, storageKey);
210
- const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);
211
- const userStorageResponse = await fetch(url.toString(), {
212
- method: 'DELETE',
213
- headers: {
214
- 'Content-Type': 'application/json',
215
- Authorization: `Bearer ${bearerToken}`,
216
- },
217
- });
218
- if (userStorageResponse.status === 404) {
219
- throw new Error(`user-storage - feature/entry not found - HTTP ${userStorageResponse.status}`);
220
- }
221
- if (!userStorageResponse.ok) {
222
- throw new Error(`user-storage - unable to delete data - HTTP ${userStorageResponse.status}`);
223
- }
224
- }
225
- /**
226
- * User Storage Service - Delete multiple storage entries for one specific feature.
227
- * You cannot use this method to delete multiple features at once.
228
- *
229
- * @param data - data to delete, in the form of an array entryKey[]
230
- * @param opts - storage options
231
- */
232
- export async function batchDeleteUserStorage(data, opts) {
233
- if (!data.length) {
234
- return;
235
- }
236
- const { bearerToken, path, storageKey } = opts;
237
- const encryptedData = [];
238
- for (const d of data) {
239
- encryptedData.push(createSHA256Hash(d + storageKey));
240
- }
241
- const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);
242
- const res = await fetch(url.toString(), {
243
- method: 'PUT',
244
- headers: {
245
- 'Content-Type': 'application/json',
246
- Authorization: `Bearer ${bearerToken}`,
247
- },
248
- body: JSON.stringify({ batch_delete: encryptedData }),
249
- });
250
- if (!res.ok) {
251
- throw new Error(`user-storage - unable to batch delete data - HTTP ${res.status}`);
252
- }
253
- }
254
- /**
255
- * User Storage Service - Delete all storage entries for a specific feature.
256
- *
257
- * @param opts - User Storage Options
258
- */
259
- export async function deleteUserStorageAllFeatureEntries(opts) {
260
- const { bearerToken, path } = opts;
261
- const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);
262
- const userStorageResponse = await fetch(url.toString(), {
263
- method: 'DELETE',
264
- headers: {
265
- 'Content-Type': 'application/json',
266
- Authorization: `Bearer ${bearerToken}`,
267
- },
268
- });
269
- if (userStorageResponse.status === 404) {
270
- throw new Error(`user-storage - feature not found - HTTP ${userStorageResponse.status}`);
271
- }
272
- if (!userStorageResponse.ok) {
273
- throw new Error(`user-storage - unable to delete data - HTTP ${userStorageResponse.status}`);
274
- }
275
- }
276
- //# sourceMappingURL=services.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"services.mjs","sourceRoot":"","sources":["../../../src/controllers/user-storage/services.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,UAAU,EAAE,EAAE,gBAAgB,EAAE,0CAAgC;AACvE,OAAO,EAAE,WAAW,EAAE,8CAA0C;AAChE,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,6BAAyB;AAKnD,OAAO,EAAE,eAAe,EAAE,wCAAoC;AAG9D,MAAM,QAAQ,GAAG,UAAU,CAAC,GAAG,CAAC,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AAK;QACb,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC;QAED,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,YAAY,EAAE,aAAa,EAAE,CAAC;KACtD,CAAC,CAAC;IAEH,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE;QACX,MAAM,IAAI,KAAK,CACb,qDAAqD,GAAG,CAAC,MAAM,EAAE,CAClE,CAAC;KACH;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,kCAAkC,CACtD,IAAyC;IAEzC,MAAM,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC;IACnC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,qBAAqB,IAAI,IAAI,EAAE,CAAC,CAAC;IAExD,MAAM,mBAAmB,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE;QACtD,MAAM,EAAE,QAAQ;QAChB,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC;KACF,CAAC,CAAC;IAEH,IAAI,mBAAmB,CAAC,MAAM,KAAK,GAAG,EAAE;QACtC,MAAM,IAAI,KAAK,CACb,2CAA2C,mBAAmB,CAAC,MAAM,EAAE,CACxE,CAAC;KACH;IAED,IAAI,CAAC,mBAAmB,CAAC,EAAE,EAAE;QAC3B,MAAM,IAAI,KAAK,CACb,+CAA+C,mBAAmB,CAAC,MAAM,EAAE,CAC5E,CAAC;KACH;AACH,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport encryption, { createSHA256Hash } from '../../shared/encryption';\nimport { SHARED_SALT } from '../../shared/encryption/constants';\nimport { Env, getEnvUrls } from '../../shared/env';\nimport type {\n UserStoragePathWithFeatureAndKey,\n UserStoragePathWithFeatureOnly,\n} from '../../shared/storage-schema';\nimport { createEntryPath } from '../../shared/storage-schema';\nimport type { NativeScrypt } from '../../shared/types/encryption';\n\nconst ENV_URLS = getEnvUrls(Env.PRD);\n\nexport const USER_STORAGE_API: string = ENV_URLS.userStorageApiUrl;\nexport const USER_STORAGE_ENDPOINT = `${USER_STORAGE_API}/api/v1/userstorage`;\n\n/**\n * This is the Server Response shape for a feature entry.\n */\nexport type GetUserStorageResponse = {\n HashedKey: string;\n Data: string;\n};\n\nexport type GetUserStorageAllFeatureEntriesResponse = {\n HashedKey: string;\n Data: string;\n}[];\n\nexport type UserStorageBaseOptions = {\n bearerToken: string;\n storageKey: string;\n nativeScryptCrypto?: NativeScrypt;\n};\n\nexport type UserStorageOptions = UserStorageBaseOptions & {\n path: UserStoragePathWithFeatureAndKey;\n};\n\nexport type UserStorageAllFeatureEntriesOptions = UserStorageBaseOptions & {\n path: UserStoragePathWithFeatureOnly;\n};\n\nexport type UserStorageBatchUpsertOptions = UserStorageAllFeatureEntriesOptions;\n\n/**\n * User Storage Service - Get Storage Entry.\n *\n * @param opts - User Storage Options\n * @returns The storage entry, or null if fails to find entry\n */\nexport async function getUserStorage(\n opts: UserStorageOptions,\n): Promise<string | null> {\n try {\n const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;\n\n const encryptedPath = createEntryPath(path, storageKey);\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);\n\n const userStorageResponse = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n });\n\n // Acceptable error - since indicates entry does not exist.\n if (userStorageResponse.status === 404) {\n return null;\n }\n\n if (userStorageResponse.status !== 200) {\n throw new Error(\n `Unable to get User Storage - HTTP ${userStorageResponse.status}`,\n );\n }\n\n const userStorage: GetUserStorageResponse | null =\n await userStorageResponse.json();\n const encryptedData = userStorage?.Data ?? null;\n\n /* istanbul ignore if - this is an edge case where our endpoint returns invalid JSON payload */\n if (!encryptedData) {\n return null;\n }\n\n const decryptedData = await encryption.decryptString(\n encryptedData,\n opts.storageKey,\n nativeScryptCrypto,\n );\n\n // Re-encrypt and re-upload the entry if the salt is random\n const salt = encryption.getSalt(encryptedData);\n if (salt.toString() !== SHARED_SALT.toString()) {\n await upsertUserStorage(decryptedData, opts);\n }\n\n return decryptedData;\n } catch (e) {\n log.error('Failed to get user storage', e);\n return null;\n }\n}\n\n/**\n * User Storage Service - Get all storage entries for a specific feature.\n *\n * @param opts - User Storage Options\n * @returns The storage entry, or null if fails to find entry\n */\nexport async function getUserStorageAllFeatureEntries(\n opts: UserStorageAllFeatureEntriesOptions,\n): Promise<string[] | null> {\n try {\n const { bearerToken, path, nativeScryptCrypto } = opts;\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const userStorageResponse = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n });\n\n // Acceptable error - since indicates feature does not exist.\n if (userStorageResponse.status === 404) {\n return null;\n }\n\n if (userStorageResponse.status !== 200) {\n throw new Error(\n `Unable to get User Storage - HTTP ${userStorageResponse.status}`,\n );\n }\n\n const userStorage: GetUserStorageAllFeatureEntriesResponse | null =\n await userStorageResponse.json();\n\n if (!Array.isArray(userStorage)) {\n return null;\n }\n\n const decryptedData: string[] = [];\n const reEncryptedEntries: [string, string][] = [];\n\n for (const entry of userStorage) {\n /* istanbul ignore if - unreachable if statement, but kept as edge case */\n if (!entry.Data) {\n continue;\n }\n\n try {\n const data = await encryption.decryptString(\n entry.Data,\n opts.storageKey,\n nativeScryptCrypto,\n );\n decryptedData.push(data);\n\n // Re-encrypt the entry if the salt is different from the shared one\n const salt = encryption.getSalt(entry.Data);\n if (salt.toString() !== SHARED_SALT.toString()) {\n reEncryptedEntries.push([\n entry.HashedKey,\n await encryption.encryptString(\n data,\n opts.storageKey,\n nativeScryptCrypto,\n ),\n ]);\n }\n } catch {\n // do nothing\n }\n }\n\n // Re-upload the re-encrypted entries\n if (reEncryptedEntries.length) {\n await batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries(\n reEncryptedEntries,\n opts,\n );\n }\n\n return decryptedData;\n } catch (e) {\n log.error('Failed to get user storage', e);\n return null;\n }\n}\n\n/**\n * User Storage Service - Set Storage Entry.\n *\n * @param data - data to store\n * @param opts - storage options\n */\nexport async function upsertUserStorage(\n data: string,\n opts: UserStorageOptions,\n): Promise<void> {\n const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;\n\n const encryptedData = await encryption.encryptString(\n data,\n opts.storageKey,\n nativeScryptCrypto,\n );\n const encryptedPath = createEntryPath(path, storageKey);\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);\n\n const res = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n body: JSON.stringify({ data: encryptedData }),\n });\n\n if (!res.ok) {\n throw new Error(\n `user-storage - unable to upsert data - HTTP ${res.status}`,\n );\n }\n}\n\n/**\n * User Storage Service - Set multiple storage entries for one specific feature.\n * You cannot use this method to set multiple features at once.\n *\n * @param data - data to store, in the form of an array of [entryKey, entryValue] pairs\n * @param opts - storage options\n */\nexport async function batchUpsertUserStorage(\n data: [string, string][],\n opts: UserStorageBatchUpsertOptions,\n): Promise<void> {\n if (!data.length) {\n return;\n }\n\n const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;\n\n const encryptedData: string[][] = [];\n\n for (const d of data) {\n encryptedData.push([\n createSHA256Hash(d[0] + storageKey),\n await encryption.encryptString(d[1], opts.storageKey, nativeScryptCrypto),\n ]);\n }\n\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const formattedData = Object.fromEntries(encryptedData);\n\n const res = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n body: JSON.stringify({ data: formattedData }),\n });\n\n if (!res.ok) {\n throw new Error(\n `user-storage - unable to batch upsert data - HTTP ${res.status}`,\n );\n }\n}\n\n/**\n * User Storage Service - Set multiple storage entries for one specific feature.\n * You cannot use this method to set multiple features at once.\n *\n * @param encryptedData - data to store, in the form of an array of [hashedKey, encryptedData] pairs\n * @param opts - storage options\n */\nexport async function batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries(\n encryptedData: [string, string][],\n opts: UserStorageBatchUpsertOptions,\n): Promise<void> {\n if (!encryptedData.length) {\n return;\n }\n\n const { bearerToken, path } = opts;\n\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const formattedData = Object.fromEntries(encryptedData);\n\n const res = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n body: JSON.stringify({ data: formattedData }),\n });\n\n if (!res.ok) {\n throw new Error(\n `user-storage - unable to batch upsert data - HTTP ${res.status}`,\n );\n }\n}\n\n/**\n * User Storage Service - Delete Storage Entry.\n *\n * @param opts - User Storage Options\n */\nexport async function deleteUserStorage(\n opts: UserStorageOptions,\n): Promise<void> {\n const { bearerToken, path, storageKey } = opts;\n const encryptedPath = createEntryPath(path, storageKey);\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`);\n\n const userStorageResponse = await fetch(url.toString(), {\n method: 'DELETE',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n });\n\n if (userStorageResponse.status === 404) {\n throw new Error(\n `user-storage - feature/entry not found - HTTP ${userStorageResponse.status}`,\n );\n }\n\n if (!userStorageResponse.ok) {\n throw new Error(\n `user-storage - unable to delete data - HTTP ${userStorageResponse.status}`,\n );\n }\n}\n\n/**\n * User Storage Service - Delete multiple storage entries for one specific feature.\n * You cannot use this method to delete multiple features at once.\n *\n * @param data - data to delete, in the form of an array entryKey[]\n * @param opts - storage options\n */\nexport async function batchDeleteUserStorage(\n data: string[],\n opts: UserStorageBatchUpsertOptions,\n): Promise<void> {\n if (!data.length) {\n return;\n }\n\n const { bearerToken, path, storageKey } = opts;\n\n const encryptedData: string[] = [];\n\n for (const d of data) {\n encryptedData.push(createSHA256Hash(d + storageKey));\n }\n\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const res = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n\n body: JSON.stringify({ batch_delete: encryptedData }),\n });\n\n if (!res.ok) {\n throw new Error(\n `user-storage - unable to batch delete data - HTTP ${res.status}`,\n );\n }\n}\n\n/**\n * User Storage Service - Delete all storage entries for a specific feature.\n *\n * @param opts - User Storage Options\n */\nexport async function deleteUserStorageAllFeatureEntries(\n opts: UserStorageAllFeatureEntriesOptions,\n): Promise<void> {\n const { bearerToken, path } = opts;\n const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);\n\n const userStorageResponse = await fetch(url.toString(), {\n method: 'DELETE',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n });\n\n if (userStorageResponse.status === 404) {\n throw new Error(\n `user-storage - feature not found - HTTP ${userStorageResponse.status}`,\n );\n }\n\n if (!userStorageResponse.ok) {\n throw new Error(\n `user-storage - unable to delete data - HTTP ${userStorageResponse.status}`,\n );\n }\n}\n"]}