This commit is contained in:
Manav Rathi
2025-06-04 13:45:32 +05:30
parent 2715bd81b0
commit a244140348
3 changed files with 51 additions and 46 deletions

View File

@@ -99,7 +99,7 @@ export const AuthenticateUser: React.FC<AuthenticateUserProps> = ({
user={user}
keyAttributes={keyAttributes}
submitButtonTitle={t("authenticate")}
onSubmit={() => {
onVerify={() => {
onClose();
onAuthenticate();
}}

View File

@@ -4,11 +4,11 @@ import { LoadingButton } from "ente-base/components/mui/LoadingButton";
import { ShowHidePasswordInputAdornment } from "ente-base/components/mui/PasswordInputAdornment";
import { sharedCryptoWorker } from "ente-base/crypto";
import log from "ente-base/log";
import { CustomError } from "ente-shared/error";
import type { KeyAttributes, User } from "ente-shared/user/types";
import { useFormik } from "formik";
import { t } from "i18next";
import { useCallback, useState } from "react";
import { CustomError } from "ente-shared/error"
import type { KeyAttributes, User } from "ente-shared/user/types";
export interface VerifyMasterPasswordFormProps {
/**
@@ -42,16 +42,22 @@ export interface VerifyMasterPasswordFormProps {
* The callback invoked with the verified password, and all the other
* auxillary information that was ascertained when verifying it.
*
* @param key The user's master key
* @param key The user's master key obtained after decrypting it from their
* passphrase.
*
* @param kek
*
* @param keyAttributes
* @param passphrase The plaintext password
*
* @param passphrase The plaintext passphrase. This can be used during login
* to derive another encrypted key using interactive mem/ops limits for
* faster reauthentication after the initial login.
*/
onSubmit: (
onVerify: (
key: string,
kek: string,
keyAttributes: KeyAttributes,
passphrase?: string,
passphrase: string,
) => void;
}
@@ -66,7 +72,7 @@ export const VerifyMasterPasswordForm: React.FC<
keyAttributes,
srpAttributes,
getKeyAttributes,
onSubmit,
onVerify,
submitButtonTitle,
}) => {
const [showPassword, setShowPassword] = useState(false);
@@ -131,7 +137,7 @@ export const VerifyMasterPasswordForm: React.FC<
keyAttributes.keyDecryptionNonce,
kek,
);
onSubmit(key, kek, keyAttributes, passphrase);
onVerify(key, kek, keyAttributes, passphrase);
} catch (e) {
log.error("user entered a wrong password", e);
throw Error(CustomError.INCORRECT_PASSWORD);

View File

@@ -150,8 +150,7 @@ const Page: React.FC = () => {
keyAttributes.keyDecryptionNonce,
kek,
);
// eslint-disable-next-line react-hooks/rules-of-hooks
useMasterPassword(key, kek, keyAttributes);
void postVerification(key, kek, keyAttributes);
return;
}
if (keyAttributes) {
@@ -258,46 +257,46 @@ const Page: React.FC = () => {
}
};
// eslint-disable-next-line @typescript-eslint/no-misused-promises
const useMasterPassword: VerifyMasterPasswordFormProps["onSubmit"] = async (
key,
kek,
keyAttributes,
passphrase,
const handleVerifyMasterPassword: VerifyMasterPasswordFormProps["onVerify"] =
(key, kek, keyAttributes, passphrase) => {
void (async () => {
if (isFirstLogin()) {
await generateAndSaveIntermediateKeyAttributes(
passphrase,
keyAttributes,
key,
);
}
await postVerification(key, kek, keyAttributes);
})();
};
const postVerification = async (
key: string,
kek: string,
keyAttributes: KeyAttributes,
) => {
await saveKeyInSessionStore("encryptionKey", key);
await decryptAndStoreToken(keyAttributes, key);
try {
if (isFirstLogin() && passphrase) {
await generateAndSaveIntermediateKeyAttributes(
passphrase,
keyAttributes,
key,
);
}
await saveKeyInSessionStore("encryptionKey", key);
await decryptAndStoreToken(keyAttributes, key);
try {
let srpAttributes: SRPAttributes | null =
getData("srpAttributes");
if (!srpAttributes && user) {
srpAttributes = await getSRPAttributes(user.email);
if (srpAttributes) {
setData("srpAttributes", srpAttributes);
}
let srpAttributes: SRPAttributes | null = getData("srpAttributes");
if (!srpAttributes && user) {
srpAttributes = await getSRPAttributes(user.email);
if (srpAttributes) {
setData("srpAttributes", srpAttributes);
}
log.debug(() => `userSRPSetupPending ${!srpAttributes}`);
if (!srpAttributes) {
const loginSubKey = await generateLoginSubKey(kek);
const srpSetupAttributes =
await generateSRPSetupAttributes(loginSubKey);
await configureSRP(srpSetupAttributes);
}
} catch (e) {
log.error("migrate to srp failed", e);
}
void router.push(unstashRedirect() ?? appHomeRoute);
log.debug(() => `userSRPSetupPending ${!srpAttributes}`);
if (!srpAttributes) {
const loginSubKey = await generateLoginSubKey(kek);
const srpSetupAttributes =
await generateSRPSetupAttributes(loginSubKey);
await configureSRP(srpSetupAttributes);
}
} catch (e) {
log.error("useMasterPassword failed", e);
log.error("migrate to srp failed", e);
}
void router.push(unstashRedirect() ?? appHomeRoute);
};
if (!keyAttributes && !srpAttributes) {
@@ -341,7 +340,7 @@ const Page: React.FC = () => {
getKeyAttributes={getKeyAttributes}
srpAttributes={srpAttributes}
submitButtonTitle={t("sign_in")}
onSubmit={useMasterPassword}
onVerify={handleVerifyMasterPassword}
/>
<AccountsPageFooterWithHost>
<LinkButton onClick={() => router.push("/recover")}>