Rocket.Chat.ReactNative/app/utils/localAuthentication.js

94 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-04-14 13:07:13 +00:00
import * as LocalAuthentication from 'expo-local-authentication';
2020-04-14 14:07:51 +00:00
import moment from 'moment';
2020-04-14 13:07:13 +00:00
import database from '../lib/database';
2020-04-16 14:30:00 +00:00
import { isIOS } from './deviceInfo';
2020-04-17 19:11:06 +00:00
import EventEmitter from './events';
import { LOCAL_AUTHENTICATE } from '../views/ScreenLockedView';
2020-04-20 13:09:19 +00:00
import RNBootSplash from 'react-native-bootsplash';
2020-04-14 13:07:13 +00:00
2020-04-17 19:11:06 +00:00
export const saveLastLocalAuthenticationSession = async(server, serverRecord) => {
2020-04-14 14:07:51 +00:00
const serversDB = database.servers;
const serversCollection = serversDB.collections.get('servers');
await serversDB.action(async() => {
try {
2020-04-17 19:11:06 +00:00
if (!serverRecord) {
serverRecord = await serversCollection.find(server);
}
2020-04-14 14:07:51 +00:00
console.log('saveLastLocalAuthenticationSession -> serverRecord', serverRecord);
await serverRecord.update((record) => {
record.lastLocalAuthenticatedSession = new Date();
});
} catch (e) {
// Do nothing
}
});
};
2020-04-17 19:11:06 +00:00
export const localPasscode = () => new Promise((resolve, reject) => {
EventEmitter.emit(LOCAL_AUTHENTICATE, {
cancel: () => reject(),
submit: () => resolve()
});
});
2020-04-14 14:07:51 +00:00
export const localAuthenticate = async(server) => {
2020-04-14 13:07:13 +00:00
const serversDB = database.servers;
const serversCollection = serversDB.collections.get('servers');
let serverRecord;
try {
serverRecord = await serversCollection.find(server);
console.log('localAuthenticate -> serverRecord', serverRecord);
} catch (error) {
return Promise.reject();
}
2020-04-17 19:11:06 +00:00
// if screen lock is enabled
if (serverRecord?.autoLock) {
2020-04-20 13:09:19 +00:00
2020-04-17 19:11:06 +00:00
// diff to last authenticated session
2020-04-16 13:52:56 +00:00
const diffToLastSession = moment().diff(serverRecord?.lastLocalAuthenticatedSession, 'seconds');
2020-04-16 14:30:00 +00:00
console.log('localAuthenticate -> diffToLastSession', diffToLastSession);
2020-04-17 19:11:06 +00:00
// if last authenticated session is older than configured auto lock time, authentication is required
2020-04-16 13:52:56 +00:00
if (diffToLastSession >= serverRecord?.autoLockTime) {
2020-04-20 13:09:19 +00:00
// Make sure splash screen has been hidden
RNBootSplash.hide();
2020-04-17 19:11:06 +00:00
const isEnrolled = await LocalAuthentication.isEnrolledAsync();
const isSupported = await LocalAuthentication.supportedAuthenticationTypesAsync();
// if biometry is enabled and enrolled on OS
if (isEnrolled && isSupported) {
// opens biometry prompt
const authResult = await LocalAuthentication.authenticateAsync({ disableDeviceFallback: true });
if (authResult?.success) {
await saveLastLocalAuthenticationSession(server, serverRecord);
} else {
await localPasscode();
}
} else {
await localPasscode();
2020-04-16 13:52:56 +00:00
}
}
}
2020-04-14 13:07:13 +00:00
};
2020-04-16 14:30:00 +00:00
export const supportedAuthenticationLabel = async() => {
const supported = await LocalAuthentication.supportedAuthenticationTypesAsync();
console.log('supportedAuthenticationLabel -> supported', supported);
const enrolled = await LocalAuthentication.isEnrolledAsync();
console.log('supportedAuthenticationLabel -> enrolled', enrolled);
if (supported.includes(LocalAuthentication.AuthenticationType.FACIAL_RECOGNITION)) {
return isIOS ? 'FaceID' : 'facial recognition';
}
if (supported.includes(LocalAuthentication.AuthenticationType.FINGERPRINT)) {
return isIOS ? 'TouchID' : 'fingerprint';
}
return null;
};