Skip to content

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

fix fingerprint authentication

ammarahm-ed3012beedac82
diff --git a/apps/mobile/android/app/src/main/AndroidManifest.xml b/apps/mobile/android/app/src/main/AndroidManifest.xml
index bdfc860d8..0f1667af4 100644
--- a/apps/mobile/android/app/src/main/AndroidManifest.xml
+++ b/apps/mobile/android/app/src/main/AndroidManifest.xml
@@ -7,7 +7,9 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
-
+ <uses-permission android:name="android.permission.USE_FINGERPRINT" />
+ <uses-permission android:name="android.permission.USE_BIOMETRIC" />
+
<application
android:name=".MainApplication"
android:allowBackup="false"
diff --git a/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/MainApplication.java b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/MainApplication.java
index 92aff6418..13304323e 100644
--- a/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/MainApplication.java
+++ b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/MainApplication.java
@@ -32,7 +32,6 @@ import com.vinzscam.reactnativefileviewer.RNFileViewerModule;
import cl.json.RNShareModule;
import io.github.elyx0.reactnativedocumentpicker.DocumentPickerModule;
import px.tooltips.RNTooltipsModule;
-import com.oblador.keychain.KeychainModuleBuilder;
public class MainApplication extends MultiDexApplication implements ReactApplication {
@@ -47,7 +46,7 @@ public class MainApplication extends MultiDexApplication implements ReactApplica
protected List<ReactPackage> getPackages() {
List<ReactPackage> packages = new PackageList(this).getPackages();
- packages.add(new KeychainPackage(new KeychainModuleBuilder().withoutWarmUp()));
+ //packages.add(new KeychainPackage(new KeychainModuleBuilder().withoutWarmUp()));
packages.add(new TurboReactPackage() {
@Override
diff --git a/apps/mobile/react-native.config.js b/apps/mobile/react-native.config.js
index 3d9e7859f..1e1b9037b 100644
--- a/apps/mobile/react-native.config.js
+++ b/apps/mobile/react-native.config.js
@@ -5,10 +5,10 @@ module.exports = {
ios: null,
},
},
- 'react-native-keychain': {
+ /* 'react-native-keychain': {
platforms: {
android: null,
},
- },
+ }, */
},
};
diff --git a/apps/mobile/src/components/VaultDialog/index.js b/apps/mobile/src/components/VaultDialog/index.js
index 5a107ef21..8b21f2a7c 100644
--- a/apps/mobile/src/components/VaultDialog/index.js
+++ b/apps/mobile/src/components/VaultDialog/index.js
@@ -5,6 +5,7 @@ import Share from 'react-native-share';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {notesnook} from '../../../e2e/test.ids';
import {Actions} from '../../provider/Actions';
+import BiometricService from '../../services/BiometricService';
import {DDS} from '../../services/DeviceDetection';
import {
eSendEvent,
@@ -85,18 +86,14 @@ export class VaultDialog extends Component {
if (!Keychain) {
Keychain = require('react-native-keychain');
}
- let biometry = await Keychain.getSupportedBiometryType();
- console.log(biometry, 'BIOMETRY');
+ let biometry = await BiometricService.isBiometryAvailable();
let available = false;
- let fingerprint = await Keychain.hasInternetCredentials('nn_vault');
+ let fingerprint = await BiometricService.hasInternetCredentials('nn_vault');
- if (
- biometry === Keychain.BIOMETRY_TYPE.FINGERPRINT ||
- biometry === Keychain.BIOMETRY_TYPE.TOUCH_ID
- ) {
+ if (biometry) {
available = true;
}
-
+ console.log('fingerprint', fingerprint, 'biometry', biometry);
this.setState({
note: data.item,
novault: data.novault,
@@ -137,7 +134,12 @@ export class VaultDialog extends Component {
);
return;
}
- updateEvent({type: Actions.NOTES});
+ Navigation.setRoutesToUpdate([
+ Navigation.routeNames.Notes,
+ Navigation.routeNames.Favorites,
+ Navigation.routeNames.NotesPage,
+ Navigation.routeNames.Notebook,
+ ]);
this.password = null;
this.confirmPassword = null;
@@ -284,7 +286,8 @@ export class VaultDialog extends Component {
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notes,
Navigation.routeNames.Favorites,
- Navigation.routeNames.refreshNotesPage,
+ Navigation.routeNames.NotesPage,
+ Navigation.routeNames.Notebook,
]);
ToastEvent.show('Note deleted', 'success');
@@ -297,29 +300,12 @@ export class VaultDialog extends Component {
loading: true,
},
async () => {
- if (!Keychain) {
- Keychain = require('react-native-keychain');
- }
- await sleep(20);
- await Keychain.setInternetCredentials(
- 'nn_vault',
- 'nn_vault',
- password,
- {
- accessControl:
- Keychain.ACCESS_CONTROL.BIOMETRY_ANY_OR_DEVICE_PASSCODE,
- authenticationPrompt: {cancel: null},
- accessible:
- Keychain.AUTHENTICATION_TYPE.DEVICE_PASSCODE_OR_BIOMETRICS,
- storage: kc.STORAGE_TYPE.AES,
- rules: 'none',
- },
- );
+ await BiometricService.storeCredentials(password);
this.setState({
loading: false,
});
eSendEvent('vaultUpdated');
- ToastEvent.show('Fingerprint access enabled!', 'success');
+ ToastEvent.show('Biometric unlocking enabled!', 'success');
this.close();
},
);
@@ -330,13 +316,17 @@ export class VaultDialog extends Component {
async _createVault() {
await db.vault.create(this.password);
+
if (this.state.biometricUnlock) {
- await this._enrollFingerprint();
+ await this._enrollFingerprint(this.password);
}
if (this.state.note && this.state.note.id && !this.state.note.locked) {
await db.vault.add(this.state.note.id);
- this.close();
+ this.setState({
+ loading: false,
+ });
ToastEvent.show('Note added to vault', 'success', 'local');
+ this.close();
} else {
eSendEvent('vaultUpdated');
this.close();
@@ -351,8 +341,11 @@ export class VaultDialog extends Component {
id: this.state.note.id,
forced: true,
});
- updateEvent({type: Actions.NOTES});
- eSendEvent(refreshNotesPage);
+ Navigation.setRoutesToUpdate([
+ Navigation.routeNames.Notes,
+ Navigation.routeNames.Favorites,
+ Navigation.routeNames.NotesPage,
+ ]);
this.close();
})
.catch((e) => {
@@ -404,21 +397,9 @@ export class VaultDialog extends Component {
_revokeFingerprintAccess = async () => {
try {
- if (!Keychain) {
- Keychain = require('react-native-keychain');
- }
- await Keychain.resetInternetCredentials('nn_vault', {
- accessControl: Keychain.ACCESS_CONTROL.BIOMETRY_ANY_OR_DEVICE_PASSCODE,
- authenticationType:
- Keychain.AUTHENTICATION_TYPE.DEVICE_PASSCODE_OR_BIOMETRICS,
- authenticationPrompt: {
- cancel: null,
- },
- storage: kc.STORAGE_TYPE.AES,
- rules: 'none',
- });
+ await BiometricService.resetCredentials();
eSendEvent('vaultUpdated');
- ToastEvent.show('Fingerprint access revoked!', 'success');
+ ToastEvent.show('Biometrics access revoked!', 'success');
} catch (e) {
ToastEvent.show(e.message, 'error', 'local');
}
@@ -426,29 +407,18 @@ export class VaultDialog extends Component {
_onPressFingerprintAuth = async () => {
try {
- if (!Keychain) {
- Keychain = require('react-native-keychain');
- }
- let credentials = await Keychain.getInternetCredentials('nn_vault', {
- accessControl: Keychain.ACCESS_CONTROL.BIOMETRY_ANY_OR_DEVICE_PASSCODE,
- authenticationType:
- Keychain.AUTHENTICATION_TYPE.DEVICE_PASSCODE_OR_BIOMETRICS,
- authenticationPrompt: {
- cancel: null,
- },
- storage: kc.STORAGE_TYPE.AES,
- rules: 'none',
- });
+ let credentials = await BiometricService.getCredentials();
+
if (credentials?.password) {
this.password = credentials.password;
this.onPress();
+ } else {
+ this.setState({
+ visible: true,
+ });
+ ToastEvent.show('Biometrics Authentication Failed', 'error', 'local');
}
- } catch (e) {
- this.setState({
- visible: true,
- });
- ToastEvent.show('Fingerprint Authentication Canceled', 'error', 'local');
- }
+ } catch (e) {}
};
render() {
@@ -630,11 +600,13 @@ export class VaultDialog extends Component {
</View>
) : null}
- {this.state.biometricUnlock && !this.state.isBiometryEnrolled && (
- <Paragraph>
- Unlock with password once to enable fingerprint access.
- </Paragraph>
- )}
+ {this.state.biometricUnlock &&
+ !this.state.isBiometryEnrolled &&
+ novault && (
+ <Paragraph>
+ Unlock with password once to enable fingerprint access.
+ </Paragraph>
+ )}
{this.state.isBiometryAvailable &&
!this.state.fingerprintAccess &&
diff --git a/apps/mobile/src/services/BiometricService.js b/apps/mobile/src/services/BiometricService.js
new file mode 100644
index 000000000..21d844db4
--- /dev/null
+++ b/apps/mobile/src/services/BiometricService.js
@@ -0,0 +1,92 @@
+import FingerprintScanner from 'react-native-fingerprint-scanner';
+import Storage from '../utils/storage';
+import {ToastEvent} from './EventManager';
+import * as Keychain from 'react-native-keychain';
+import {Platform} from 'react-native';
+
+const CRYPT_CONFIG = Platform.select({
+ ios: {
+ accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED_THIS_DEVICE_ONLY,
+ },
+ android: {},
+});
+
+async function isBiometryAvailable() {
+ try {
+
+ return await FingerprintScanner.isSensorAvailable();
+ } catch (e) {
+ ToastEvent.show(e.message, 'error');
+ return false;
+ }
+}
+
+async function enableFingerprintAuth() {
+ if (!isBiometryAvailable()) return;
+ await Storage.write('fingerprintAuthEnabled', 'enabled');
+}
+
+async function isFingerprintAuthEnabled() {
+ return await Storage.read('fingerprintAuthEnabled', 'enabled');
+}
+
+async function storeCredentials(password) {
+ await Keychain.setInternetCredentials(
+ 'nn_vault',
+ 'notesnookvault',
+ password,
+ CRYPT_CONFIG,
+ );
+}
+
+async function resetCredentials() {
+ return await Keychain.resetInternetCredentials('nn_vault');
+}
+
+async function hasInternetCredentials() {
+ return await Keychain.hasInternetCredentials('nn_vault');
+}
+
+async function getCredentials() {
+ try {
+ await FingerprintScanner.authenticate(
+ Platform.select({
+ ios: {
+ fallbackEnabled: true,
+ description:
+ 'Biometrics are required to unlock note.',
+ },
+ android: {
+ title: 'Unlock Note',
+ description: 'Biometrics are required to unlock note.',
+ deviceCredentialAllowed: true,
+ },
+ }),
+ );
+ console.log('allowed');
+ FingerprintScanner.release();
+ return await Keychain.getInternetCredentials('nn_vault', CRYPT_CONFIG);
+ } catch (e) {
+ console.log('failed');
+ FingerprintScanner.release();
+ if (e.name === 'DeviceLocked') {
+ ToastEvent.show(
+ 'Authentication failed. Wait 30 seconds to try again.',
+ 'error',
+ );
+ } else {
+ ToastEvent.show('Authentication failed. Tap to try again.', 'error');
+ }
+ return null;
+ }
+}
+
+export default {
+ isBiometryAvailable,
+ enableFingerprintAuth,
+ isFingerprintAuthEnabled,
+ resetCredentials,
+ getCredentials,
+ storeCredentials,
+ hasInternetCredentials
+};