Skip to content

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

mobile: fix app lock issues

Ammar Ahmed5b57614bfe8e
diff --git a/apps/mobile/app/common/database/encryption.js b/apps/mobile/app/common/database/encryption.js
index 09963cf45..794701e60 100644
--- a/apps/mobile/app/common/database/encryption.js
+++ b/apps/mobile/app/common/database/encryption.js
@@ -43,9 +43,8 @@ const IOS_KEYCHAIN_ACCESS_GROUP = "group.org.streetwriters.notesnook";
const IOS_KEYCHAIN_SERVICE_NAME = "org.streetwriters.notesnook";
const KEYCHAIN_SERVER_DBKEY = "notesnook:db";
-const NOTESNOOK_APPLOCK_KEY_SALT = "3dqclWbOYllfk9kk";
-const NOTESNOOK_DB_KEY_SALT = "2rcgSprDmRvZ1AAa";
-const NOTESNOOK_USER_KEY_SALT = "7qO4qeoM6PbsAJ0Q";
+const NOTESNOOK_APPLOCK_KEY_SALT = "kBwr1Kre86ebOZ8ThLu2OA";
+const NOTESNOOK_DB_KEY_SALT = "SNuzOcEK3amoqL0WvPeKqw";
const DB_KEY_CIPHER = "databaseKeyCipher";
const USER_KEY_CIPHER = "userKeyCipher";
@@ -117,9 +116,7 @@ export async function setAppLockVerificationCipher(appLockPassword) {
NOTESNOOK_APPLOCK_KEY_SALT
);
const encrypted = await encrypt(appLockCredentials, generatePassword());
-
CipherStorage.setMap(APPLOCK_CIPHER, encrypted);
-
DatabaseLogger.info("setAppLockVerificationCipher");
} catch (e) {
DatabaseLogger.error(e);
@@ -135,10 +132,8 @@ export async function validateAppLockPassword(appLockPassword) {
try {
const appLockCipher = CipherStorage.getMap(APPLOCK_CIPHER);
if (!appLockCipher) return true;
- const decrypted = await decrypt(
- await Sodium.deriveKey(appLockPassword, NOTESNOOK_APPLOCK_KEY_SALT),
- appLockCipher
- );
+ const key = await Sodium.deriveKey(appLockPassword, appLockCipher.salt);
+ const decrypted = await decrypt(key, appLockCipher);
DatabaseLogger.info(
`validateAppLockPassword: ${typeof decrypted === "string"}`
@@ -213,8 +208,7 @@ export async function getDatabaseKey(appLockPassword) {
if (userKeyCredentials) {
const userKeyCipher = await encrypt(
{
- key: DB_KEY,
- salt: NOTESNOOK_USER_KEY_SALT
+ key: DB_KEY
},
userKeyCredentials.password
);
@@ -238,8 +232,7 @@ export async function deriveCryptoKey(data) {
let credentials = await Sodium.deriveKey(data.password, data.salt);
const userKeyCipher = await encrypt(
{
- key: await getDatabaseKey(),
- salt: NOTESNOOK_USER_KEY_SALT
+ key: await getDatabaseKey()
},
credentials.key
);
diff --git a/apps/mobile/app/components/app-lock-overlay/index.tsx b/apps/mobile/app/components/app-lock-overlay/index.tsx
index 13074814b..e4d66986e 100644
--- a/apps/mobile/app/components/app-lock-overlay/index.tsx
+++ b/apps/mobile/app/components/app-lock-overlay/index.tsx
@@ -34,11 +34,13 @@ import {
import { MMKV } from "../../common/database/mmkv";
import { useAppState } from "../../hooks/use-app-state";
import BiometricService from "../../services/biometrics";
+import { ToastManager } from "../../services/event-manager";
import SettingsService from "../../services/settings";
import { useSettingStore } from "../../stores/use-setting-store";
import { useUserStore } from "../../stores/use-user-store";
import { NotesnookModule } from "../../utils/notesnook-module";
import { SIZE } from "../../utils/size";
+import { Toast } from "../toast";
import { Button } from "../ui/button";
import { IconButton } from "../ui/icon-button";
import Input from "../ui/input";
@@ -148,6 +150,12 @@ const AppLockedOverlay = () => {
lockApp(false);
enabled(false);
password.current = undefined;
+ } else {
+ ToastManager.show({
+ heading: `Invalid ${keyboardType === "numeric" ? "pin" : "password"}`,
+ type: "error",
+ context: "local"
+ });
}
} catch (e) {
console.error(e);
@@ -205,6 +213,7 @@ const AppLockedOverlay = () => {
justifyContent: "center"
}}
>
+ <Toast context="local" />
<View
style={{
flex: 1,
diff --git a/apps/mobile/app/components/dialog/functions.ts b/apps/mobile/app/components/dialog/functions.ts
index 9bc39fe92..486ae3c12 100644
--- a/apps/mobile/app/components/dialog/functions.ts
+++ b/apps/mobile/app/components/dialog/functions.ts
@@ -44,6 +44,7 @@ type DialogInfo = {
// eslint-disable-next-line @typescript-eslint/ban-types
context: "global" | "local" | (string & {});
secureTextEntry?: boolean;
+ keyboardType?: string;
};
export function presentDialog(data: Partial<DialogInfo>): void {
diff --git a/apps/mobile/app/components/dialog/index.js b/apps/mobile/app/components/dialog/index.js
index 4ed2dd4fd..3ad8d84a7 100644
--- a/apps/mobile/app/components/dialog/index.js
+++ b/apps/mobile/app/components/dialog/index.js
@@ -174,6 +174,7 @@ export const Dialog = ({ context = "global" }) => {
onSubmit={onPressPositive}
returnKeyLabel="Done"
returnKeyType="done"
+ keyboardType={dialogInfo.keyboardType || "default"}
placeholder={dialogInfo.inputPlaceholder}
/>
</View>
diff --git a/apps/mobile/app/components/dialogs/applock-password/index.tsx b/apps/mobile/app/components/dialogs/applock-password/index.tsx
index 3583e32db..0f2fa3c73 100644
--- a/apps/mobile/app/components/dialogs/applock-password/index.tsx
+++ b/apps/mobile/app/components/dialogs/applock-password/index.tsx
@@ -291,7 +291,8 @@ export const AppLockPassword = () => {
);
return;
}
- await setAppLockVerificationCipher(values.current.password);
+ const password = values.current.password;
+ setAppLockVerificationCipher(password);
SettingsService.setProperty("appLockHasPasswordSecurity", true);
} else if (mode === "change") {
if (
@@ -319,7 +320,6 @@ export const AppLockPassword = () => {
);
return;
}
-
const isCurrentPasswordCorrect = await validateAppLockPassword(
values.current.currentPassword
);
@@ -335,8 +335,10 @@ export const AppLockPassword = () => {
return;
}
+ const password = values.current.password;
+ await clearAppLockVerificationCipher();
SettingsService.setProperty("appLockHasPasswordSecurity", true);
- await setAppLockVerificationCipher(values.current.password);
+ await setAppLockVerificationCipher(password);
} else if (mode === "remove") {
if (!values.current.password) {
ToastManager.error(
@@ -354,7 +356,11 @@ export const AppLockPassword = () => {
if (!isCurrentPasswordCorrect) {
ToastManager.error(
new Error(
- `${keyboardType === "pin" ? "Pin" : "Password"} incorrect`
+ accountPass
+ ? "Account password incorrect"
+ : `${
+ keyboardType === "pin" ? "Pin" : "Password"
+ } incorrect`
),
undefined,
"local"
diff --git a/apps/mobile/app/screens/settings/functions.js b/apps/mobile/app/screens/settings/functions.js
index c3b744be3..786227864 100644
--- a/apps/mobile/app/screens/settings/functions.js
+++ b/apps/mobile/app/screens/settings/functions.js
@@ -90,13 +90,26 @@ export async function verifyUserWithApplock() {
positiveText: "Verify",
secureTextEntry: true,
negativeText: "Cancel",
+ keyboardType: keyboardType,
positivePress: async (value) => {
try {
const verified = await validateAppLockPassword(value);
+ if (!verified) {
+ ToastManager.show({
+ heading: `Invalid ${
+ keyboardType === "numeric" ? "pin" : "password"
+ }`,
+ type: "error",
+ context: "local"
+ });
+ return false;
+ }
resolve(verified);
} catch (e) {
resolve(false);
+ return false;
}
+ return true;
}
});
} else {
diff --git a/apps/mobile/app/screens/settings/settings-data.tsx b/apps/mobile/app/screens/settings/settings-data.tsx
index 0568570e1..45667ec47 100644
--- a/apps/mobile/app/screens/settings/settings-data.tsx
+++ b/apps/mobile/app/screens/settings/settings-data.tsx
@@ -976,7 +976,7 @@ export const settingsGroups: SettingSection[] = [
SettingsService.getProperty("applockKeyboardType") === "numeric"
? "pin"
: "password"
- }, app lock will fallback to using account password to unlock the app`,
+ }, app lock will be disabled if no other security method is enabled.`,
hidden: () => {
return !SettingsService.getProperty("appLockHasPasswordSecurity");
},