Skip to content

armgit / ente

Public Imported from GitHub
0

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

Rest

Manav Rathi5c4b4b9194a7
diff --git a/web/packages/accounts/components/SignUpContents.tsx b/web/packages/accounts/components/SignUpContents.tsx
index 2392333843..3dad9a3ede 100644
--- a/web/packages/accounts/components/SignUpContents.tsx
+++ b/web/packages/accounts/components/SignUpContents.tsx
@@ -17,11 +17,9 @@ import {
saveJustSignedUp,
saveOriginalKeyAttributes,
stashReferralSource,
-} from "ente-accounts/services/accounts-db";
-import {
- generateSRPSetupAttributes,
stashSRPSetupAttributes,
-} from "ente-accounts/services/srp";
+} from "ente-accounts/services/accounts-db";
+import { generateSRPSetupAttributes } from "ente-accounts/services/srp";
import {
generateAndSaveInteractiveKeyAttributes,
generateKeysAndAttributes,
diff --git a/web/packages/accounts/pages/credentials.tsx b/web/packages/accounts/pages/credentials.tsx
index 7c2a30c03b..8579913702 100644
--- a/web/packages/accounts/pages/credentials.tsx
+++ b/web/packages/accounts/pages/credentials.tsx
@@ -18,10 +18,11 @@ import {
getData,
getToken,
savedIsFirstLogin,
+ savedKeyAttributes,
+ savedSRPAttributes,
saveIsFirstLogin,
saveKeyAttributes,
saveSRPAttributes,
- setData,
setLSUser,
} from "ente-accounts/services/accounts-db";
import {
@@ -138,8 +139,8 @@ const Page: React.FC = () => {
return;
}
const kek = await unstashKeyEncryptionKeyFromSession();
- const keyAttributes: KeyAttributes = getData("keyAttributes");
- const srpAttributes: SRPAttributes = getData("srpAttributes");
+ const keyAttributes = savedKeyAttributes();
+ const srpAttributes = savedSRPAttributes();
if (getToken()) {
setSessionValidityCheck(validateSession());
@@ -153,7 +154,7 @@ const Page: React.FC = () => {
},
kek,
);
- void postVerification(masterKey, kek, keyAttributes);
+ await postVerification(masterKey, kek, keyAttributes);
return;
}
@@ -242,7 +243,7 @@ const Page: React.FC = () => {
id,
isTwoFactorEnabled: false,
});
- if (keyAttributes) setData("keyAttributes", keyAttributes);
+ if (keyAttributes) saveKeyAttributes(keyAttributes);
return keyAttributes;
}
} catch (e) {
@@ -278,14 +279,14 @@ const Page: React.FC = () => {
await saveMasterKeyInSessionAndSafeStore(masterKey);
await decryptAndStoreToken(keyAttributes, masterKey);
try {
- let srpAttributes: SRPAttributes | null | undefined =
- getData("srpAttributes");
+ let srpAttributes = savedSRPAttributes();
if (!srpAttributes && user) {
srpAttributes = await getSRPAttributes(user.email);
if (srpAttributes) {
- setData("srpAttributes", srpAttributes);
+ saveSRPAttributes(srpAttributes);
}
}
+ // TODO: todo?
log.debug(() => `userSRPSetupPending ${!srpAttributes}`);
if (!srpAttributes) {
await setupSRP(await generateSRPSetupAttributes(kek));
diff --git a/web/packages/accounts/pages/recover.tsx b/web/packages/accounts/pages/recover.tsx
index 8b9da40e4f..92d07f86d4 100644
--- a/web/packages/accounts/pages/recover.tsx
+++ b/web/packages/accounts/pages/recover.tsx
@@ -3,7 +3,10 @@ import {
AccountsPageFooter,
AccountsPageTitle,
} from "ente-accounts/components/layouts/centered-paper";
-import { getData } from "ente-accounts/services/accounts-db";
+import {
+ getData,
+ savedKeyAttributes,
+} from "ente-accounts/services/accounts-db";
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";
@@ -30,13 +33,12 @@ const Page: React.FC = () => {
const [keyAttributes, setKeyAttributes] = useState<
KeyAttributes | undefined
- >();
+ >(undefined);
const router = useRouter();
useEffect(() => {
const user: User = getData("user");
- const keyAttributes: KeyAttributes = getData("keyAttributes");
if (!user?.email) {
void router.push("/");
return;
@@ -47,6 +49,8 @@ const Page: React.FC = () => {
void router.push("/verify");
return;
}
+
+ const keyAttributes = savedKeyAttributes();
if (!keyAttributes) {
void router.push("/generate");
} else if (haveCredentialsInSession()) {
diff --git a/web/packages/accounts/pages/two-factor/verify.tsx b/web/packages/accounts/pages/two-factor/verify.tsx
index a3f6849c2d..2c8f39f394 100644
--- a/web/packages/accounts/pages/two-factor/verify.tsx
+++ b/web/packages/accounts/pages/two-factor/verify.tsx
@@ -1,7 +1,7 @@
import { Verify2FACodeForm } from "ente-accounts/components/Verify2FACodeForm";
import {
getData,
- setData,
+ saveKeyAttributes,
setLSUser,
} from "ente-accounts/services/accounts-db";
import type { User } from "ente-accounts/services/user";
@@ -57,7 +57,7 @@ const Page: React.FC = () => {
token: undefined,
encryptedToken,
});
- setData("keyAttributes", keyAttributes);
+ saveKeyAttributes(keyAttributes);
await router.push(unstashRedirect() ?? "/credentials");
} catch (e) {
if (isHTTPErrorWithStatus(e, 404)) {
diff --git a/web/packages/accounts/pages/verify.tsx b/web/packages/accounts/pages/verify.tsx
index 8f72ccac44..d65e2c4d4b 100644
--- a/web/packages/accounts/pages/verify.tsx
+++ b/web/packages/accounts/pages/verify.tsx
@@ -9,11 +9,14 @@ import { SecondFactorChoice } from "ente-accounts/components/SecondFactorChoice"
import { useSecondFactorChoiceIfNeeded } from "ente-accounts/components/utils/second-factor-choice";
import {
getData,
+ savedKeyAttributes,
savedOriginalKeyAttributes,
+ savedSRPAttributes,
saveIsFirstLogin,
saveKeyAttributes,
saveOriginalKeyAttributes,
setLSUser,
+ unstashAfterUseSRPSetupAttributes,
unstashReferralSource,
} from "ente-accounts/services/accounts-db";
import {
@@ -24,13 +27,8 @@ import {
stashedRedirect,
unstashRedirect,
} from "ente-accounts/services/redirect";
-import {
- getSRPAttributes,
- setupSRP,
- unstashAndUseSRPSetupAttributes,
- type SRPAttributes,
-} from "ente-accounts/services/srp";
-import type { KeyAttributes, User } from "ente-accounts/services/user";
+import { getSRPAttributes, setupSRP } from "ente-accounts/services/srp";
+import type { User } from "ente-accounts/services/user";
import {
putUserKeyAttributes,
sendOTT,
@@ -142,7 +140,7 @@ const Page: React.FC = () => {
if (originalKeyAttributes) {
await putUserKeyAttributes(originalKeyAttributes);
}
- await unstashAndUseSRPSetupAttributes(setupSRP);
+ await unstashAfterUseSRPSetupAttributes(setupSRP);
}
saveIsFirstLogin();
const redirectURL = unstashRedirect();
@@ -261,7 +259,7 @@ const redirectionIfNeeded = async (user: User | undefined) => {
return "/";
}
- const keyAttributes: KeyAttributes = getData("keyAttributes");
+ const keyAttributes = savedKeyAttributes();
if (keyAttributes?.encryptedKey && (user.token || user.encryptedToken)) {
return "/credentials";
@@ -281,7 +279,7 @@ const redirectionIfNeeded = async (user: User | undefined) => {
// saved them). If they are present and indicate that email verification is
// not required, redirect to the password verification page.
- const srpAttributes: SRPAttributes = getData("srpAttributes");
+ const srpAttributes = savedSRPAttributes();
if (srpAttributes && !srpAttributes.isEmailMFAEnabled) {
// Fetch the latest SRP attributes instead of relying on the potentially
// stale stored values. This is an infrequent scenario path, so extra
diff --git a/web/packages/accounts/services/accounts-db.ts b/web/packages/accounts/services/accounts-db.ts
index 8582d619ec..19893578bd 100644
--- a/web/packages/accounts/services/accounts-db.ts
+++ b/web/packages/accounts/services/accounts-db.ts
@@ -2,15 +2,14 @@ import { getKVS, removeKV, setKV } from "ente-base/kv";
import log from "ente-base/log";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod/v4";
+import {
+ RemoteSRPAttributes,
+ SRPSetupAttributes,
+ type SRPAttributes,
+} from "./srp";
import { RemoteKeyAttributes, type KeyAttributes } from "./user";
-import { RemoteSRPAttributes, type SRPAttributes } from "./srp";
-export type LocalStorageKey =
- | "user"
- | "keyAttributes"
- // Moved to ente-accounts
- // "srpSetupAttributes"
- | "srpAttributes";
+export type LocalStorageKey = "user";
/**
* [Note: Accounts DB]
@@ -200,6 +199,39 @@ export const savedSRPAttributes = (): SRPAttributes | undefined => {
export const saveSRPAttributes = (srpAttributes: SRPAttributes) =>
localStorage.setItem("srpAttributes", JSON.stringify(srpAttributes));
+/**
+ * Save {@link SRPSetupAttributes} in local storage for later use via
+ * {@link unstashAfterUseSRPSetupAttributes}.
+ *
+ * See: [Note: SRP setup attributes]
+ */
+export const stashSRPSetupAttributes = (
+ srpSetupAttributes: SRPSetupAttributes,
+) =>
+ localStorage.setItem(
+ "srpSetupAttributes",
+ JSON.stringify(srpSetupAttributes),
+ );
+
+/**
+ * Retrieve the {@link SRPSetupAttributes}, if any, that were stashed by a
+ * previous call to {@link stashSRPSetupAttributes}.
+ *
+ * - If they are found, then invoke the provided callback ({@link cb}) with the
+ * value. If the promise returned by the callback fulfills, then remove the
+ * stashed value from local storage.
+ *
+ * - If they are not found, then the callback is not invoked.
+ */
+export const unstashAfterUseSRPSetupAttributes = async (
+ cb: (srpSetupAttributes: SRPSetupAttributes) => Promise<void>,
+) => {
+ const jsonString = localStorage.getItem("srpSetupAttributes");
+ if (!jsonString) return;
+ const srpSetupAttributes = SRPSetupAttributes.parse(JSON.parse(jsonString));
+ await cb(srpSetupAttributes);
+ localStorage.removeItem("srpSetupAttributes");
+};
export const getToken = (): string => {
const token = getData("user")?.token;
diff --git a/web/packages/accounts/services/session.ts b/web/packages/accounts/services/session.ts
index 8c51839582..414fba3378 100644
--- a/web/packages/accounts/services/session.ts
+++ b/web/packages/accounts/services/session.ts
@@ -1,6 +1,6 @@
import {
- getData,
savedOriginalKeyAttributes,
+ savedSRPAttributes,
} from "ente-accounts/services/accounts-db";
import type { KeyAttributes } from "ente-accounts/services/user";
import { authenticatedRequestHeaders, HTTPError } from "ente-base/http";
@@ -27,6 +27,9 @@ type SessionValidity =
const SessionValidityResponse = z.object({
hasSetKeys: z.boolean(),
+ /**
+ * Will not be present if {@link hasSetKeys} is `false`.
+ */
keyAttributes: RemoteKeyAttributes.nullish().transform(nullToUndefined),
});
@@ -85,15 +88,16 @@ export const checkSessionValidity = async (): Promise<SessionValidity> => {
else throw new HTTPError(res);
}
- // See if the response contains keyAttributes (they might not for older
- // deployments).
+ // See if the response contains keyAttributes (it will not if `hasSetKeys`
+ // in the response is `false`).
const { keyAttributes } = SessionValidityResponse.parse(await res.json());
if (keyAttributes) {
const remoteKeyAttributes = keyAttributes;
// We should have these values locally if we reach here.
const email = ensureLocalUser().email;
- const localSRPAttributes = getData("srpAttributes")!;
+ // TODO(RE): This is not being set until initial reauthentication.
+ const localSRPAttributes = savedSRPAttributes()!;
// Fetch the remote SRP attributes.
//
@@ -124,7 +128,7 @@ export const checkSessionValidity = async (): Promise<SessionValidity> => {
}
}
- // The token is still valid (to the best of our ascertainable knowledge).
+ // The token is still valid.
return { status: "valid" };
};
diff --git a/web/packages/accounts/services/srp.ts b/web/packages/accounts/services/srp.ts
index 3fded86f0c..7e3c1bf209 100644
--- a/web/packages/accounts/services/srp.ts
+++ b/web/packages/accounts/services/srp.ts
@@ -245,7 +245,7 @@ export const getSRPAttributes = async (
* temporarily stash them in local storage using an object that conforms to the
* following {@link SRPSetupAttributes} schema.
*/
-const SRPSetupAttributes = z.object({
+export const SRPSetupAttributes = z.object({
srpUserID: z.string(),
srpSalt: z.string(),
srpVerifier: z.string(),
@@ -301,40 +301,6 @@ const b64ToBuffer = (base64: string) => Buffer.from(base64, "base64");
const bufferToB64 = (buffer: Buffer) => buffer.toString("base64");
-/**
- * Save {@link SRPSetupAttributes} in local storage for later use via
- * {@link unstashAndUseSRPSetupAttributes}.
- *
- * See: [Note: SRP setup attributes]
- */
-export const stashSRPSetupAttributes = (
- srpSetupAttributes: SRPSetupAttributes,
-) =>
- localStorage.setItem(
- "srpSetupAttributes",
- JSON.stringify(srpSetupAttributes),
- );
-
-/**
- * Retrieve the {@link SRPSetupAttributes}, if any, that were stashed by a
- * previous call to {@link stashSRPSetupAttributes}.
- *
- * - If they are found, then invoke the provided callback ({@link cb}) with the
- * value. If the promise returned by the callback fulfills, then remove the
- * stashed value from local storage.
- *
- * - If they are not found, then the callback is not invoked.
- */
-export const unstashAndUseSRPSetupAttributes = async (
- cb: (srpSetupAttributes: SRPSetupAttributes) => Promise<void>,
-) => {
- const jsonString = localStorage.getItem("srpSetupAttributes");
- if (!jsonString) return;
- const srpSetupAttributes = SRPSetupAttributes.parse(JSON.parse(jsonString));
- await cb(srpSetupAttributes);
- localStorage.removeItem("srpSetupAttributes");
-};
-
/**
* Use the provided {@link SRPSetupAttributes} to, well, setup SRP.
*