jazz-tools 0.10.4 → 0.10.6
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.
- package/.turbo/turbo-build.log +4 -4
- package/CHANGELOG.md +14 -0
- package/dist/{chunk-XRX3CCYY.js → chunk-DPW23T6J.js} +42 -5
- package/dist/chunk-DPW23T6J.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/testing.js +1 -1
- package/package.json +2 -2
- package/src/auth/AuthSecretStorage.ts +36 -2
- package/src/implementation/createContext.ts +8 -3
- package/src/tests/AuthSecretStorage.test.ts +35 -3
- package/src/tests/ContextManager.test.ts +81 -0
- package/dist/chunk-XRX3CCYY.js.map +0 -1
package/dist/index.js
CHANGED
package/dist/testing.js
CHANGED
package/package.json
CHANGED
@@ -28,16 +28,50 @@ export class AuthSecretStorage {
|
|
28
28
|
if (!(await kvStore.get(STORAGE_KEY))) {
|
29
29
|
const demoAuthSecret = await kvStore.get("demo-auth-logged-in-secret");
|
30
30
|
if (demoAuthSecret) {
|
31
|
-
|
31
|
+
const parsed = JSON.parse(demoAuthSecret);
|
32
|
+
await kvStore.set(
|
33
|
+
STORAGE_KEY,
|
34
|
+
JSON.stringify({
|
35
|
+
accountID: parsed.accountID,
|
36
|
+
accountSecret: parsed.accountSecret,
|
37
|
+
provider: "demo",
|
38
|
+
}),
|
39
|
+
);
|
32
40
|
await kvStore.delete("demo-auth-logged-in-secret");
|
33
41
|
}
|
34
42
|
|
35
43
|
const clerkAuthSecret = await kvStore.get("jazz-clerk-auth");
|
36
44
|
if (clerkAuthSecret) {
|
37
|
-
|
45
|
+
const parsed = JSON.parse(clerkAuthSecret);
|
46
|
+
await kvStore.set(
|
47
|
+
STORAGE_KEY,
|
48
|
+
JSON.stringify({
|
49
|
+
accountID: parsed.accountID,
|
50
|
+
accountSecret: parsed.secret,
|
51
|
+
provider: "clerk",
|
52
|
+
}),
|
53
|
+
);
|
38
54
|
await kvStore.delete("jazz-clerk-auth");
|
39
55
|
}
|
40
56
|
}
|
57
|
+
|
58
|
+
const value = await kvStore.get(STORAGE_KEY);
|
59
|
+
|
60
|
+
if (value) {
|
61
|
+
const parsed = JSON.parse(value);
|
62
|
+
|
63
|
+
if ("secret" in parsed) {
|
64
|
+
await kvStore.set(
|
65
|
+
STORAGE_KEY,
|
66
|
+
JSON.stringify({
|
67
|
+
accountID: parsed.accountID,
|
68
|
+
secretSeed: parsed.secretSeed,
|
69
|
+
accountSecret: parsed.secret,
|
70
|
+
provider: parsed.provider,
|
71
|
+
}),
|
72
|
+
);
|
73
|
+
}
|
74
|
+
}
|
41
75
|
}
|
42
76
|
|
43
77
|
async get(): Promise<AuthCredentials | null> {
|
@@ -110,14 +110,19 @@ export async function createJazzContextFromExistingCredentials<
|
|
110
110
|
sessionID: sessionID,
|
111
111
|
peersToLoadFrom: peersToLoadFrom,
|
112
112
|
crypto: crypto,
|
113
|
+
migration: async (rawAccount, _node, creationProps) => {
|
114
|
+
const account = new CurrentAccountSchema({
|
115
|
+
fromRaw: rawAccount,
|
116
|
+
}) as Acc;
|
117
|
+
activeAccountContext.set(account);
|
118
|
+
|
119
|
+
await account.applyMigration(creationProps);
|
120
|
+
},
|
113
121
|
});
|
114
122
|
|
115
123
|
const account = CurrentAccountSchema.fromNode(node);
|
116
124
|
activeAccountContext.set(account);
|
117
125
|
|
118
|
-
// Running the migration outside of withLoadedAccount for better error management
|
119
|
-
await account.applyMigration();
|
120
|
-
|
121
126
|
return {
|
122
127
|
node,
|
123
128
|
account,
|
@@ -28,20 +28,52 @@ describe("AuthSecretStorage", () => {
|
|
28
28
|
|
29
29
|
await authSecretStorage.migrate();
|
30
30
|
|
31
|
-
expect(await kvStore.get("jazz-logged-in-secret")).toBe(
|
31
|
+
expect(await kvStore.get("jazz-logged-in-secret")).toBe(
|
32
|
+
JSON.stringify({
|
33
|
+
accountID: "demo123",
|
34
|
+
accountSecret: "secret123",
|
35
|
+
provider: "demo",
|
36
|
+
}),
|
37
|
+
);
|
32
38
|
expect(await kvStore.get("demo-auth-logged-in-secret")).toBeNull();
|
33
39
|
});
|
34
40
|
|
35
41
|
it("should migrate clerk auth secret", async () => {
|
36
42
|
const clerkSecret = JSON.stringify({
|
37
43
|
accountID: "clerk123",
|
38
|
-
|
44
|
+
secret: "secret123",
|
39
45
|
});
|
40
46
|
await kvStore.set("jazz-clerk-auth", clerkSecret);
|
41
47
|
|
42
48
|
await authSecretStorage.migrate();
|
43
49
|
|
44
|
-
expect(await kvStore.get("jazz-logged-in-secret")).toBe(
|
50
|
+
expect(await kvStore.get("jazz-logged-in-secret")).toBe(
|
51
|
+
JSON.stringify({
|
52
|
+
accountID: "clerk123",
|
53
|
+
accountSecret: "secret123",
|
54
|
+
provider: "clerk",
|
55
|
+
}),
|
56
|
+
);
|
57
|
+
expect(await kvStore.get("jazz-clerk-auth")).toBeNull();
|
58
|
+
});
|
59
|
+
|
60
|
+
it("should migrate auth wrong secret key to accountSecret", async () => {
|
61
|
+
const clerkSecret = JSON.stringify({
|
62
|
+
accountID: "clerk123",
|
63
|
+
secret: "secret123",
|
64
|
+
provider: "clerk",
|
65
|
+
});
|
66
|
+
await kvStore.set("jazz-logged-in-secret", clerkSecret);
|
67
|
+
|
68
|
+
await authSecretStorage.migrate();
|
69
|
+
|
70
|
+
expect(await kvStore.get("jazz-logged-in-secret")).toBe(
|
71
|
+
JSON.stringify({
|
72
|
+
accountID: "clerk123",
|
73
|
+
accountSecret: "secret123",
|
74
|
+
provider: "clerk",
|
75
|
+
}),
|
76
|
+
);
|
45
77
|
expect(await kvStore.get("jazz-clerk-auth")).toBeNull();
|
46
78
|
});
|
47
79
|
});
|
@@ -196,6 +196,87 @@ describe("ContextManager", () => {
|
|
196
196
|
expect(onAnonymousAccountDiscarded).not.toHaveBeenCalled();
|
197
197
|
});
|
198
198
|
|
199
|
+
test("the migration should be applied correctly on existing accounts ", async () => {
|
200
|
+
class AccountRoot extends CoMap {
|
201
|
+
value = co.string;
|
202
|
+
}
|
203
|
+
|
204
|
+
let lastRootId: string | undefined;
|
205
|
+
|
206
|
+
class CustomAccount extends Account {
|
207
|
+
root = co.ref(AccountRoot);
|
208
|
+
|
209
|
+
migrate() {
|
210
|
+
this.root = AccountRoot.create({
|
211
|
+
value: "Hello",
|
212
|
+
});
|
213
|
+
lastRootId = this.root.id;
|
214
|
+
}
|
215
|
+
}
|
216
|
+
const customManager = new TestJazzContextManager<CustomAccount>();
|
217
|
+
|
218
|
+
// Create initial anonymous context
|
219
|
+
await customManager.createContext({
|
220
|
+
AccountSchema: CustomAccount,
|
221
|
+
});
|
222
|
+
|
223
|
+
const account = (
|
224
|
+
customManager.getCurrentValue() as JazzAuthContext<CustomAccount>
|
225
|
+
).me;
|
226
|
+
|
227
|
+
await customManager.authenticate({
|
228
|
+
accountID: account.id,
|
229
|
+
accountSecret: account._raw.core.node.account.agentSecret,
|
230
|
+
provider: "test",
|
231
|
+
});
|
232
|
+
|
233
|
+
const me = await CustomAccount.getMe().ensureLoaded({ root: {} });
|
234
|
+
|
235
|
+
expect(me.root.id).toBe(lastRootId);
|
236
|
+
});
|
237
|
+
|
238
|
+
test("the migration should be applied correctly on existing accounts (2)", async () => {
|
239
|
+
class AccountRoot extends CoMap {
|
240
|
+
value = co.number;
|
241
|
+
}
|
242
|
+
|
243
|
+
class CustomAccount extends Account {
|
244
|
+
root = co.ref(AccountRoot);
|
245
|
+
|
246
|
+
async migrate(this: CustomAccount) {
|
247
|
+
if (this.root === undefined) {
|
248
|
+
this.root = AccountRoot.create({
|
249
|
+
value: 1,
|
250
|
+
});
|
251
|
+
} else {
|
252
|
+
const { root } = await this.ensureLoaded({ root: {} });
|
253
|
+
|
254
|
+
root.value = 2;
|
255
|
+
}
|
256
|
+
}
|
257
|
+
}
|
258
|
+
const customManager = new TestJazzContextManager<CustomAccount>();
|
259
|
+
|
260
|
+
// Create initial anonymous context
|
261
|
+
await customManager.createContext({
|
262
|
+
AccountSchema: CustomAccount,
|
263
|
+
});
|
264
|
+
|
265
|
+
const account = (
|
266
|
+
customManager.getCurrentValue() as JazzAuthContext<CustomAccount>
|
267
|
+
).me;
|
268
|
+
|
269
|
+
await customManager.authenticate({
|
270
|
+
accountID: account.id,
|
271
|
+
accountSecret: account._raw.core.node.account.agentSecret,
|
272
|
+
provider: "test",
|
273
|
+
});
|
274
|
+
|
275
|
+
const me = await CustomAccount.getMe().ensureLoaded({ root: {} });
|
276
|
+
|
277
|
+
expect(me.root.value).toBe(2);
|
278
|
+
});
|
279
|
+
|
199
280
|
test("onAnonymousAccountDiscarded should work on transfering data between accounts", async () => {
|
200
281
|
class AccountRoot extends CoMap {
|
201
282
|
value = co.string;
|