Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move

Manav Rathiaf391346d1dc
diff --git a/web/packages/accounts/components/SignUpContents.tsx b/web/packages/accounts/components/SignUpContents.tsx
index c9611ee3d4..3607765f4b 100644
--- a/web/packages/accounts/components/SignUpContents.tsx
+++ b/web/packages/accounts/components/SignUpContents.tsx
@@ -15,6 +15,7 @@ import {
} from "@mui/material";
import { generateKeyAndSRPAttributes } from "ente-accounts/services/srp";
import { sendOTT } from "ente-accounts/services/user";
+import { generateAndSaveIntermediateKeyAttributes } from "ente-accounts/utils/helpers";
import { isWeakPassword } from "ente-accounts/utils/password";
import { LinkButton } from "ente-base/components/LinkButton";
import { LoadingButton } from "ente-base/components/mui/LoadingButton";
@@ -22,10 +23,7 @@ import { ShowHidePasswordInputAdornment } from "ente-base/components/mui/Passwor
import { isMuseumHTTPError } from "ente-base/http";
import log from "ente-base/log";
import { setLSUser } from "ente-shared//storage/localStorage";
-import {
- generateAndSaveIntermediateKeyAttributes,
- saveKeyInSessionStore,
-} from "ente-shared/crypto/helpers";
+import { saveKeyInSessionStore } from "ente-shared/crypto/helpers";
import { setData } from "ente-shared/storage/localStorage";
import {
setJustSignedUp,
diff --git a/web/packages/accounts/pages/change-password.tsx b/web/packages/accounts/pages/change-password.tsx
index d05da4a044..8f4c7e0999 100644
--- a/web/packages/accounts/pages/change-password.tsx
+++ b/web/packages/accounts/pages/change-password.tsx
@@ -24,13 +24,11 @@ import type {
UpdatedKey,
User,
} from "ente-accounts/services/user";
+import { generateAndSaveIntermediateKeyAttributes } from "ente-accounts/utils/helpers";
import { LinkButton } from "ente-base/components/LinkButton";
import { sharedCryptoWorker } from "ente-base/crypto";
import type { DerivedKey } from "ente-base/crypto/types";
-import {
- generateAndSaveIntermediateKeyAttributes,
- saveKeyInSessionStore,
-} from "ente-shared/crypto/helpers";
+import { saveKeyInSessionStore } from "ente-shared/crypto/helpers";
import { getData, setData } from "ente-shared/storage/localStorage";
import { getActualKey } from "ente-shared/user";
import { t } from "i18next";
diff --git a/web/packages/accounts/pages/credentials.tsx b/web/packages/accounts/pages/credentials.tsx
index 2dc62a5b37..e58e5047ea 100644
--- a/web/packages/accounts/pages/credentials.tsx
+++ b/web/packages/accounts/pages/credentials.tsx
@@ -30,6 +30,10 @@ import {
import type { SRPAttributes } from "ente-accounts/services/srp-remote";
import { getSRPAttributes } from "ente-accounts/services/srp-remote";
import type { KeyAttributes, User } from "ente-accounts/services/user";
+import {
+ decryptAndStoreToken,
+ generateAndSaveIntermediateKeyAttributes,
+} from "ente-accounts/utils/helpers";
import { LinkButton } from "ente-base/components/LinkButton";
import { LoadingIndicator } from "ente-base/components/loaders";
import { useBaseContext } from "ente-base/context";
@@ -37,11 +41,7 @@ import { sharedCryptoWorker } from "ente-base/crypto";
import type { B64EncryptionResult } from "ente-base/crypto/libsodium";
import { clearLocalStorage } from "ente-base/local-storage";
import log from "ente-base/log";
-import {
- decryptAndStoreToken,
- generateAndSaveIntermediateKeyAttributes,
- saveKeyInSessionStore,
-} from "ente-shared/crypto/helpers";
+import { saveKeyInSessionStore } from "ente-shared/crypto/helpers";
import { CustomError } from "ente-shared/error";
import { getData, setData, setLSUser } from "ente-shared/storage/localStorage";
import {
diff --git a/web/packages/accounts/pages/generate.tsx b/web/packages/accounts/pages/generate.tsx
index ff3411b449..76a3ece06d 100644
--- a/web/packages/accounts/pages/generate.tsx
+++ b/web/packages/accounts/pages/generate.tsx
@@ -14,14 +14,12 @@ import {
} from "ente-accounts/services/srp";
import type { KeyAttributes, User } from "ente-accounts/services/user";
import { putUserKeyAttributes } from "ente-accounts/services/user";
+import { generateAndSaveIntermediateKeyAttributes } from "ente-accounts/utils/helpers";
import { LinkButton } from "ente-base/components/LinkButton";
import { LoadingIndicator } from "ente-base/components/loaders";
import { useBaseContext } from "ente-base/context";
import log from "ente-base/log";
-import {
- generateAndSaveIntermediateKeyAttributes,
- saveKeyInSessionStore,
-} from "ente-shared/crypto/helpers";
+import { saveKeyInSessionStore } from "ente-shared/crypto/helpers";
import { getData } from "ente-shared/storage/localStorage";
import {
justSignedUp,
diff --git a/web/packages/accounts/pages/recover.tsx b/web/packages/accounts/pages/recover.tsx
index 22a69510cd..08b0a22945 100644
--- a/web/packages/accounts/pages/recover.tsx
+++ b/web/packages/accounts/pages/recover.tsx
@@ -7,6 +7,7 @@ import { recoveryKeyFromMnemonic } from "ente-accounts/services/recovery-key";
import { appHomeRoute, stashRedirect } from "ente-accounts/services/redirect";
import type { KeyAttributes, User } from "ente-accounts/services/user";
import { sendOTT } from "ente-accounts/services/user";
+import { decryptAndStoreToken } from "ente-accounts/utils/helpers";
import { LinkButton } from "ente-base/components/LinkButton";
import {
SingleInputForm,
@@ -15,10 +16,7 @@ import {
import { useBaseContext } from "ente-base/context";
import { decryptBox } from "ente-base/crypto";
import log from "ente-base/log";
-import {
- decryptAndStoreToken,
- saveKeyInSessionStore,
-} from "ente-shared/crypto/helpers";
+import { saveKeyInSessionStore } from "ente-shared/crypto/helpers";
import { getData, setData } from "ente-shared/storage/localStorage";
import { getKey } from "ente-shared/storage/sessionStorage";
import { t } from "i18next";
diff --git a/web/packages/accounts/utils/helpers.ts b/web/packages/accounts/utils/helpers.ts
new file mode 100644
index 0000000000..b2b9ae45af
--- /dev/null
+++ b/web/packages/accounts/utils/helpers.ts
@@ -0,0 +1,65 @@
+// TODO: Audit this file, this can be better. e.g. do we need the Object.assign?
+
+import { type KeyAttributes } from "ente-accounts/services/user";
+import {
+ boxSealOpenBytes,
+ decryptBox,
+ sharedCryptoWorker,
+ toB64URLSafe,
+} from "ente-base/crypto";
+import { getData, setData, setLSUser } from "ente-shared/storage/localStorage";
+
+export async function decryptAndStoreToken(
+ keyAttributes: KeyAttributes,
+ masterKey: string,
+) {
+ const user = getData("user");
+ const { encryptedToken } = user;
+
+ if (encryptedToken && encryptedToken.length > 0) {
+ const { encryptedSecretKey, secretKeyDecryptionNonce, publicKey } =
+ keyAttributes;
+ const privateKey = await decryptBox(
+ {
+ encryptedData: encryptedSecretKey,
+ nonce: secretKeyDecryptionNonce,
+ },
+ masterKey,
+ );
+
+ const decryptedToken = await toB64URLSafe(
+ await boxSealOpenBytes(encryptedToken, { publicKey, privateKey }),
+ );
+
+ await setLSUser({
+ ...user,
+ token: decryptedToken,
+ encryptedToken: null,
+ });
+ }
+}
+
+// We encrypt the masterKey, with an intermediate key derived from the
+// passphrase (with Interactive mem and ops limits) to avoid saving it to local
+// storage in plain text. This means that on the web user will always have to
+// enter their passphrase to access their masterKey.
+export async function generateAndSaveIntermediateKeyAttributes(
+ passphrase: string,
+ existingKeyAttributes: KeyAttributes,
+ key: string,
+): Promise<KeyAttributes> {
+ const cryptoWorker = await sharedCryptoWorker();
+ const intermediateKek = await cryptoWorker.deriveInteractiveKey(passphrase);
+ const { encryptedData: encryptedKey, nonce: keyDecryptionNonce } =
+ await cryptoWorker.encryptBox(key, intermediateKek.key);
+
+ const intermediateKeyAttributes = Object.assign(existingKeyAttributes, {
+ encryptedKey,
+ keyDecryptionNonce,
+ kekSalt: intermediateKek.salt,
+ opsLimit: intermediateKek.opsLimit,
+ memLimit: intermediateKek.memLimit,
+ });
+ setData("keyAttributes", intermediateKeyAttributes);
+ return intermediateKeyAttributes;
+}
diff --git a/web/packages/shared/crypto/helpers.ts b/web/packages/shared/crypto/helpers.ts
index 235cc319ba..315fa3b36d 100644
--- a/web/packages/shared/crypto/helpers.ts
+++ b/web/packages/shared/crypto/helpers.ts
@@ -1,68 +1,6 @@
-import { type KeyAttributes } from "ente-accounts/services/user";
-import {
- boxSealOpenBytes,
- decryptBox,
- sharedCryptoWorker,
- toB64URLSafe,
-} from "ente-base/crypto";
-import { getData, setData, setLSUser } from "ente-shared/storage/localStorage";
+import { sharedCryptoWorker } from "ente-base/crypto";
import { setKey, type SessionKey } from "ente-shared/storage/sessionStorage";
-export async function decryptAndStoreToken(
- keyAttributes: KeyAttributes,
- masterKey: string,
-) {
- const user = getData("user");
- const { encryptedToken } = user;
-
- if (encryptedToken && encryptedToken.length > 0) {
- const { encryptedSecretKey, secretKeyDecryptionNonce, publicKey } =
- keyAttributes;
- const privateKey = await decryptBox(
- {
- encryptedData: encryptedSecretKey,
- nonce: secretKeyDecryptionNonce,
- },
- masterKey,
- );
-
- const decryptedToken = await toB64URLSafe(
- await boxSealOpenBytes(encryptedToken, { publicKey, privateKey }),
- );
-
- await setLSUser({
- ...user,
- token: decryptedToken,
- encryptedToken: null,
- });
- }
-}
-
-// We encrypt the masterKey, with an intermediate key derived from the
-// passphrase (with Interactive mem and ops limits) to avoid saving it to local
-// storage in plain text. This means that on the web user will always have to
-// enter their passphrase to access their masterKey.
-export async function generateAndSaveIntermediateKeyAttributes(
- passphrase: string,
- existingKeyAttributes: KeyAttributes,
- key: string,
-): Promise<KeyAttributes> {
- const cryptoWorker = await sharedCryptoWorker();
- const intermediateKek = await cryptoWorker.deriveInteractiveKey(passphrase);
- const { encryptedData: encryptedKey, nonce: keyDecryptionNonce } =
- await cryptoWorker.encryptBox(key, intermediateKek.key);
-
- const intermediateKeyAttributes = Object.assign(existingKeyAttributes, {
- encryptedKey,
- keyDecryptionNonce,
- kekSalt: intermediateKek.salt,
- opsLimit: intermediateKek.opsLimit,
- memLimit: intermediateKek.memLimit,
- });
- setData("keyAttributes", intermediateKeyAttributes);
- return intermediateKeyAttributes;
-}
-
export const saveKeyInSessionStore = async (
keyType: SessionKey,
key: string,