Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Reuse the opened session for collection keys

Manav Rathie011e56a32d8
diff --git a/web/apps/photos/src/services/authenticated-session.ts b/web/apps/photos/src/services/authenticated-session.ts
index 05df86d44d..cdd590440f 100644
--- a/web/apps/photos/src/services/authenticated-session.ts
+++ b/web/apps/photos/src/services/authenticated-session.ts
@@ -4,20 +4,21 @@ import {
bindCollectionKeyOpener,
unbindCollectionKeyOpener,
} from "ente-new/photos/services/collection";
-import { openCollectionKey, openSession, type Session } from "ente-photos-wasm";
+import { openCollectionKey, openSession } from "ente-photos-wasm";
-const sessions = createAuthenticatedSessionCache((input): Promise<Session> => {
- const opening = openSession(input);
- bindCollectionKeyOpener(async (input) =>
- openCollectionKey(
- await sessions.ensure(),
- input.ownerID,
- input.encryptedKey,
- input.keyDecryptionNonce,
+const sessions = createAuthenticatedSessionCache(
+ openSession,
+ masterKeyFromSession,
+ (session) =>
+ bindCollectionKeyOpener((input) =>
+ openCollectionKey(
+ session,
+ input.ownerID,
+ input.encryptedKey,
+ input.keyDecryptionNonce,
+ ),
),
- );
- return opening;
-}, masterKeyFromSession);
+);
export const openAuthenticatedSession = sessions.open;
export const ensureAuthenticatedSession = sessions.ensure;
diff --git a/web/apps/photos/tests/authenticated-session.test.ts b/web/apps/photos/tests/authenticated-session.test.ts
index 42fe5a1594..311806d753 100644
--- a/web/apps/photos/tests/authenticated-session.test.ts
+++ b/web/apps/photos/tests/authenticated-session.test.ts
@@ -114,15 +114,42 @@ test("concurrent opens share a session and retain the latest token", async () =>
expect(openSession).toHaveBeenCalledTimes(2);
});
-test("the bound collection opener retries a failed initialization", async () => {
+test("collection keys reuse the opened session without reading browser storage", async () => {
+ const session = mockSession();
+ openSession.mockResolvedValue(session);
+ await sessions.ensureAuthenticatedSession();
+ const opener = bindCollectionKeyOpener.mock.calls[0]![0];
+ vi.clearAllMocks();
+
+ openCollectionKey.mockResolvedValue("collection-key");
+ for (const ownerID of [1, 2]) {
+ await expect(
+ opener({ ownerID, encryptedKey: "encrypted-key" }),
+ ).resolves.toBe("collection-key");
+ expect(openCollectionKey).toHaveBeenLastCalledWith(
+ session,
+ ownerID,
+ "encrypted-key",
+ undefined,
+ );
+ }
+ expect(savedAuthToken).not.toHaveBeenCalled();
+ expect(apiOrigin).not.toHaveBeenCalled();
+ expect(masterKeyFromSession).not.toHaveBeenCalled();
+ expect(openSession).not.toHaveBeenCalled();
+});
+
+test("failed initialization leaves the collection opener unbound and can be retried", async () => {
const error = new Error("WASM download failed");
const session = mockSession();
openSession.mockRejectedValueOnce(error).mockResolvedValueOnce(session);
const opening = sessions.openAuthenticatedSession(1, "token", "key");
- await Promise.resolve();
- const opener = bindCollectionKeyOpener.mock.calls[0]![0];
await expect(opening).rejects.toBe(error);
+ expect(bindCollectionKeyOpener).not.toHaveBeenCalled();
+
+ await sessions.ensureAuthenticatedSession();
+ const opener = bindCollectionKeyOpener.mock.calls[0]![0];
openCollectionKey.mockResolvedValue("collection-key");
await expect(
@@ -184,6 +211,7 @@ test("clearing during initialization disposes its eventual handle", async () =>
ready.resolve(session);
await expect(opening).rejects.toThrow("Authenticated session was cleared");
expect(session.free).toHaveBeenCalledTimes(1);
+ expect(bindCollectionKeyOpener).not.toHaveBeenCalled();
});
test.each(["resolve", "reject"])(
@@ -213,6 +241,7 @@ test.each(["resolve", "reject"])(
outcome === "resolve" ? 1 : 0,
);
expect(next.free).not.toHaveBeenCalled();
+ expect(bindCollectionKeyOpener).toHaveBeenCalledOnce();
expect(
await sessions.openAuthenticatedSession(2, "new-token", "key"),
).toBe(next);
diff --git a/web/packages/accounts/services/authenticated-session.ts b/web/packages/accounts/services/authenticated-session.ts
index 60493c5a12..913bb746b2 100644
--- a/web/packages/accounts/services/authenticated-session.ts
+++ b/web/packages/accounts/services/authenticated-session.ts
@@ -21,6 +21,7 @@ interface SessionInput {
export const createAuthenticatedSessionCache = <T extends Session>(
openSession: (input: SessionInput) => Promise<T>,
masterKeyFromSession: () => Promise<string | undefined>,
+ onOpen?: (session: T) => void,
) => {
let current: { key: string; opening: Promise<T> } | undefined;
let generation = 0;
@@ -60,6 +61,7 @@ export const createAuthenticatedSessionCache = <T extends Session>(
session.free();
throw new Error("Authenticated session was cleared");
}
+ onOpen?.(session);
return session;
})
.catch((error: unknown) => {