UNPKG

@sbaiahmed1/react-native-biometrics

Version:

React Native biometric authentication library for iOS and Android. Easy integration of Face ID, Touch ID, and Fingerprint authentication with TypeScript support. Compatible with new architecture (TurboModules) and Expo. Secure mobile login solution.

407 lines (390 loc) 13.5 kB
"use strict"; import ReactNativeBiometrics from "./NativeReactNativeBiometrics.js"; import { logger, LogLevel } from "./logger.js"; import { Platform } from 'react-native'; import { BiometricStrength } from "./types.js"; export function isSensorAvailable(options) { logger.debug('Checking sensor availability', 'isSensorAvailable'); if (Platform.OS === 'android') { const biometricStrength = options?.biometricStrength || BiometricStrength.Strong; return ReactNativeBiometrics.isSensorAvailable(biometricStrength).then(result => { logger.info('Sensor availability check completed', 'isSensorAvailable', { available: result.available, biometryType: result.biometryType }); return result; }).catch(error => { logger.error('Sensor availability check failed', 'isSensorAvailable', error); throw error; }); } // For iOS, we still call without parameters as iOS doesn't support biometric strength return ReactNativeBiometrics.isSensorAvailable().then(result => { logger.info('Sensor availability check completed', 'isSensorAvailable', { available: result.available, biometryType: result.biometryType }); return result; }).catch(error => { logger.error('Sensor availability check failed', 'isSensorAvailable', error); throw error; }); } export function simplePrompt(promptMessage, options) { logger.debug('Starting simple biometric prompt', 'simplePrompt', { promptMessage, biometricStrength: options?.biometricStrength }); if (Platform.OS === 'android') { return ReactNativeBiometrics.simplePrompt(promptMessage, options?.biometricStrength).then(result => { logger.info('Simple prompt completed', 'simplePrompt', { success: result }); return result; }).catch(error => { logger.error('Simple prompt failed', 'simplePrompt', error, { promptMessage, biometricStrength: options?.biometricStrength }); throw error; }); } // iOS and other platforms ignore biometricStrength and use default behavior return ReactNativeBiometrics.simplePrompt(promptMessage).then(result => { logger.info('Simple prompt completed', 'simplePrompt', { success: result }); return result; }).catch(error => { logger.error('Simple prompt failed', 'simplePrompt', error, { promptMessage }); throw error; }); } export { BiometricStrength } from "./types.js"; export function authenticateWithOptions(options) { logger.debug('Starting authentication with options', 'authenticateWithOptions'); if (Platform.OS === 'android' && options.biometricStrength) { return ReactNativeBiometrics.authenticateWithOptions(options).then(result => { logger.info('Authentication with options completed', 'authenticateWithOptions', { success: result.success }); return result; }).catch(error => { logger.error('Authentication with options failed', 'authenticateWithOptions', error); throw error; }); } // For iOS or Android without biometricStrength, remove biometricStrength from options // eslint-disable-next-line @typescript-eslint/no-unused-vars const { biometricStrength: _, ...cleanOptions } = options; return ReactNativeBiometrics.authenticateWithOptions(cleanOptions).then(result => { logger.info('Authentication with options completed', 'authenticateWithOptions', { success: result.success }); return result; }).catch(error => { logger.error('Authentication with options failed', 'authenticateWithOptions', error); throw error; }); } export function createKeys(keyAlias, keyType, biometricStrength) { logger.debug('Creating biometric keys', 'createKeys', { keyAlias, keyType, biometricStrength }); return ReactNativeBiometrics.createKeys(keyAlias, keyType, biometricStrength).then(result => { logger.info('Keys created successfully', 'createKeys', { keyAlias, keyType, biometricStrength, publicKeyLength: result.publicKey?.length }); return result; }).catch(error => { logger.error('Key creation failed', 'createKeys', error, { keyAlias, keyType, biometricStrength }); throw error; }); } export function deleteKeys(keyAlias) { logger.debug('Deleting biometric keys', 'deleteKeys', { keyAlias }); return ReactNativeBiometrics.deleteKeys(keyAlias).then(result => { logger.info('Keys deletion completed', 'deleteKeys', { keyAlias, success: result.success }); return result; }).catch(error => { logger.error('Key deletion failed', 'deleteKeys', error, { keyAlias }); throw error; }); } export function validateKeyIntegrity(keyAlias) { logger.debug('Validating key integrity', 'validateKeyIntegrity', { keyAlias }); return ReactNativeBiometrics.validateKeyIntegrity(keyAlias).then(result => { logger.info('Key integrity validation completed', 'validateKeyIntegrity', { keyAlias, valid: result.valid, keyExists: result.keyExists, integrityChecks: result.integrityChecks }); return result; }).catch(error => { logger.error('Key integrity validation failed', 'validateKeyIntegrity', error, { keyAlias }); throw error; }); } export function verifyKeySignature(keyAlias = '', data, promptTitle, promptSubtitle, cancelButtonText) { logger.debug('Verifying key signature', 'verifyKeySignature', { keyAlias, dataLength: data.length }); return ReactNativeBiometrics.verifyKeySignature(keyAlias, data, promptTitle, promptSubtitle, cancelButtonText).then(result => { logger.info('Key signature verification completed', 'verifyKeySignature', { keyAlias, success: result.success, hasSignature: !!result.signature }); return result; }).catch(error => { logger.error('Key signature verification failed', 'verifyKeySignature', error, { keyAlias }); throw error; }); } export function validateSignature(keyAlias = '', data, signature) { logger.debug('Validating signature', 'validateSignature', { keyAlias, dataLength: data.length, signatureLength: signature.length }); return ReactNativeBiometrics.validateSignature(keyAlias, data, signature).then(result => { logger.info('Signature validation completed', 'validateSignature', { keyAlias, valid: result.valid }); return result; }).catch(error => { logger.error('Signature validation failed', 'validateSignature', error, { keyAlias }); throw error; }); } export function getKeyAttributes(keyAlias) { logger.debug('Getting key attributes', 'getKeyAttributes', { keyAlias }); return ReactNativeBiometrics.getKeyAttributes(keyAlias).then(result => { logger.info('Key attributes retrieved', 'getKeyAttributes', { keyAlias, exists: result.exists, attributes: result.attributes }); return result; }).catch(error => { logger.error('Key attributes retrieval failed', 'getKeyAttributes', error, { keyAlias }); throw error; }); } // Key management configuration export function configureKeyAlias(keyAlias) { logger.debug('Configuring key alias', 'configureKeyAlias', { keyAlias }); return ReactNativeBiometrics.configureKeyAlias(keyAlias).then(result => { logger.info('Key alias configured successfully', 'configureKeyAlias', { keyAlias }); return result; }).catch(error => { logger.error('Key alias configuration failed', 'configureKeyAlias', error, { keyAlias }); throw error; }); } export function getDefaultKeyAlias() { logger.debug('Getting default key alias', 'getDefaultKeyAlias'); return ReactNativeBiometrics.getDefaultKeyAlias().then(result => { logger.info('Default key alias retrieved', 'getDefaultKeyAlias', { keyAlias: result }); return result; }).catch(error => { logger.error('Failed to get default key alias', 'getDefaultKeyAlias', error); throw error; }); } export function getAllKeys(customAlias) { logger.debug('Getting all keys', 'getAllKeys', { customAlias }); return ReactNativeBiometrics.getAllKeys(customAlias).then(result => { logger.info('All keys retrieved', 'getAllKeys', { keyCount: result.keys?.length || 0, customAlias }); return result; }).catch(error => { logger.error('Failed to get all keys', 'getAllKeys', error); throw error; }); } // Debugging utilities export function getDiagnosticInfo() { logger.debug('Getting diagnostic information', 'getDiagnosticInfo'); return ReactNativeBiometrics.getDiagnosticInfo().then(result => { logger.info('Diagnostic information retrieved', 'getDiagnosticInfo', { platform: result.platform, osVersion: result.osVersion, deviceModel: result.deviceModel, biometricCapabilities: result.biometricCapabilities }); return result; }).catch(error => { logger.error('Failed to get diagnostic information', 'getDiagnosticInfo', error); throw error; }); } export function runBiometricTest() { logger.debug('Running biometric test', 'runBiometricTest'); return ReactNativeBiometrics.runBiometricTest().then(result => { logger.info('Biometric test completed', 'runBiometricTest', { success: result.success, errorCount: result.errors?.length || 0, warningCount: result.warnings?.length || 0 }); return result; }).catch(error => { logger.error('Biometric test failed', 'runBiometricTest', error); throw error; }); } export function setDebugMode(enabled) { logger.debug('Setting debug mode', 'setDebugMode', { enabled }); // Enable/disable centralized logging based on debug mode logger.setEnabled(enabled); if (enabled) { logger.setLevel(LogLevel.DEBUG); } else { logger.setLevel(LogLevel.INFO); } return ReactNativeBiometrics.setDebugMode(enabled).then(result => { logger.info('Debug mode updated', 'setDebugMode', { enabled }); return result; }).catch(error => { logger.error('Failed to set debug mode', 'setDebugMode', error, { enabled }); throw error; }); } export function getDeviceIntegrityStatus() { logger.debug('Getting device integrity status', 'getDeviceIntegrityStatus'); return ReactNativeBiometrics.getDeviceIntegrityStatus().then(result => { logger.info('Device integrity status retrieved', 'getDeviceIntegrityStatus', { isCompromised: result.isCompromised, riskLevel: result.riskLevel }); return result; }).catch(error => { logger.error('Failed to get device integrity status', 'getDeviceIntegrityStatus', error); throw error; }); } // Configuration types // Initialize library with configuration export function configure(config) { logger.debug('Configuring library', 'configure', config); if (config.keyAlias) { return configureKeyAlias(config.keyAlias).then(result => { logger.info('Library configuration completed', 'configure', config); return result; }).catch(error => { logger.error('Library configuration failed', 'configure', error, config); throw error; }); } logger.info('Library configuration completed (no key alias)', 'configure', config); return Promise.resolve(); } // Export types for TypeScript users // Export logging utilities export { logger, LogLevel, enableLogging, setLogLevel, configureLogger } from "./logger.js"; // Convenience function to get logs for debugging export function getLogs() { return logger.getLogs(); } // Convenience function to clear logs export function clearLogs() { logger.clearLogs(); } // Biometric Change Detection API import { DeviceEventEmitter } from 'react-native'; /** * Subscribes to biometric change events. * * @param callback - Function to be called when biometric changes are detected * @returns EventSubscription that can be used to unsubscribe */ export function subscribeToBiometricChanges(callback) { logger.debug('Subscribing to biometric changes', 'subscribeToBiometricChanges'); // Subscribe using DeviceEventEmitter for cross-architecture compatibility // The native modules emit events via DeviceEventManagerModule return DeviceEventEmitter.addListener('onBiometricChange', callback); } /** * Unsubscribes from biometric change events. * * @param subscription - The EventSubscription returned from subscribeToBiometricChanges */ export function unsubscribeFromBiometricChanges(subscription) { logger.debug('Unsubscribing from biometric changes', 'unsubscribeFromBiometricChanges'); subscription.remove(); } /** * Starts biometric change detection. * This will begin monitoring for changes in biometric enrollment. * * @returns Promise that resolves when detection is started */ export function startBiometricChangeDetection() { logger.debug('Starting biometric change detection', 'startBiometricChangeDetection'); return ReactNativeBiometrics.startBiometricChangeDetection(); } /** * Stops biometric change detection. * This will stop monitoring for changes in biometric enrollment. * * @returns Promise that resolves when detection is stopped */ export function stopBiometricChangeDetection() { logger.debug('Stopping biometric change detection', 'stopBiometricChangeDetection'); return ReactNativeBiometrics.stopBiometricChangeDetection(); } //# sourceMappingURL=index.js.map