π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
migrated recover page
Abhinav42d40378cce3
diff --git a/apps/photos/src/pages/_app.tsx b/apps/photos/src/pages/_app.tsxindex dfe0b4c987..de3fae7278 100644--- a/apps/photos/src/pages/_app.tsx+++ b/apps/photos/src/pages/_app.tsx@@ -32,6 +32,7 @@ import {SetDialogBoxAttributes,DialogBoxAttributes,DialogBoxAttributesV2,+ SetDialogBoxAttributesV2,} from 'types/dialogBox';import {getFamilyPortalRedirectURL,@@ -120,7 +121,7 @@ type AppContextType = {themeColor: THEME_COLOR;setThemeColor: SetTheme;somethingWentWrong: () => void;- setDialogBoxAttributesV2: (attributes: DialogBoxAttributesV2) => void;+ setDialogBoxAttributesV2: SetDialogBoxAttributesV2;};export const AppContext = createContext<AppContextType>(null);diff --git a/apps/photos/src/pages/recover/index.tsx b/apps/photos/src/pages/recover/index.tsxindex 8a001f03dc..93a05b8bca 100644--- a/apps/photos/src/pages/recover/index.tsx+++ b/apps/photos/src/pages/recover/index.tsx@@ -1,122 +1,17 @@-import React, { useContext, useEffect, useState } from 'react';-import { t } from 'i18next';--import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';+import RecoverPage from '@ente/accounts/pages/recover';import { useRouter } from 'next/router';-import { PAGES } from 'constants/pages';-import { decryptAndStoreToken, saveKeyInSessionStore } from 'utils/crypto';-import SingleInputForm, {- SingleInputFormProps,-} from 'components/SingleInputForm';-import { VerticallyCentered } from 'components/Container';import { AppContext } from 'pages/_app';-import { logError } from 'utils/sentry';-import { getKey, SESSION_KEYS } from 'utils/storage/sessionStorage';-import { KeyAttributes, User } from 'types/user';-import FormPaper from 'components/Form/FormPaper';-import FormPaperTitle from 'components/Form/FormPaper/Title';-import FormPaperFooter from 'components/Form/FormPaper/Footer';-import LinkButton from 'components/pages/gallery/LinkButton';-import ComlinkCryptoWorker from 'utils/comlink/ComlinkCryptoWorker';-import { sendOtt } from 'services/userService';-import InMemoryStore, { MS_KEYS } from 'services/InMemoryStore';-const bip39 = require('bip39');-// mobile client library only supports english.-bip39.setDefaultWordlist('english');+import { useContext } from 'react';+import { APPS } from '@ente/shared/apps/constants';export default function Recover() {- const router = useRouter();- const [keyAttributes, setKeyAttributes] = useState<KeyAttributes>();const appContext = useContext(AppContext);-- useEffect(() => {- router.prefetch(PAGES.GALLERY);- const user: User = getData(LS_KEYS.USER);- const keyAttributes: KeyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);- const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);- if (!user?.email) {- router.push(PAGES.ROOT);- return;- }- if (!user?.encryptedToken && !user?.token) {- sendOtt(user.email);- InMemoryStore.set(MS_KEYS.REDIRECT_URL, PAGES.RECOVER);- router.push(PAGES.VERIFY);- return;- }- if (!keyAttributes) {- router.push(PAGES.GENERATE);- } else if (key) {- router.push(PAGES.GALLERY);- } else {- setKeyAttributes(keyAttributes);- }- appContext.showNavBar(true);- }, []);-- const recover: SingleInputFormProps['callback'] = async (- recoveryKey: string,- setFieldError- ) => {- try {- recoveryKey = recoveryKey- .trim()- .split(' ')- .map((part) => part.trim())- .filter((part) => !!part)- .join(' ');- // check if user is entering mnemonic recovery key- if (recoveryKey.indexOf(' ') > 0) {- if (recoveryKey.split(' ').length !== 24) {- throw new Error('recovery code should have 24 words');- }- recoveryKey = bip39.mnemonicToEntropy(recoveryKey);- }- const cryptoWorker = await ComlinkCryptoWorker.getInstance();- const masterKey = await cryptoWorker.decryptB64(- keyAttributes.masterKeyEncryptedWithRecoveryKey,- keyAttributes.masterKeyDecryptionNonce,- await cryptoWorker.fromHex(recoveryKey)- );- await saveKeyInSessionStore(SESSION_KEYS.ENCRYPTION_KEY, masterKey);- await decryptAndStoreToken(keyAttributes, masterKey);-- setData(LS_KEYS.SHOW_BACK_BUTTON, { value: false });- router.push(PAGES.CHANGE_PASSWORD);- } catch (e) {- logError(e, 'password recovery failed');- setFieldError(t('INCORRECT_RECOVERY_KEY'));- }- };-- const showNoRecoveryKeyMessage = () => {- appContext.setDialogMessage({- title: t('SORRY'),- close: {},- content: t('NO_RECOVERY_KEY_MESSAGE'),- });- };-+ const router = useRouter();return (- <VerticallyCentered>- <FormPaper>- <FormPaperTitle>{t('RECOVER_ACCOUNT')}</FormPaperTitle>- <SingleInputForm- callback={recover}- fieldType="text"- placeholder={t('RECOVERY_KEY_HINT')}- buttonText={t('RECOVER')}- disableAutoComplete- />- <FormPaperFooter style={{ justifyContent: 'space-between' }}>- <LinkButton onClick={showNoRecoveryKeyMessage}>- {t('NO_RECOVERY_KEY')}- </LinkButton>- <LinkButton onClick={router.back}>- {t('GO_BACK')}- </LinkButton>- </FormPaperFooter>- </FormPaper>- </VerticallyCentered>+ <RecoverPage+ appContext={appContext}+ router={router}+ appName={APPS.PHOTOS}+ />);}diff --git a/packages/accounts/pages/recover.tsx b/packages/accounts/pages/recover.tsxnew file mode 100644index 0000000000..f2f2fae274--- /dev/null+++ b/packages/accounts/pages/recover.tsx@@ -0,0 +1,122 @@+import React, { useEffect, useState } from 'react';+import { t } from 'i18next';++import { getData, LS_KEYS, setData } from '@ente/shared/storage/localStorage';+import { PAGES } from '@ente/accounts/constants/pages';+import {+ decryptAndStoreToken,+ saveKeyInSessionStore,+} from '@ente/shared/crypto/helpers';+import SingleInputForm, {+ SingleInputFormProps,+} from '@ente/shared/components/SingleInputForm';+import { VerticallyCentered } from '@ente/shared/components/Container';+// import { logError } from 'utils/sentry';+import { getKey, SESSION_KEYS } from '@ente/shared/storage/sessionStorage';+import { KeyAttributes, User } from '@ente/shared/user/types';+import FormPaper from '@ente/shared/components/Form/FormPaper';+import FormPaperTitle from '@ente/shared/components/Form/FormPaper/Title';+import FormPaperFooter from '@ente/shared/components/Form/FormPaper/Footer';+import LinkButton from '@ente/shared/components/LinkButton';+import ComlinkCryptoWorker from '@ente/shared/crypto';+import { sendOtt } from '@ente/accounts/api/user';+import InMemoryStore, { MS_KEYS } from '@ente/shared/storage/InMemoryStore';+import { PageProps } from '@ente/shared/apps/types';+const bip39 = require('bip39');+// mobile client library only supports english.+bip39.setDefaultWordlist('english');++export default function Recover({ appContext, router, appName }: PageProps) {+ const [keyAttributes, setKeyAttributes] = useState<KeyAttributes>();++ useEffect(() => {+ router.prefetch(PAGES.GALLERY);+ const user: User = getData(LS_KEYS.USER);+ const keyAttributes: KeyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);+ const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);+ if (!user?.email) {+ router.push(PAGES.ROOT);+ return;+ }+ if (!user?.encryptedToken && !user?.token) {+ sendOtt(appName, user.email);+ InMemoryStore.set(MS_KEYS.REDIRECT_URL, PAGES.RECOVER);+ router.push(PAGES.VERIFY);+ return;+ }+ if (!keyAttributes) {+ router.push(PAGES.GENERATE);+ } else if (key) {+ router.push(PAGES.GALLERY);+ } else {+ setKeyAttributes(keyAttributes);+ }+ appContext.showNavBar(true);+ }, []);++ const recover: SingleInputFormProps['callback'] = async (+ recoveryKey: string,+ setFieldError+ ) => {+ try {+ recoveryKey = recoveryKey+ .trim()+ .split(' ')+ .map((part) => part.trim())+ .filter((part) => !!part)+ .join(' ');+ // check if user is entering mnemonic recovery key+ if (recoveryKey.indexOf(' ') > 0) {+ if (recoveryKey.split(' ').length !== 24) {+ throw new Error('recovery code should have 24 words');+ }+ recoveryKey = bip39.mnemonicToEntropy(recoveryKey);+ }+ const cryptoWorker = await ComlinkCryptoWorker.getInstance();+ const masterKey = await cryptoWorker.decryptB64(+ keyAttributes.masterKeyEncryptedWithRecoveryKey,+ keyAttributes.masterKeyDecryptionNonce,+ await cryptoWorker.fromHex(recoveryKey)+ );+ await saveKeyInSessionStore(SESSION_KEYS.ENCRYPTION_KEY, masterKey);+ await decryptAndStoreToken(keyAttributes, masterKey);++ setData(LS_KEYS.SHOW_BACK_BUTTON, { value: false });+ router.push(PAGES.CHANGE_PASSWORD);+ } catch (e) {+ // logError(e, 'password recovery failed');+ setFieldError(t('INCORRECT_RECOVERY_KEY'));+ }+ };++ const showNoRecoveryKeyMessage = () => {+ appContext.setDialogBoxAttributesV2({+ title: t('SORRY'),+ close: {},+ content: t('NO_RECOVERY_KEY_MESSAGE'),+ });+ };++ return (+ <VerticallyCentered>+ <FormPaper>+ <FormPaperTitle>{t('RECOVER_ACCOUNT')}</FormPaperTitle>+ <SingleInputForm+ callback={recover}+ fieldType="text"+ placeholder={t('RECOVERY_KEY_HINT')}+ buttonText={t('RECOVER')}+ disableAutoComplete+ />+ <FormPaperFooter style={{ justifyContent: 'space-between' }}>+ <LinkButton onClick={showNoRecoveryKeyMessage}>+ {t('NO_RECOVERY_KEY')}+ </LinkButton>+ <LinkButton onClick={router.back}>+ {t('GO_BACK')}+ </LinkButton>+ </FormPaperFooter>+ </FormPaper>+ </VerticallyCentered>+ );+}diff --git a/packages/shared/apps/types.ts b/packages/shared/apps/types.tsindex 87ac58fac8..a02926e710 100644--- a/packages/shared/apps/types.ts+++ b/packages/shared/apps/types.ts@@ -1,9 +1,11 @@import { NextRouter } from 'next/router';import { APPS } from './constants';+import { SetDialogBoxAttributesV2 } from '@ente/shared/components/DialogBoxV2';export interface PageProps {appContext: {showNavBar: (show: boolean) => void;+ setDialogBoxAttributesV2: SetDialogBoxAttributesV2;};router: NextRouter;appName: APPS;diff --git a/packages/shared/components/DialogBoxV2.tsx b/packages/shared/components/DialogBoxV2.tsxnew file mode 100644index 0000000000..424e0fb762--- /dev/null+++ b/packages/shared/components/DialogBoxV2.tsx@@ -0,0 +1,37 @@+import { ButtonProps } from '@mui/material';++export interface DialogBoxAttributesV2 {+ icon?: React.ReactNode;+ title?: string;+ staticBackdrop?: boolean;+ nonClosable?: boolean;+ content?: any;+ close?: {+ text?: string;+ variant?: ButtonProps['color'];+ action?: () => void;+ };+ proceed?: {+ text: string;+ action: (setLoading?: (value: boolean) => void) => void | Promise<void>;+ variant?: ButtonProps['color'];+ disabled?: boolean;+ };+ secondary?: {+ text: string;+ action: () => void;+ variant?: ButtonProps['color'];+ disabled?: boolean;+ };+ buttons?: {+ text: string;+ action: () => void;+ variant: ButtonProps['color'];+ disabled?: boolean;+ }[];+ buttonDirection?: 'row' | 'column';+}++export type SetDialogBoxAttributesV2 = React.Dispatch<+ React.SetStateAction<DialogBoxAttributesV2>+>;