This commit is contained in:
Manav Rathi
2025-07-03 08:17:55 +05:30
parent e856a653b8
commit aec3ec718b
7 changed files with 41 additions and 35 deletions

View File

@@ -1,9 +1,7 @@
import { Input, Stack, TextField, Typography } from "@mui/material";
import { AccountsPageFooter } from "ente-accounts/components/layouts/centered-paper";
import {
getSRPAttributes,
saveSRPAttributes,
} from "ente-accounts/services/srp";
import { saveSRPAttributes } from "ente-accounts/services/accounts-db";
import { getSRPAttributes } from "ente-accounts/services/srp";
import { savePartialLocalUser, sendOTT } from "ente-accounts/services/user";
import { LinkButton } from "ente-base/components/LinkButton";
import { LoadingButton } from "ente-base/components/mui/LoadingButton";

View File

@@ -19,6 +19,8 @@ import {
getToken,
savedIsFirstLogin,
saveIsFirstLogin,
saveKeyAttributes,
saveSRPAttributes,
setData,
setLSUser,
} from "ente-accounts/services/accounts-db";
@@ -105,8 +107,8 @@ const Page: React.FC = () => {
case "valid":
break;
case "validButPasswordChanged":
setData("keyAttributes", session.updatedKeyAttributes);
setData("srpAttributes", session.updatedSRPAttributes);
saveKeyAttributes(session.updatedKeyAttributes);
saveSRPAttributes(session.updatedSRPAttributes);
// Set a flag that causes new interactive key attributes to
// be generated.
saveIsFirstLogin();

View File

@@ -28,8 +28,8 @@ const Page: React.FC = () => {
const response = searchParams.get("response");
if (!passkeySessionID || !response) return;
void saveCredentialsAndNavigateTo(passkeySessionID, response).then(
(slug: string) => router.push(slug),
void saveQueryCredentialsAndNavigateTo(passkeySessionID, response).then(
(slug) => router.push(slug),
);
}, [router]);
@@ -51,10 +51,13 @@ export default Page;
*
* @returns the slug that we should navigate to now.
*/
const saveCredentialsAndNavigateTo = async (
const saveQueryCredentialsAndNavigateTo = async (
passkeySessionID: string,
response: string,
) => {
// This function's implementation is on the same lines as that of the
// `saveCredentialsAndNavigateTo` function in passkey utilities.
const inflightPasskeySessionID = nullToUndefined(
sessionStorage.getItem("inflightPasskeySessionID"),
);

View File

@@ -3,6 +3,7 @@ import log from "ente-base/log";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod/v4";
import { RemoteKeyAttributes, type KeyAttributes } from "./user";
import { RemoteSRPAttributes, type SRPAttributes } from "./srp";
export type LocalStorageKey =
| "user"
@@ -179,6 +180,27 @@ export const saveOriginalKeyAttributes = (keyAttributes: KeyAttributes) =>
JSON.stringify(keyAttributes),
);
/**
* Return the user's {@link SRPAttributes} if they are present in local storage.
*
* Like key attributes, SRP attributes are also stored in the browser's local
* storage so will not be accessible to web workers.
*/
export const savedSRPAttributes = (): SRPAttributes | undefined => {
const jsonString = localStorage.getItem("srpAttributes");
if (!jsonString) return undefined;
return RemoteSRPAttributes.parse(JSON.parse(jsonString));
};
/**
* Save the user's {@link SRPAttributes} in local storage.
*
* Use {@link savedSRPAttributes} to retrieve them.
*/
export const saveSRPAttributes = (srpAttributes: SRPAttributes) =>
localStorage.setItem("srpAttributes", JSON.stringify(srpAttributes));
export const getToken = (): string => {
const token = getData("user")?.token;
return token;

View File

@@ -1,6 +1,6 @@
import {
getData,
setData,
saveKeyAttributes,
setLSUser,
} from "ente-accounts/services/accounts-db";
import { TwoFactorAuthorizationResponse } from "ente-accounts/services/user";
@@ -243,12 +243,13 @@ export const checkPasskeyVerificationStatus = async (
export const saveCredentialsAndNavigateTo = async (
response: TwoFactorAuthorizationResponse,
) => {
// This method somewhat duplicates `saveCredentialsAndNavigateTo` in the
// /passkeys/finish page.
// The implementation is similar to the `saveQueryCredentialsAndNavigateTo`
// function on the "/passkeys/finish" page.
const { id, encryptedToken, keyAttributes } = response;
await setLSUser({ ...getData("user"), encryptedToken, id });
setData("keyAttributes", keyAttributes);
saveKeyAttributes(keyAttributes);
return unstashRedirect() ?? "/credentials";
};

View File

@@ -190,7 +190,7 @@ export interface SRPAttributes {
* it to local storage, so the same schema describes both the remote type and
* the local storage type.
*/
const RemoteSRPAttributes = z.object({
export const RemoteSRPAttributes = z.object({
srpUserID: z.string(),
srpSalt: z.string(),
memLimit: z.number(),
@@ -221,26 +221,6 @@ export const getSRPAttributes = async (
.attributes;
};
/**
* Return the user's {@link SRPAttributes} if they are present in local storage.
*
* Like key attributes, SRP attributes are also stored in the browser's local
* storage so will not be accessible to web workers.
*/
export const savedSRPAttributes = (): SRPAttributes | undefined => {
const jsonString = localStorage.getItem("srpAttributes");
if (!jsonString) return undefined;
return RemoteSRPAttributes.parse(JSON.parse(jsonString));
};
/**
* Save the user's {@link SRPAttributes} in local storage.
*
* Use {@link savedSRPAttributes} to retrieve them.
*/
export const saveSRPAttributes = (srpAttributes: SRPAttributes) =>
localStorage.setItem("srpAttributes", JSON.stringify(srpAttributes));
/**
* A local-only structure holding information required for SRP setup.
*

View File

@@ -2,12 +2,12 @@ import {
getData,
savedKeyAttributes,
saveKeyAttributes,
saveSRPAttributes,
setLSUser,
} from "ente-accounts/services/accounts-db";
import {
generateSRPSetupAttributes,
getSRPAttributes,
saveSRPAttributes,
updateSRPAndKeyAttributes,
type UpdatedKeyAttr,
} from "ente-accounts/services/srp";