Skip to content

Commit

Permalink
request biometry on first password
Browse files Browse the repository at this point in the history
  • Loading branch information
djorkaeffalexandre committed May 5, 2020
1 parent a7f753d commit 431ca78
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
3 changes: 2 additions & 1 deletion app/i18n/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,5 +584,6 @@ export default {
Passcode_choose_force_set: 'Passcode required by admin',
Passcode_app_locked_title: 'App locked',
Passcode_app_locked_subtitle: 'Try again in {{timeLeft}} seconds',
After_seconds_set_by_admin: 'After {{seconds}} seconds (set by admin)'
After_seconds_set_by_admin: 'After {{seconds}} seconds (set by admin)',
Dont_activate: 'Don\'t activate now'
};
3 changes: 2 additions & 1 deletion app/i18n/locales/pt-BR.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,5 +523,6 @@ export default {
Passcode_choose_force_set: 'Senha foi exigida pelo admin',
Passcode_app_locked_title: 'Aplicativo bloqueado',
Passcode_app_locked_subtitle: 'Tente novamente em {{timeLeft}} segundos',
After_seconds_set_by_admin: 'Após {{seconds}} segundos (Configurado pelo adm)'
After_seconds_set_by_admin: 'Após {{seconds}} segundos (Configurado pelo adm)',
Dont_activate: 'Não ativar agora'
};
36 changes: 28 additions & 8 deletions app/utils/localAuthentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,41 @@ export const changePasscode = async({ force = false }) => {
await RNUserDefaults.set(PASSCODE_KEY, sha256(passcode));
};

export const checkHasPasscode = async({ force = true }) => {
export const biometryAuth = force => LocalAuthentication.authenticateAsync({
disableDeviceFallback: true,
cancelLabel: force ? I18n.t('Dont_activate') : I18n.t('Local_authentication_biometry_fallback'),
promptMessage: I18n.t('Local_authentication_biometry_title')
});

/*
* It'll help us to get the permission to use FaceID
* and enable/disable the biometry when user put their first passcode
*/
const checkBiometry = async(serverRecord) => {
const serversDB = database.servers;

const result = await biometryAuth(true);
await serversDB.action(async() => {
try {
await serverRecord.update((record) => {
record.biometry = !!result?.success;
});
} catch {
// Do nothing
}
});
};

export const checkHasPasscode = async({ force = true, serverRecord }) => {
const storedPasscode = await RNUserDefaults.get(PASSCODE_KEY);
if (!storedPasscode) {
await changePasscode({ force });
await checkBiometry(serverRecord);
return Promise.resolve({ newPasscode: true });
}
return Promise.resolve();
};

export const biometryAuth = () => LocalAuthentication.authenticateAsync({
disableDeviceFallback: true,
cancelLabel: I18n.t('Local_authentication_biometry_fallback'),
promptMessage: I18n.t('Local_authentication_biometry_title')
});

export const localAuthenticate = async(server) => {
const serversDB = database.servers;
const serversCollection = serversDB.collections.get('servers');
Expand All @@ -84,7 +104,7 @@ export const localAuthenticate = async(server) => {
RNBootSplash.hide();

// Check if the app has passcode
const result = await checkHasPasscode({});
const result = await checkHasPasscode({ serverRecord });

// `checkHasPasscode` results newPasscode = true if a passcode has been set
if (!result?.newPasscode) {
Expand Down
2 changes: 1 addition & 1 deletion app/views/ScreenLockConfigView.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ScreenLockConfigView extends React.Component {
const { autoLock } = this.state;
if (autoLock) {
try {
await checkHasPasscode({ force: false });
await checkHasPasscode({ force: false, serverRecord: this.serverRecord });
} catch {
this.toggleAutoLock();
}
Expand Down

0 comments on commit 431ca78

Please sign in to comment.