[FIX] Differ to Last Session Authenticated (#3667)

* [FIX] Differ to Last Session Authenticated

* Added timesync

* [FIX] Differ to Last Session Authenticated

* Added timesync

* timesync tweaks

* refactor diffLastLocalSession and saveLastLocalAuthentication

* did a race

* Update comment in app/utils/localAuthentication.ts

Co-authored-by: Diego Mello <diegolmello@gmail.com>

* refactor getServerTimeSync and when use this route

* tweak

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Reinaldo Neto 2022-02-21 13:38:49 -03:00 committed by GitHub
parent 17c63c717b
commit afb91b49db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 8 deletions

View File

@ -0,0 +1,16 @@
import RNFetchBlob from 'rn-fetch-blob';
export const getServerTimeSync = async (server: string) => {
try {
const response = await Promise.race([
RNFetchBlob.fetch('GET', `${server}/_timesync`),
new Promise<undefined>(res => setTimeout(res, 2000))
]);
if (response?.data) {
return parseInt(response.data);
}
return null;
} catch {
return null;
}
};

View File

@ -1,12 +1,13 @@
import * as LocalAuthentication from 'expo-local-authentication'; import * as LocalAuthentication from 'expo-local-authentication';
import moment from 'moment';
import RNBootSplash from 'react-native-bootsplash'; import RNBootSplash from 'react-native-bootsplash';
import AsyncStorage from '@react-native-community/async-storage'; import AsyncStorage from '@react-native-community/async-storage';
import { sha256 } from 'js-sha256'; import { sha256 } from 'js-sha256';
import moment from 'moment';
import UserPreferences from '../lib/userPreferences'; import UserPreferences from '../lib/userPreferences';
import { store } from '../lib/auxStore'; import { store } from '../lib/auxStore';
import database from '../lib/database'; import database from '../lib/database';
import { getServerTimeSync } from '../lib/rocketchat/services/getServerTimeSync';
import { import {
ATTEMPTS_KEY, ATTEMPTS_KEY,
BIOMETRY_ENABLED_KEY, BIOMETRY_ENABLED_KEY,
@ -21,16 +22,25 @@ import { TServerModel } from '../definitions/IServer';
import EventEmitter from './events'; import EventEmitter from './events';
import { isIOS } from './deviceInfo'; import { isIOS } from './deviceInfo';
export const saveLastLocalAuthenticationSession = async (server: string, serverRecord?: TServerModel): Promise<void> => { export const saveLastLocalAuthenticationSession = async (
server: string,
serverRecord?: TServerModel,
timesync?: number | null
): Promise<void> => {
if (!timesync) {
timesync = new Date().getTime();
}
const serversDB = database.servers; const serversDB = database.servers;
const serversCollection = serversDB.get('servers'); const serversCollection = serversDB.get('servers');
await serversDB.write(async () => { await serversDB.write(async () => {
try { try {
if (!serverRecord) { if (!serverRecord) {
serverRecord = (await serversCollection.find(server)) as TServerModel; serverRecord = await serversCollection.find(server);
} }
const time = timesync || 0;
await serverRecord.update(record => { await serverRecord.update(record => {
record.lastLocalAuthenticatedSession = new Date(); record.lastLocalAuthenticatedSession = new Date(time);
}); });
} catch (e) { } catch (e) {
// Do nothing // Do nothing
@ -103,6 +113,9 @@ export const localAuthenticate = async (server: string): Promise<void> => {
// if screen lock is enabled // if screen lock is enabled
if (serverRecord?.autoLock) { if (serverRecord?.autoLock) {
// Get time from server
const timesync = await getServerTimeSync(server);
// Make sure splash screen has been hidden // Make sure splash screen has been hidden
try { try {
await RNBootSplash.hide(); await RNBootSplash.hide();
@ -116,10 +129,10 @@ export const localAuthenticate = async (server: string): Promise<void> => {
// `checkHasPasscode` results newPasscode = true if a passcode has been set // `checkHasPasscode` results newPasscode = true if a passcode has been set
if (!result?.newPasscode) { if (!result?.newPasscode) {
// diff to last authenticated session // diff to last authenticated session
const diffToLastSession = moment().diff(serverRecord?.lastLocalAuthenticatedSession, 'seconds'); const diffToLastSession = moment(timesync).diff(serverRecord?.lastLocalAuthenticatedSession, 'seconds');
// if last authenticated session is older than configured auto lock time, authentication is required // if it was not possible to get `timesync` from server or the last authenticated session is older than the configured auto lock time, authentication is required
if (diffToLastSession >= serverRecord.autoLockTime!) { if (!timesync || (serverRecord?.autoLockTime && diffToLastSession >= serverRecord.autoLockTime)) {
// set isLocalAuthenticated to false // set isLocalAuthenticated to false
store.dispatch(setLocalAuthenticated(false)); store.dispatch(setLocalAuthenticated(false));
@ -141,7 +154,7 @@ export const localAuthenticate = async (server: string): Promise<void> => {
} }
await resetAttempts(); await resetAttempts();
await saveLastLocalAuthenticationSession(server, serverRecord); await saveLastLocalAuthenticationSession(server, serverRecord, timesync);
} }
}; };