Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: allow using encryption key to restore backup

Abdullah Atta9a4f3ccbadfc
diff --git a/apps/web/package-lock.json b/apps/web/package-lock.json
index a654cd829..7888b95fd 100644
--- a/apps/web/package-lock.json
+++ b/apps/web/package-lock.json
@@ -439,7 +439,7 @@
},
"../desktop": {
"name": "@notesnook/desktop",
- "version": "2.6.14",
+ "version": "2.6.15",
"hasInstallScript": true,
"license": "GPL-3.0-or-later",
"dependencies": {
diff --git a/apps/web/src/common/dialog-controller.tsx b/apps/web/src/common/dialog-controller.tsx
index c3a225851..44c622429 100644
--- a/apps/web/src/common/dialog-controller.tsx
+++ b/apps/web/src/common/dialog-controller.tsx
@@ -381,13 +381,6 @@ function getDialogData(type: string) {
subtitle: "Please enter your vault password to continue.",
positiveButtonText: "Unlock"
};
- case "ask_backup_password":
- return {
- title: "Encrypted backup",
- subtitle:
- "Please enter the password to decrypt and restore this backup.",
- positiveButtonText: "Restore"
- };
case "change_account_password":
return {
title: "Change account password",
@@ -463,6 +456,17 @@ export function showPasswordDialog(
));
}
+export function showBackupPasswordDialog(
+ validate: (outputs: {
+ password?: string;
+ key?: string;
+ }) => boolean | Promise<boolean>
+) {
+ return showDialog("BackupPasswordDialog", (Dialog, perform) => (
+ <Dialog onClose={() => perform(false)} validate={validate} />
+ ));
+}
+
export function showRecoveryKeyDialog() {
return showDialog("RecoveryKeyDialog", (Dialog, perform) => (
<Dialog onDone={() => perform(true)} />
diff --git a/apps/web/src/common/index.ts b/apps/web/src/common/index.ts
index a84c8ab0b..a05c892d6 100644
--- a/apps/web/src/common/index.ts
+++ b/apps/web/src/common/index.ts
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import {
+ showBackupPasswordDialog,
showFeatureDialog,
showPasswordDialog,
showReminderDialog
@@ -150,9 +151,9 @@ export async function restoreBackupFile(backupFile: File) {
const backup = JSON.parse(await readFile(backupFile));
if (backup.data.iv && backup.data.salt) {
- await showPasswordDialog("ask_backup_password", async ({ password }) => {
- if (!password) return false;
- const error = await restoreWithProgress(backup, password);
+ await showBackupPasswordDialog(async ({ password, key }) => {
+ if (!password && !key) return false;
+ const error = await restoreWithProgress(backup, password, key);
return !error;
});
} else {
@@ -166,6 +167,7 @@ export async function restoreBackupFile(backupFile: File) {
type: "modal",
action: async (report) => {
let cachedPassword: string | undefined = undefined;
+ let cachedKey: string | undefined = undefined;
// const { read, totalFiles } = await Reader(backupFile);
const entries: ZipEntry[] = [];
let filesProcessed = 0;
@@ -183,20 +185,20 @@ export async function restoreBackupFile(backupFile: File) {
for (const entry of entries) {
const backup = JSON.parse(await entry.text());
if (backup.encrypted) {
- if (!cachedPassword) {
- const result = await showPasswordDialog(
- "ask_backup_password",
- async ({ password }) => {
- if (!password) return false;
- await db.backup?.import(backup, password);
+ if (!cachedPassword && !cachedKey) {
+ const result = await showBackupPasswordDialog(
+ async ({ password, key }) => {
+ if (!password && !key) return false;
+ await db.backup?.import(backup, password, key);
cachedPassword = password;
+ cachedKey = key;
return true;
}
);
if (!result) break;
- } else await db.backup?.import(backup, cachedPassword);
+ } else await db.backup?.import(backup, cachedPassword, cachedKey);
} else {
- await db.backup?.import(backup, null);
+ await db.backup?.import(backup);
}
report({
@@ -216,7 +218,8 @@ export async function restoreBackupFile(backupFile: File) {
async function restoreWithProgress(
backup: Record<string, unknown>,
- password?: string
+ password?: string,
+ key?: string
) {
return await TaskManager.startTask<Error | void>({
title: "Restoring backup",
@@ -243,7 +246,7 @@ async function restoreWithProgress(
);
report({ text: `Restoring...` });
- return restore(backup, password);
+ return restore(backup, password, key);
}
});
}
@@ -283,9 +286,13 @@ export async function showUpgradeReminderDialogs() {
}
}
-async function restore(backup: Record<string, unknown>, password?: string) {
+async function restore(
+ backup: Record<string, unknown>,
+ password?: string,
+ key?: string
+) {
try {
- await db.backup?.import(backup, password);
+ await db.backup?.import(backup, password, key);
showToast("success", "Backup restored!");
} catch (e) {
logger.error(e as Error, "Could not restore the backup");
diff --git a/apps/web/src/dialogs/backup-password-dialog.tsx b/apps/web/src/dialogs/backup-password-dialog.tsx
new file mode 100644
index 000000000..d2cfefa33
--- /dev/null
+++ b/apps/web/src/dialogs/backup-password-dialog.tsx
@@ -0,0 +1,115 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { useState } from "react";
+import { Perform } from "../common/dialog-controller";
+import Field from "../components/field";
+import Dialog from "../components/dialog";
+import { Box, Button } from "@theme-ui/components";
+
+export type PromptDialogProps = {
+ onClose: Perform;
+ validate: (outputs: {
+ password?: string;
+ key?: string;
+ }) => boolean | Promise<boolean>;
+};
+
+export default function BackupPasswordDialog(props: PromptDialogProps) {
+ const { onClose, validate } = props;
+
+ const [error, setError] = useState<string>();
+ const [isLoading, setIsLoading] = useState(false);
+ const [isEncryptionKey, setIsEncryptionKey] = useState(false);
+
+ return (
+ <Dialog
+ isOpen={true}
+ title={"Encrypted backup"}
+ description={
+ "Please enter the password to decrypt and restore this backup."
+ }
+ onClose={() => onClose(false)}
+ positiveButton={{
+ form: "backupPasswordForm",
+ type: "submit",
+ loading: isLoading,
+ disabled: isLoading,
+ text: "Restore"
+ }}
+ negativeButton={{ text: "Cancel", onClick: () => onClose(false) }}
+ >
+ <Box
+ id="backupPasswordForm"
+ as="form"
+ onSubmit={async (e) => {
+ e.preventDefault();
+ const formData = new FormData(e.target as HTMLFormElement);
+
+ try {
+ setIsLoading(true);
+ setError(undefined);
+
+ const key = formData.get("key") as string;
+ const password = formData.get("password") as string;
+ if (!key && !password) return;
+
+ if (await validate({ key, password })) {
+ onClose(true);
+ } else {
+ setError("Wrong password.");
+ }
+ } catch (e) {
+ setError((e as Error).message);
+ } finally {
+ setIsLoading(false);
+ }
+ }}
+ >
+ {isEncryptionKey ? (
+ <Field
+ required
+ autoFocus
+ data-test-id="dialog-key"
+ label="Encryption key"
+ type="password"
+ id="key"
+ name="key"
+ />
+ ) : (
+ <Field
+ required
+ autoFocus
+ data-test-id="dialog-password"
+ label="Password"
+ type="password"
+ autoComplete="current-password"
+ id="password"
+ name="password"
+ />
+ )}
+ </Box>
+ <Button variant="anchor" onClick={() => setIsEncryptionKey((s) => !s)}>
+ {isEncryptionKey
+ ? "Don't have encryption key? Use password."
+ : "Forgot password? Use encryption key."}
+ </Button>
+ </Dialog>
+ );
+}
diff --git a/apps/web/src/dialogs/index.ts b/apps/web/src/dialogs/index.ts
index 83e22590a..cf368e053 100644
--- a/apps/web/src/dialogs/index.ts
+++ b/apps/web/src/dialogs/index.ts
@@ -52,6 +52,9 @@ const MigrationDialog = React.lazy(() => import("./migration-dialog"));
const EmailChangeDialog = React.lazy(() => import("./email-change-dialog"));
const AddTagsDialog = React.lazy(() => import("./add-tags-dialog"));
const ThemeDetailsDialog = React.lazy(() => import("./theme-details-dialog"));
+const BackupPasswordDialog = React.lazy(
+ () => import("./backup-password-dialog")
+);
export const Dialogs = {
AddNotebookDialog,
@@ -79,5 +82,6 @@ export const Dialogs = {
EmailChangeDialog,
AddTagsDialog,
SettingsDialog,
- ThemeDetailsDialog
+ ThemeDetailsDialog,
+ BackupPasswordDialog
};