Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use

Manav Rathi2df06ccf618e
diff --git a/web/packages/accounts/services/user.ts b/web/packages/accounts/services/user.ts
index f75c72679f..130c4020b1 100644
--- a/web/packages/accounts/services/user.ts
+++ b/web/packages/accounts/services/user.ts
@@ -55,10 +55,16 @@ export const localUser = (): LocalUser | undefined => {
/**
* A wrapper over {@link localUser} with that throws if no one is logged in.
*/
-export const ensureLocalUser = (): LocalUser => {
- const user = localUser();
- if (!user) throw new Error("Not logged in");
- return user;
+export const ensureLocalUser = (): LocalUser =>
+ ensureExpectedLoggedInValue(localUser());
+
+/**
+ * A helper function that throws an error if a value that is expected to be
+ * truthy when the user is logged in is instead falsey.
+ */
+const ensureExpectedLoggedInValue = <T>(t: T | undefined): T => {
+ if (!t) throw new Error("Not logged in");
+ return t;
};
/**
@@ -234,6 +240,13 @@ export const savedKeyAttributes = (): KeyAttributes | undefined => {
return RemoteKeyAttributes.parse(JSON.parse(jsonString));
};
+/**
+ * A variant of {@link savedKeyAttributes} that throws if keyAttributes are not
+ * present in local storage.
+ */
+export const ensureSavedKeyAttributes = (): KeyAttributes =>
+ ensureExpectedLoggedInValue(savedKeyAttributes());
+
export interface UserVerificationResponse {
id: number;
keyAttributes?: KeyAttributes | undefined;
diff --git a/web/packages/shared/crypto/helpers.ts b/web/packages/shared/crypto/helpers.ts
index dd20cb9cb4..330cb91bf5 100644
--- a/web/packages/shared/crypto/helpers.ts
+++ b/web/packages/shared/crypto/helpers.ts
@@ -1,8 +1,11 @@
-import type { KeyAttributes } from "ente-accounts/services/user";
+import {
+ ensureSavedKeyAttributes,
+ type KeyAttributes,
+} from "ente-accounts/services/user";
import { sharedCryptoWorker } from "ente-base/crypto";
import { ensureMasterKeyFromSession } from "ente-base/session";
import { getData, setData, setLSUser } from "ente-shared/storage/localStorage";
-import { type SessionKey, setKey } from "ente-shared/storage/sessionStorage";
+import { setKey, type SessionKey } from "ente-shared/storage/sessionStorage";
export async function decryptAndStoreToken(
keyAttributes: KeyAttributes,
@@ -88,7 +91,7 @@ export const decryptDeleteAccountChallenge = async (
) => {
const cryptoWorker = await sharedCryptoWorker();
const masterKey = await ensureMasterKeyFromSession();
- const keyAttributes = getData("keyAttributes");
+ const keyAttributes = ensureSavedKeyAttributes();
const secretKey = await cryptoWorker.decryptBox(
{
encryptedData: keyAttributes.encryptedSecretKey,