From cf71acd75386cceb190c729141da46ed77c4071a Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Mon, 27 Apr 2020 17:08:47 -0300 Subject: [PATCH] Save passcode with promise to prevent empty passcodes and immediately lock --- app/i18n/locales/en.js | 1 + app/i18n/locales/pt-BR.js | 1 + app/views/ChangePasscodeView.js | 18 ++++++------ app/views/ScreenLockConfigView.js | 48 +++++++++++++++++++------------ 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/app/i18n/locales/en.js b/app/i18n/locales/en.js index 16a90f76d..f13fc3602 100644 --- a/app/i18n/locales/en.js +++ b/app/i18n/locales/en.js @@ -568,6 +568,7 @@ export default { Local_authentication_facial_recognition: 'facial recognition', Local_authentication_fingerprint: 'fingerprint', Local_authentication_unlock_with_label: 'Unlock with {{label}}', + Local_authentication_auto_lock_0: 'Immediately', Local_authentication_auto_lock_60: 'After 1 minute', Local_authentication_auto_lock_300: 'After 5 minutes', Local_authentication_auto_lock_900: 'After 15 minutes', diff --git a/app/i18n/locales/pt-BR.js b/app/i18n/locales/pt-BR.js index 21eea6e0e..89f3334bf 100644 --- a/app/i18n/locales/pt-BR.js +++ b/app/i18n/locales/pt-BR.js @@ -508,6 +508,7 @@ export default { Local_authentication_facial_recognition: 'reconhecimento facial', Local_authentication_fingerprint: 'impressão digital', Local_authentication_unlock_with_label: 'Desbloquear com {{label}}', + Local_authentication_auto_lock_0: 'Imediatamente', Local_authentication_auto_lock_60: 'Após 1 minuto', Local_authentication_auto_lock_300: 'Após 5 minutos', Local_authentication_auto_lock_900: 'Após 15 minutos', diff --git a/app/views/ChangePasscodeView.js b/app/views/ChangePasscodeView.js index 2e0cd7eb9..1c4960bbe 100644 --- a/app/views/ChangePasscodeView.js +++ b/app/views/ChangePasscodeView.js @@ -1,23 +1,21 @@ import React, { useEffect } from 'react'; import PropTypes from 'prop-types'; import { SafeAreaView } from 'react-navigation'; -import RNUserDefaults from 'rn-user-defaults'; import Orientation from 'react-native-orientation-locker'; -import { sha256 } from 'js-sha256'; import { themedHeader } from '../utils/navigation'; import { withTheme } from '../theme'; import { themes } from '../constants/colors'; import sharedStyles from './Styles'; -import { PASSCODE_KEY } from '../constants/localAuthentication'; import { isTablet } from '../utils/deviceInfo'; import { TYPE } from '../containers/Passcode/constants'; import { PasscodeChoose } from '../containers/Passcode'; -const ScreenLockConfigView = React.memo(({ navigation, theme }) => { - const savePasscode = async(passcode) => { - await RNUserDefaults.set(PASSCODE_KEY, sha256(passcode)); +const ChangePasscodeView = React.memo(({ navigation, theme }) => { + const getPasscode = (passcode) => { navigation.pop(); + const get = navigation.getParam('getPasscode', () => {}); + get(passcode); }; useEffect(() => { @@ -35,12 +33,12 @@ const ScreenLockConfigView = React.memo(({ navigation, theme }) => { - + ); }); -ScreenLockConfigView.navigationOptions = ({ screenProps, navigation }) => { +ChangePasscodeView.navigationOptions = ({ screenProps, navigation }) => { const forceSetPasscode = navigation.getParam('forceSetPasscode', false); if (forceSetPasscode) { return { @@ -54,9 +52,9 @@ ScreenLockConfigView.navigationOptions = ({ screenProps, navigation }) => { }; }; -ScreenLockConfigView.propTypes = { +ChangePasscodeView.propTypes = { navigation: PropTypes.object, theme: PropTypes.string }; -export default withTheme(ScreenLockConfigView); +export default withTheme(ChangePasscodeView); diff --git a/app/views/ScreenLockConfigView.js b/app/views/ScreenLockConfigView.js index 3d0d9b494..b8df05758 100644 --- a/app/views/ScreenLockConfigView.js +++ b/app/views/ScreenLockConfigView.js @@ -6,6 +6,7 @@ import { import { SafeAreaView } from 'react-navigation'; import { connect } from 'react-redux'; import RNUserDefaults from 'rn-user-defaults'; +import { sha256 } from 'js-sha256'; import I18n from '../i18n'; import { themedHeader } from '../utils/navigation'; @@ -31,6 +32,9 @@ const styles = StyleSheet.create({ } }); +const DEFAULT_AUTO_LOCK = 1800; +const DEFAULT_BIOMETRY = true; + class ScreenLockConfigView extends React.Component { static navigationOptions = ({ screenProps }) => ({ title: I18n.t('Screen_lock'), @@ -44,6 +48,10 @@ class ScreenLockConfigView extends React.Component { } defaultAutoLockOptions = [ + { + title: I18n.t('Local_authentication_auto_lock_0'), + value: 0 + }, { title: I18n.t('Local_authentication_auto_lock_60'), value: 60 @@ -85,8 +93,8 @@ class ScreenLockConfigView extends React.Component { this.serverRecord = await serversCollection.find(server); this.setState({ autoLock: this.serverRecord?.autoLock, - autoLockTime: this.serverRecord?.autoLockTime || 1800, - biometry: this.serverRecord.biometry === null ? true : this.serverRecord.biometry + autoLockTime: this.serverRecord?.autoLockTime === null ? DEFAULT_AUTO_LOCK : this.serverRecord?.autoLockTime, + biometry: this.serverRecord.biometry === null ? DEFAULT_BIOMETRY : this.serverRecord.biometry }); } catch (error) { // Do nothing @@ -102,27 +110,36 @@ class ScreenLockConfigView extends React.Component { await serversDB.action(async() => { await this.serverRecord?.update((record) => { record.autoLock = autoLock; - record.autoLockTime = autoLockTime; - record.biometry = biometry === null ? true : biometry; + record.autoLockTime = autoLockTime === null ? DEFAULT_AUTO_LOCK : autoLockTime; + record.biometry = biometry === null ? DEFAULT_BIOMETRY : biometry; }); }); } - setPasscode = async() => { - const { autoLock } = this.state; + getPasscode = ({ force = false }) => new Promise((resolve) => { const { navigation } = this.props; - if (autoLock) { - const storedPasscode = await RNUserDefaults.get(PASSCODE_KEY); - if (!storedPasscode) { - navigation.navigate('ChangePasscodeView', { forceSetPasscode: true }); - } + navigation.navigate('ChangePasscodeView', { forceSetPasscode: force, getPasscode: passcode => resolve(passcode) }); + }); + + changePasscode = async({ force }) => { + const passcode = await this.getPasscode({ force }); + await RNUserDefaults.set(PASSCODE_KEY, sha256(passcode)); + } + + checkHasPasscode = async() => { + const storedPasscode = await RNUserDefaults.get(PASSCODE_KEY); + if (!storedPasscode) { + await this.changePasscode({ force: true }); } } toggleAutoLock = () => { - this.setState(({ autoLock }) => ({ autoLock: !autoLock }), () => { + this.setState(({ autoLock }) => ({ autoLock: !autoLock }), async() => { + const { autoLock } = this.state; + if (autoLock) { + await this.checkHasPasscode(); + } this.save(); - this.setPasscode(); }); } @@ -139,11 +156,6 @@ class ScreenLockConfigView extends React.Component { this.setState({ autoLockTime }, () => this.save()); } - changePasscode = () => { - const { navigation } = this.props; - navigation.navigate('ChangePasscodeView'); - } - renderSeparator = () => { const { theme } = this.props; return ;