Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move Legacy adaptation into its WASM package

Manav Rathi0e3280a35274
diff --git a/rust/crates/contacts/Cargo.toml b/rust/crates/contacts/Cargo.toml
index e95638ed77..18839e9ac0 100644
--- a/rust/crates/contacts/Cargo.toml
+++ b/rust/crates/contacts/Cargo.toml
@@ -9,7 +9,6 @@ museum = []
ente-core.workspace = true
md-5.workspace = true
serde.workspace = true
-serde_json.workspace = true
thiserror.workspace = true
[dev-dependencies]
@@ -19,6 +18,7 @@ ente-test-support.workspace = true
hmac = "0.13"
mockito.workspace = true
reqwest = { workspace = true, features = ["json"] }
+serde_json.workspace = true
sha1 = "0.11"
tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] }
uuid = { version = "1", features = ["v4", "js"] }
diff --git a/rust/crates/contacts/src/models.rs b/rust/crates/contacts/src/models.rs
index adf45b05e1..83dc25ecd5 100644
--- a/rust/crates/contacts/src/models.rs
+++ b/rust/crates/contacts/src/models.rs
@@ -39,22 +39,3 @@ pub struct ContactRecord {
pub created_at: i64,
pub updated_at: i64,
}
-
-#[cfg(test)]
-mod tests {
- use super::ContactData;
-
- #[test]
- fn contact_data_ignores_legacy_birth_date() {
- let data: ContactData =
- serde_json::from_str(r#"{"contactUserId":42,"name":"Alex","birthDate":"2001-04-02"}"#)
- .unwrap();
-
- assert_eq!(data.contact_user_id, 42);
- assert_eq!(data.name, "Alex");
- assert_eq!(
- serde_json::to_string(&data).unwrap(),
- r#"{"contactUserId":42,"name":"Alex"}"#,
- );
- }
-}
diff --git a/rust/crates/contacts/tests/contacts_mock.rs b/rust/crates/contacts/tests/contacts_mock.rs
index 9df67642c1..84fa6e78c5 100644
--- a/rust/crates/contacts/tests/contacts_mock.rs
+++ b/rust/crates/contacts/tests/contacts_mock.rs
@@ -4,11 +4,9 @@ use ente_contacts::client::{ContactsClient, OpenContactsInput, OpenContactsResul
use ente_contacts::crypto as contacts_crypto;
use ente_contacts::models::{AttachmentType, ContactData, WrappedRootContactKey};
use ente_core::{
- b64,
crypto::{Key, SecretVec},
http::{Api, ApiConfig, Auth, Http},
};
-use md5::Digest;
use mockito::{Matcher, Server};
fn sample_contact() -> ContactData {
@@ -646,9 +644,3 @@ async fn get_diff_uses_cached_wrapped_root_contact_key_for_reads_without_fetchin
assert_eq!(diff.len(), 1);
assert_eq!(diff[0].name.as_deref(), Some(contact.name.as_str()));
}
-
-#[test]
-fn root_key_md5_helper_is_base64_digest() {
- let digest = contacts_crypto::content_md5_base64(b"hello");
- assert_eq!(digest, b64::encode(&md5::Md5::digest(b"hello")));
-}
diff --git a/web/packages/contacts/legacy/service.test.ts b/web/packages/contacts/legacy/service.test.ts
deleted file mode 100644
index d096131637..0000000000
--- a/web/packages/contacts/legacy/service.test.ts
+++ /dev/null
@@ -1,64 +0,0 @@
-import { expect, test, vi } from "vitest";
-
-const { free, getInfo } = vi.hoisted(() => ({
- free: vi.fn(),
- getInfo: vi.fn(() =>
- Promise.resolve({
- contacts: [
- {
- user: { id: 101n, email: "[email protected]" },
- emergencyContact: { id: 202n, email: "[email protected]" },
- state: "ACCEPTED",
- recoveryNoticeInDays: 14n,
- },
- ],
- recoverSessions: [
- {
- id: "session_1",
- user: { id: 101n, email: "[email protected]" },
- emergencyContact: { id: 202n, email: "[email protected]" },
- status: "WAITING",
- waitTill: 3_600_000_000n,
- createdAt: 1_700_000_000_000_000n,
- },
- ],
- othersEmergencyContact: [],
- othersRecoverySession: [],
- }),
- ),
-}));
-
-vi.mock("ente-accounts/services/accounts-db", () => ({
- savedKeyAttributes: vi.fn(),
-}));
-vi.mock("ente-accounts/services/recovery-key", () => ({
- getUserRecoveryKey: vi.fn(),
-}));
-vi.mock("ente-accounts/services/session-storage", () => ({
- masterKeyFromSession: () => "master-key",
-}));
-vi.mock("ente-base/app", () => ({
- clientPackageName: "io.ente.test",
- desktopAppVersion: undefined,
- isDesktop: false,
-}));
-vi.mock("ente-base/origins", () => ({
- apiOrigin: () => "https://api.example",
-}));
-vi.mock("ente-base/token", () => ({ savedAuthToken: () => "auth-token" }));
-vi.mock("ente-legacy-wasm/authenticated", () => ({
- openLegacy: () => Promise.resolve({ free, getInfo }),
-}));
-
-test("normalizes legacy numeric fields at the wasm boundary", async () => {
- const { legacyGetInfo } = await import("./service");
-
- const info = await legacyGetInfo();
-
- expect(info.contacts[0]?.user.id).toBe(101);
- expect(info.contacts[0]?.emergencyContact.id).toBe(202);
- expect(info.contacts[0]?.recoveryNoticeInDays).toBe(14);
- expect(info.recoverSessions[0]?.waitTill).toBe(3_600_000_000);
- expect(info.recoverSessions[0]?.createdAt).toBe(1_700_000_000_000_000);
- expect(free).toHaveBeenCalledOnce();
-});
diff --git a/web/packages/contacts/legacy/service.ts b/web/packages/contacts/legacy/service.ts
index ed9e838923..59437f1ee7 100644
--- a/web/packages/contacts/legacy/service.ts
+++ b/web/packages/contacts/legacy/service.ts
@@ -5,81 +5,9 @@ import { clientPackageName, desktopAppVersion, isDesktop } from "ente-base/app";
import { apiOrigin } from "ente-base/origins";
import { savedAuthToken } from "ente-base/token";
import { openLegacy } from "ente-legacy-wasm/authenticated";
-import type {
- LegacyContactState,
- LegacyInfo,
- LegacyRecoveryStatus,
-} from "./types";
+import type { LegacyContactState, LegacyInfo } from "./types";
-type KeyAttributes = NonNullable<ReturnType<typeof savedKeyAttributes>>;
-
-interface RemoteLegacyUser {
- id: number | bigint;
- email: string;
-}
-
-interface RemoteLegacyContactRecord {
- user: RemoteLegacyUser;
- emergencyContact: RemoteLegacyUser;
- state: LegacyContactState;
- recoveryNoticeInDays: number | bigint;
-}
-
-interface RemoteLegacyRecoverySession {
- id: string;
- user: RemoteLegacyUser;
- emergencyContact: RemoteLegacyUser;
- status: LegacyRecoveryStatus;
- waitTill: number | bigint;
- createdAt: number | bigint;
-}
-
-interface RemoteLegacyInfo {
- contacts: RemoteLegacyContactRecord[];
- recoverSessions: RemoteLegacyRecoverySession[];
- othersEmergencyContact: RemoteLegacyContactRecord[];
- othersRecoverySession: RemoteLegacyRecoverySession[];
-}
-
-interface LegacyClient {
- free: () => void;
- getInfo: () => Promise<RemoteLegacyInfo>;
- publicKey: (email: string) => Promise<string | undefined>;
- verificationID: (publicKeyB64: string) => string;
- addContact: (
- email: string,
- currentUserKeyAttributes: KeyAttributes,
- recoveryNoticeInDays?: number,
- ) => Promise<void>;
- updateContact: (
- userID: bigint,
- emergencyContactID: bigint,
- state: LegacyContactState,
- ) => Promise<void>;
- updateRecoveryNotice: (
- emergencyContactID: bigint,
- recoveryNoticeInDays: number,
- ) => Promise<void>;
- startRecovery: (
- userID: bigint,
- emergencyContactID: bigint,
- ) => Promise<void>;
- stopRecovery: (
- recoveryID: string,
- userID: bigint,
- emergencyContactID: bigint,
- ) => Promise<void>;
- rejectRecovery: (
- recoveryID: string,
- userID: bigint,
- emergencyContactID: bigint,
- ) => Promise<void>;
- changePassword: (
- recoveryID: string,
- currentUserKeyAttributes: KeyAttributes,
- newPassword: string,
- ) => Promise<void>;
-}
+type LegacyClient = Awaited<ReturnType<typeof openLegacy>>;
const withLegacyClient = async <T>(
operation: (client: LegacyClient) => T | Promise<T>,
@@ -92,7 +20,7 @@ const withLegacyClient = async <T>(
if (!authToken) {
throw new Error("Missing auth token");
}
- const client: LegacyClient = await openLegacy({
+ const client = await openLegacy({
baseUrl: await apiOrigin(),
authToken,
masterKeyB64,
@@ -102,7 +30,7 @@ const withLegacyClient = async <T>(
try {
return await operation(client);
} finally {
- client.free();
+ client.close();
}
};
@@ -114,44 +42,8 @@ const currentKeyAttributes = () => {
return keyAttributes;
};
-const normalizeLegacyUser = (user: RemoteLegacyUser) => ({
- id: Number(user.id),
- email: user.email,
-});
-
-const normalizeLegacyContactRecord = (record: RemoteLegacyContactRecord) => ({
- user: normalizeLegacyUser(record.user),
- emergencyContact: normalizeLegacyUser(record.emergencyContact),
- state: record.state,
- recoveryNoticeInDays: Number(record.recoveryNoticeInDays),
-});
-
-const normalizeLegacyRecoverySession = (
- session: RemoteLegacyRecoverySession,
-) => ({
- id: session.id,
- user: normalizeLegacyUser(session.user),
- emergencyContact: normalizeLegacyUser(session.emergencyContact),
- status: session.status,
- waitTill: Number(session.waitTill),
- createdAt: Number(session.createdAt),
-});
-
-const normalizeLegacyInfo = (info: RemoteLegacyInfo): LegacyInfo => ({
- contacts: info.contacts.map(normalizeLegacyContactRecord),
- recoverSessions: info.recoverSessions.map(normalizeLegacyRecoverySession),
- othersEmergencyContact: info.othersEmergencyContact.map(
- normalizeLegacyContactRecord,
- ),
- othersRecoverySession: info.othersRecoverySession.map(
- normalizeLegacyRecoverySession,
- ),
-});
-
-export const legacyGetInfo = () =>
- withLegacyClient(async (client) =>
- normalizeLegacyInfo(await client.getInfo()),
- );
+export const legacyGetInfo = (): Promise<LegacyInfo> =>
+ withLegacyClient((client) => client.getInfo());
export const legacyPublicKey = (email: string) =>
withLegacyClient((client) => client.publicKey(email));
@@ -178,7 +70,7 @@ export const legacyUpdateContact = (
state: LegacyContactState,
) =>
withLegacyClient((client) =>
- client.updateContact(BigInt(userID), BigInt(emergencyContactID), state),
+ client.updateContact(userID, emergencyContactID, state),
);
export const legacyUpdateRecoveryNotice = (
@@ -186,10 +78,7 @@ export const legacyUpdateRecoveryNotice = (
recoveryNoticeInDays: number,
) =>
withLegacyClient((client) =>
- client.updateRecoveryNotice(
- BigInt(emergencyContactID),
- recoveryNoticeInDays,
- ),
+ client.updateRecoveryNotice(emergencyContactID, recoveryNoticeInDays),
);
export const legacyStartRecovery = (
@@ -197,7 +86,7 @@ export const legacyStartRecovery = (
emergencyContactID: number,
) =>
withLegacyClient((client) =>
- client.startRecovery(BigInt(userID), BigInt(emergencyContactID)),
+ client.startRecovery(userID, emergencyContactID),
);
export const legacyStopRecovery = (
@@ -206,11 +95,7 @@ export const legacyStopRecovery = (
emergencyContactID: number,
) =>
withLegacyClient((client) =>
- client.stopRecovery(
- recoveryID,
- BigInt(userID),
- BigInt(emergencyContactID),
- ),
+ client.stopRecovery(recoveryID, userID, emergencyContactID),
);
export const legacyRejectRecovery = (
@@ -219,11 +104,7 @@ export const legacyRejectRecovery = (
emergencyContactID: number,
) =>
withLegacyClient((client) =>
- client.rejectRecovery(
- recoveryID,
- BigInt(userID),
- BigInt(emergencyContactID),
- ),
+ client.rejectRecovery(recoveryID, userID, emergencyContactID),
);
export const legacyChangePassword = (recoveryID: string, newPassword: string) =>
diff --git a/web/packages/wasm/legacy/authenticated.ts b/web/packages/wasm/legacy/authenticated.ts
index a0b78b054e..17e6773dee 100644
--- a/web/packages/wasm/legacy/authenticated.ts
+++ b/web/packages/wasm/legacy/authenticated.ts
@@ -6,17 +6,145 @@ export interface OpenLegacyInput {
clientVersion?: string;
}
+interface KeyAttributes {
+ kekSalt: string;
+ kekHash?: string;
+ encryptedKey: string;
+ keyDecryptionNonce: string;
+ publicKey: string;
+ encryptedSecretKey: string;
+ secretKeyDecryptionNonce: string;
+ memLimit: number;
+ opsLimit: number;
+ masterKeyEncryptedWithRecoveryKey?: string;
+ masterKeyDecryptionNonce?: string;
+ recoveryKeyEncryptedWithMasterKey?: string;
+ recoveryKeyDecryptionNonce?: string;
+}
+
+type LegacyContactState =
+ | "INVITED"
+ | "REVOKED"
+ | "ACCEPTED"
+ | "CONTACT_LEFT"
+ | "CONTACT_DENIED";
+
+type LegacyRecoveryStatus =
+ | "INITIATED"
+ | "WAITING"
+ | "REJECTED"
+ | "RECOVERED"
+ | "STOPPED"
+ | "READY";
+
+interface LegacyUser {
+ id: number;
+ email: string;
+}
+
+interface LegacyContactRecord {
+ user: LegacyUser;
+ emergencyContact: LegacyUser;
+ state: LegacyContactState;
+ recoveryNoticeInDays: number;
+}
+
+interface LegacyRecoverySession {
+ id: string;
+ user: LegacyUser;
+ emergencyContact: LegacyUser;
+ status: LegacyRecoveryStatus;
+ waitTill: number;
+ createdAt: number;
+}
+
+interface LegacyInfo {
+ contacts: LegacyContactRecord[];
+ recoverSessions: LegacyRecoverySession[];
+ othersEmergencyContact: LegacyContactRecord[];
+ othersRecoverySession: LegacyRecoverySession[];
+}
+
export const openLegacy = async ({
baseUrl,
authToken,
masterKeyB64,
clientPackage,
clientVersion,
-}: OpenLegacyInput) =>
- (await import("./pkg/ente_legacy_wasm")).openLegacy(
+}: OpenLegacyInput) => {
+ const client = (await import("./pkg/ente_legacy_wasm")).openLegacy(
baseUrl,
authToken,
masterKeyB64,
clientPackage,
clientVersion,
);
+
+ return {
+ close: () => client.free(),
+ getInfo: () => client.getInfo() as Promise<LegacyInfo>,
+ publicKey: (email: string) => client.publicKey(email),
+ verificationID: (publicKeyB64: string) =>
+ client.verificationID(publicKeyB64),
+ addContact: (
+ email: string,
+ currentUserKeyAttributes: KeyAttributes,
+ recoveryNoticeInDays?: number,
+ ) =>
+ client.addContact(
+ email,
+ currentUserKeyAttributes,
+ recoveryNoticeInDays,
+ ),
+ updateContact: (
+ userID: number,
+ emergencyContactID: number,
+ state: LegacyContactState,
+ ) =>
+ client.updateContact(
+ BigInt(userID),
+ BigInt(emergencyContactID),
+ state,
+ ),
+ updateRecoveryNotice: (
+ emergencyContactID: number,
+ recoveryNoticeInDays: number,
+ ) =>
+ client.updateRecoveryNotice(
+ BigInt(emergencyContactID),
+ recoveryNoticeInDays,
+ ),
+ startRecovery: (userID: number, emergencyContactID: number) =>
+ client.startRecovery(BigInt(userID), BigInt(emergencyContactID)),
+ stopRecovery: (
+ recoveryID: string,
+ userID: number,
+ emergencyContactID: number,
+ ) =>
+ client.stopRecovery(
+ recoveryID,
+ BigInt(userID),
+ BigInt(emergencyContactID),
+ ),
+ rejectRecovery: (
+ recoveryID: string,
+ userID: number,
+ emergencyContactID: number,
+ ) =>
+ client.rejectRecovery(
+ recoveryID,
+ BigInt(userID),
+ BigInt(emergencyContactID),
+ ),
+ changePassword: (
+ recoveryID: string,
+ currentUserKeyAttributes: KeyAttributes,
+ newPassword: string,
+ ) =>
+ client.changePassword(
+ recoveryID,
+ currentUserKeyAttributes,
+ newPassword,
+ ),
+ };
+};