Skip to content

armgit / ente

Public Imported from GitHub
0

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

Join

Manav Rathia938c82fc6c8
diff --git a/web/packages/accounts/components/LoginContents.tsx b/web/packages/accounts/components/LoginContents.tsx
index 8cfd131d47..968ebf81af 100644
--- a/web/packages/accounts/components/LoginContents.tsx
+++ b/web/packages/accounts/components/LoginContents.tsx
@@ -1,6 +1,6 @@
import { Input, Stack, TextField, Typography } from "@mui/material";
import { AccountsPageFooter } from "ente-accounts/components/layouts/centered-paper";
-import { getSRPAttributes } from "ente-accounts/services/srp-remote";
+import { getSRPAttributes } from "ente-accounts/services/srp";
import { sendOTT } from "ente-accounts/services/user";
import { LinkButton } from "ente-base/components/LinkButton";
import { LoadingButton } from "ente-base/components/mui/LoadingButton";
diff --git a/web/packages/accounts/components/VerifyMasterPasswordForm.tsx b/web/packages/accounts/components/VerifyMasterPasswordForm.tsx
index 274f5b420e..59999647ab 100644
--- a/web/packages/accounts/components/VerifyMasterPasswordForm.tsx
+++ b/web/packages/accounts/components/VerifyMasterPasswordForm.tsx
@@ -1,5 +1,5 @@
import { Input, TextField } from "@mui/material";
-import type { SRPAttributes } from "ente-accounts/services/srp-remote";
+import type { SRPAttributes } from "ente-accounts/services/srp";
import type { KeyAttributes, User } from "ente-accounts/services/user";
import { LoadingButton } from "ente-base/components/mui/LoadingButton";
import { ShowHidePasswordInputAdornment } from "ente-base/components/mui/PasswordInputAdornment";
diff --git a/web/packages/accounts/pages/change-password.tsx b/web/packages/accounts/pages/change-password.tsx
index 31b87169ab..b0e3c564f0 100644
--- a/web/packages/accounts/pages/change-password.tsx
+++ b/web/packages/accounts/pages/change-password.tsx
@@ -13,12 +13,10 @@ import {
deriveSRPPassword,
generateSRPClient,
generateSRPSetupAttributes,
-} from "ente-accounts/services/srp";
-import {
getSRPAttributes,
startSRPSetup,
updateSRPAndKeys,
-} from "ente-accounts/services/srp-remote";
+} from "ente-accounts/services/srp";
import type {
KeyAttributes,
UpdatedKey,
diff --git a/web/packages/accounts/pages/credentials.tsx b/web/packages/accounts/pages/credentials.tsx
index 1198a37d65..78b9780628 100644
--- a/web/packages/accounts/pages/credentials.tsx
+++ b/web/packages/accounts/pages/credentials.tsx
@@ -21,14 +21,14 @@ import {
unstashRedirect,
} from "ente-accounts/services/redirect";
import { checkSessionValidity } from "ente-accounts/services/session";
+import type { SRPAttributes } from "ente-accounts/services/srp";
import {
configureSRP,
deriveSRPPassword,
generateSRPSetupAttributes,
+ getSRPAttributes,
loginViaSRP,
} from "ente-accounts/services/srp";
-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,
diff --git a/web/packages/accounts/pages/verify.tsx b/web/packages/accounts/pages/verify.tsx
index 521ddbd95f..e09548ee0c 100644
--- a/web/packages/accounts/pages/verify.tsx
+++ b/web/packages/accounts/pages/verify.tsx
@@ -16,12 +16,11 @@ import {
stashedRedirect,
unstashRedirect,
} from "ente-accounts/services/redirect";
-import { configureSRP } from "ente-accounts/services/srp";
import type {
SRPAttributes,
SRPSetupAttributes,
-} from "ente-accounts/services/srp-remote";
-import { getSRPAttributes } from "ente-accounts/services/srp-remote";
+} from "ente-accounts/services/srp";
+import { configureSRP, getSRPAttributes } from "ente-accounts/services/srp";
import type { KeyAttributes, User } from "ente-accounts/services/user";
import {
putUserKeyAttributes,
diff --git a/web/packages/accounts/services/session.ts b/web/packages/accounts/services/session.ts
index 53abbdc4f7..09c88435b6 100644
--- a/web/packages/accounts/services/session.ts
+++ b/web/packages/accounts/services/session.ts
@@ -6,8 +6,8 @@ import { getAuthToken } from "ente-base/token";
import { getData } from "ente-shared/storage/localStorage";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod/v4";
-import type { SRPAttributes } from "./srp-remote";
-import { getSRPAttributes } from "./srp-remote";
+import type { SRPAttributes } from "./srp";
+import { getSRPAttributes } from "./srp";
import {
ensureLocalUser,
putUserKeyAttributes,
diff --git a/web/packages/accounts/services/srp-remote.ts b/web/packages/accounts/services/srp-remote.ts
deleted file mode 100644
index 79b86163e3..0000000000
--- a/web/packages/accounts/services/srp-remote.ts
+++ /dev/null
@@ -1,184 +0,0 @@
-import { HttpStatusCode } from "axios";
-import { ensureOk, publicRequestHeaders } from "ente-base/http";
-import log from "ente-base/log";
-import { apiURL } from "ente-base/origins";
-import { ApiError, CustomError } from "ente-shared/error";
-import HTTPService from "ente-shared/network/HTTPService";
-import type { UpdatedKey, UserVerificationResponse } from "./user";
-
-export interface SRPAttributes {
- srpUserID: string;
- srpSalt: string;
- memLimit: number;
- opsLimit: number;
- kekSalt: string;
- isEmailMFAEnabled: boolean;
-}
-
-export interface GetSRPAttributesResponse {
- attributes: SRPAttributes;
-}
-
-export interface SRPSetupAttributes {
- srpSalt: string;
- srpVerifier: string;
- srpUserID: string;
- loginSubKey: string;
-}
-
-export interface SetupSRPRequest {
- srpUserID: string;
- srpSalt: string;
- srpVerifier: string;
- srpA: string;
-}
-
-export interface SetupSRPResponse {
- setupID: string;
- srpB: string;
-}
-
-export interface CompleteSRPSetupRequest {
- setupID: string;
- srpM1: string;
-}
-
-export interface CompleteSRPSetupResponse {
- setupID: string;
- srpM2: string;
-}
-
-export interface CreateSRPSessionResponse {
- sessionID: string;
- srpB: string;
-}
-
-export interface SRPVerificationResponse extends UserVerificationResponse {
- srpM2: string;
-}
-
-export interface UpdateSRPAndKeysRequest {
- srpM1: string;
- setupID: string;
- updatedKeyAttr: UpdatedKey;
- /**
- * If true (default), then all existing sessions for the user will be
- * invalidated.
- */
- logOutOtherDevices?: boolean;
-}
-
-export interface UpdateSRPAndKeysResponse {
- srpM2: string;
- setupID: string;
-}
-
-export const getSRPAttributes = async (
- email: string,
-): Promise<SRPAttributes | null> => {
- try {
- const resp = await HTTPService.get(
- await apiURL("/users/srp/attributes"),
- { email },
- );
- return (resp.data as GetSRPAttributesResponse).attributes;
- } catch (e) {
- log.error("failed to get SRP attributes", e);
- return null;
- }
-};
-
-export const startSRPSetup = async (
- token: string,
- setupSRPRequest: SetupSRPRequest,
-): Promise<SetupSRPResponse> => {
- try {
- const resp = await HTTPService.post(
- await apiURL("/users/srp/setup"),
- setupSRPRequest,
- undefined,
- { "X-Auth-Token": token },
- );
-
- return resp.data as SetupSRPResponse;
- } catch (e) {
- log.error("failed to post SRP attributes", e);
- throw e;
- }
-};
-
-export const completeSRPSetup = async (
- token: string,
- completeSRPSetupRequest: CompleteSRPSetupRequest,
-) => {
- try {
- const resp = await HTTPService.post(
- await apiURL("/users/srp/complete"),
- completeSRPSetupRequest,
- undefined,
- { "X-Auth-Token": token },
- );
- return resp.data as CompleteSRPSetupResponse;
- } catch (e) {
- log.error("failed to complete SRP setup", e);
- throw e;
- }
-};
-
-export const createSRPSession = async (srpUserID: string, srpA: string) => {
- const res = await fetch(await apiURL("/users/srp/create-session"), {
- method: "POST",
- headers: publicRequestHeaders(),
- body: JSON.stringify({ srpUserID, srpA }),
- });
- ensureOk(res);
- const data = await res.json();
- // TODO: Use zod
- return data as CreateSRPSessionResponse;
-};
-
-export const verifySRPSession = async (
- sessionID: string,
- srpUserID: string,
- srpM1: string,
-) => {
- try {
- const resp = await HTTPService.post(
- await apiURL("/users/srp/verify-session"),
- { sessionID, srpUserID, srpM1 },
- undefined,
- );
- return resp.data as SRPVerificationResponse;
- } catch (e) {
- log.error("verifySRPSession failed", e);
- if (
- e instanceof ApiError &&
- // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
- e.httpStatusCode === HttpStatusCode.Unauthorized
- ) {
- // The API contract allows for a SRP verification 401 both because
- // of incorrect credentials or a non existent account.
- throw Error(CustomError.INCORRECT_PASSWORD_OR_NO_ACCOUNT);
- } else {
- throw e;
- }
- }
-};
-
-export const updateSRPAndKeys = async (
- token: string,
- updateSRPAndKeyRequest: UpdateSRPAndKeysRequest,
-): Promise<UpdateSRPAndKeysResponse> => {
- try {
- const resp = await HTTPService.post(
- await apiURL("/users/srp/update"),
- updateSRPAndKeyRequest,
- undefined,
- { "X-Auth-Token": token },
- );
- return resp.data as UpdateSRPAndKeysResponse;
- } catch (e) {
- log.error("updateSRPAndKeys failed", e);
- throw e;
- }
-};
diff --git a/web/packages/accounts/services/srp.ts b/web/packages/accounts/services/srp.ts
index f018d787a0..a8fce7b9d6 100644
--- a/web/packages/accounts/services/srp.ts
+++ b/web/packages/accounts/services/srp.ts
@@ -1,18 +1,15 @@
+import { HttpStatusCode } from "axios";
import type { KeyAttributes } from "ente-accounts/services/user";
import { deriveSubKeyBytes, sharedCryptoWorker, toB64 } from "ente-base/crypto";
+import { ensureOk, publicRequestHeaders } from "ente-base/http";
import log from "ente-base/log";
+import { apiURL } from "ente-base/origins";
+import { ApiError, CustomError } from "ente-shared/error";
+import HTTPService from "ente-shared/network/HTTPService";
import { getToken } from "ente-shared/storage/localStorage/helpers";
import { SRP, SrpClient } from "fast-srp-hap";
import { v4 as uuidv4 } from "uuid";
-import {
- completeSRPSetup,
- createSRPSession,
- startSRPSetup,
- verifySRPSession,
- type SRPAttributes,
- type SRPSetupAttributes,
-} from "./srp-remote";
-import type { UserVerificationResponse } from "./user";
+import type { UpdatedKey, UserVerificationResponse } from "./user";
/**
* Derive a "password" (which is really an arbitrary binary value, not human
@@ -30,6 +27,183 @@ export const deriveSRPPassword = async (kek: string) => {
return toB64(kekSubKeyBytes.slice(0, 16));
};
+export interface SRPAttributes {
+ srpUserID: string;
+ srpSalt: string;
+ memLimit: number;
+ opsLimit: number;
+ kekSalt: string;
+ isEmailMFAEnabled: boolean;
+}
+
+export interface GetSRPAttributesResponse {
+ attributes: SRPAttributes;
+}
+
+export interface SRPSetupAttributes {
+ srpSalt: string;
+ srpVerifier: string;
+ srpUserID: string;
+ loginSubKey: string;
+}
+
+export interface SetupSRPRequest {
+ srpUserID: string;
+ srpSalt: string;
+ srpVerifier: string;
+ srpA: string;
+}
+
+export interface SetupSRPResponse {
+ setupID: string;
+ srpB: string;
+}
+
+export interface CompleteSRPSetupRequest {
+ setupID: string;
+ srpM1: string;
+}
+
+export interface CompleteSRPSetupResponse {
+ setupID: string;
+ srpM2: string;
+}
+
+export interface CreateSRPSessionResponse {
+ sessionID: string;
+ srpB: string;
+}
+
+export interface SRPVerificationResponse extends UserVerificationResponse {
+ srpM2: string;
+}
+
+export interface UpdateSRPAndKeysRequest {
+ srpM1: string;
+ setupID: string;
+ updatedKeyAttr: UpdatedKey;
+ /**
+ * If true (default), then all existing sessions for the user will be
+ * invalidated.
+ */
+ logOutOtherDevices?: boolean;
+}
+
+export interface UpdateSRPAndKeysResponse {
+ srpM2: string;
+ setupID: string;
+}
+
+export const getSRPAttributes = async (
+ email: string,
+): Promise<SRPAttributes | null> => {
+ try {
+ const resp = await HTTPService.get(
+ await apiURL("/users/srp/attributes"),
+ { email },
+ );
+ return (resp.data as GetSRPAttributesResponse).attributes;
+ } catch (e) {
+ log.error("failed to get SRP attributes", e);
+ return null;
+ }
+};
+
+export const startSRPSetup = async (
+ token: string,
+ setupSRPRequest: SetupSRPRequest,
+): Promise<SetupSRPResponse> => {
+ try {
+ const resp = await HTTPService.post(
+ await apiURL("/users/srp/setup"),
+ setupSRPRequest,
+ undefined,
+ { "X-Auth-Token": token },
+ );
+
+ return resp.data as SetupSRPResponse;
+ } catch (e) {
+ log.error("failed to post SRP attributes", e);
+ throw e;
+ }
+};
+
+export const completeSRPSetup = async (
+ token: string,
+ completeSRPSetupRequest: CompleteSRPSetupRequest,
+) => {
+ try {
+ const resp = await HTTPService.post(
+ await apiURL("/users/srp/complete"),
+ completeSRPSetupRequest,
+ undefined,
+ { "X-Auth-Token": token },
+ );
+ return resp.data as CompleteSRPSetupResponse;
+ } catch (e) {
+ log.error("failed to complete SRP setup", e);
+ throw e;
+ }
+};
+
+export const createSRPSession = async (srpUserID: string, srpA: string) => {
+ const res = await fetch(await apiURL("/users/srp/create-session"), {
+ method: "POST",
+ headers: publicRequestHeaders(),
+ body: JSON.stringify({ srpUserID, srpA }),
+ });
+ ensureOk(res);
+ const data = await res.json();
+ // TODO: Use zod
+ return data as CreateSRPSessionResponse;
+};
+
+export const verifySRPSession = async (
+ sessionID: string,
+ srpUserID: string,
+ srpM1: string,
+) => {
+ try {
+ const resp = await HTTPService.post(
+ await apiURL("/users/srp/verify-session"),
+ { sessionID, srpUserID, srpM1 },
+ undefined,
+ );
+ return resp.data as SRPVerificationResponse;
+ } catch (e) {
+ log.error("verifySRPSession failed", e);
+ if (
+ e instanceof ApiError &&
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
+ e.httpStatusCode === HttpStatusCode.Unauthorized
+ ) {
+ // The API contract allows for a SRP verification 401 both because
+ // of incorrect credentials or a non existent account.
+ throw Error(CustomError.INCORRECT_PASSWORD_OR_NO_ACCOUNT);
+ } else {
+ throw e;
+ }
+ }
+};
+
+export const updateSRPAndKeys = async (
+ token: string,
+ updateSRPAndKeyRequest: UpdateSRPAndKeysRequest,
+): Promise<UpdateSRPAndKeysResponse> => {
+ try {
+ const resp = await HTTPService.post(
+ await apiURL("/users/srp/update"),
+ updateSRPAndKeyRequest,
+ undefined,
+ { "X-Auth-Token": token },
+ );
+ return resp.data as UpdateSRPAndKeysResponse;
+ } catch (e) {
+ log.error("updateSRPAndKeys failed", e);
+ throw e;
+ }
+};
+
export const configureSRP = async ({
srpSalt,
srpUserID,