Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-biometrics

Package Overview
Dependencies
Maintainers
4
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-biometrics

React Native biometric functionality for signing and encryption

  • 3.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
4
Created
Source

react-native-biometrics

React native biometrics is a simple bridge to native iOS and Android keystore management. It allows you to create public private key pairs that are stored in native keystores and protected by biometric authentication. Those keys can then be retrieved later, after proper authentication, and used to create a cryptographic signature.

React Native Compatibility

react-native-biometrics versionRequired React Native Version
>= 2.0.0>= 0.60
<= 1.7.0<= 0.59.x

Getting started

using either Yarn:

yarn add react-native-biometrics

or npm:

$ npm install react-native-biometrics --save

Install pods

$ npx pod-install

On React Native 0.60+ the CLI autolink feature links the module while building the app.

Additional configuration

iOS

This package requires an iOS target SDK version of iOS 10 or higher

Ensure that you have the NSFaceIDUsageDescription entry set in your react native iOS project, or Face ID will not work properly. This description will be presented to the user the first time a biometrics action is taken, and the user will be asked if they want to allow the app to use Face ID. If the user declines the usage of face id for the app, the isSensorAvailable function will indicate biometrics is unavailable until the face id permission is specifically allowed for the app by the user.

Android

This package requires a compiled SDK version of 29 (Android 10.0) or higher

Usage

This package is designed to make server authentication using biometrics easier. Here is an image from https://android-developers.googleblog.com/2015/10/new-in-android-samples-authenticating.html illustrating the basic use case:

react-native-biometrics

When a user enrolls in biometrics, a key pair is generated. The private key is stored securely on the device and the public key is sent to a server for registration. When the user wishes to authenticate, the user is prompted for biometrics, which unlocks the securely stored private key. Then a cryptographic signature is generated and sent to the server for verification. The server then verifies the signature. If the verification was successful, the server returns an appropriate response and authorizes the user.

Biometry Types

TouchID (iOS only)

A constant for the touch id sensor type, evaluates to 'TouchID'

Example

import ReactNativeBiometrics, { BiometryTypes } from 'react-native-biometrics'

const rnBiometrics = new ReactNativeBiometrics()

const { biometryType } = await rnBiometrics.isSensorAvailable()

if (biometryType === BiometryTypes.TouchID) {
  //do something fingerprint specific
}

FaceID (iOS only)

A constant for the face id sensor type, evaluates to 'FaceID'

Example

import ReactNativeBiometrics, { BiometryTypes } from 'react-native-biometrics'

const rnBiometrics = new ReactNativeBiometrics()

const { biometryType } = await rnBiometrics.isSensorAvailable()

if (biometryType === BiometryTypes.FaceID) {
  //do something face id specific
}

Biometrics (Android only)

A constant for generic Biometrics, evaluates to 'Biometrics'

Example

import ReactNativeBiometrics, { BiometryTypes } from 'react-native-biometrics'

const rnBiometrics = new ReactNativeBiometrics()

const { biometryType } = await rnBiometrics.isSensorAvailable()

if (biometryType === BiometryTypes.Biometrics) {
  //do something face id specific
}

Class

Constructor

Options Object

ParameterTypeDescriptioniOSAndroid
allowDeviceCredentialsbooleanBoolean that will enable the ability for the device passcode to be used instead of biometric information. On iOS, the prompt will only be shown after biometrics has failed twice. On Android, the prompt will be shown on the biometric prompt and does not require the user to attempt to use biometrics information first. Note: This feature is not supported on Android versions prior to API 30.

Example

import ReactNativeBiometrics from 'react-native-biometrics'

const rnBiometrics = new ReactNativeBiometrics({ allowDeviceCredentials: true })

// Perform operations as normal
// All prompts will allow for fallback to the device's credentials for authentication

isSensorAvailable()

Detects what type of biometric sensor is available. Returns a Promise that resolves to an object with details about biometrics availability

Result Object

PropertyTypeDescription
availableboolA boolean indicating if biometrics is available or not
biometryTypestringA string indicating what type of biometrics is available. TouchID, FaceID, Biometrics, or undefined if biometrics is not available.
errorstringAn error message indicating why biometrics may not be available. undefined if there is no error.

Example

import ReactNativeBiometrics, { BiometryTypes } from 'react-native-biometrics'

const rnBiometrics = new ReactNativeBiometrics()

rnBiometrics.isSensorAvailable()
  .then((resultObject) => {
    const { available, biometryType } = resultObject

    if (available && biometryType === BiometryTypes.TouchID) {
      console.log('TouchID is supported')
    } else if (available && biometryType === BiometryTypes.FaceID) {
      console.log('FaceID is supported')
    } else if (available && biometryType === BiometryTypes.Biometrics) {
      console.log('Biometrics is supported')
    } else {
      console.log('Biometrics not supported')
    }
  })

createKeys()

Generates a public private RSA 2048 key pair that will be stored in the device keystore. Returns a Promise that resolves to an object providing details about the keys.

Result Object

PropertyTypeDescription
publicKeystringA base64 encoded string representing the public key

Example

import ReactNativeBiometrics from 'react-native-biometrics'

const rnBiometrics = new ReactNativeBiometrics()

rnBiometrics.createKeys()
  .then((resultObject) => {
    const { publicKey } = resultObject
    console.log(publicKey)
    sendPublicKeyToServer(publicKey)
  })

biometricKeysExist()

Detects if keys have already been generated and exist in the keystore. Returns a Promise that resolves to an object indicating details about the keys.

Result Object

PropertyTypeDescription
keysExistboolA boolean indicating if keys exist in the keystore

Example

import ReactNativeBiometrics from 'react-native-biometrics'

const rnBiometrics = new ReactNativeBiometrics()
rnBiometrics.biometricKeysExist()
  .then((resultObject) => {
    const { keysExist } = resultObject

    if (keysExist) {
      console.log('Keys exist')
    } else {
      console.log('Keys do not exist or were deleted')
    }
  })

deleteKeys()

Deletes the generated keys from the device keystore. Returns a Promise that resolves to an object indicating details about the deletion.

Result Object

PropertyTypeDescription
keysDeletedboolA boolean indicating if keys were deleted from the keystore

Example

import ReactNativeBiometrics from 'react-native-biometrics'

const rnBiometrics = new ReactNativeBiometrics()

rnBiometrics.deleteKeys()
  .then((resultObject) => {
    const { keysDeleted } = resultObject

    if (keysDeleted) {
      console.log('Successful deletion')
    } else {
      console.log('Unsuccessful deletion because there were no keys to delete')
    }
  })

createSignature(options)

Prompts the user for their fingerprint or face id in order to retrieve the private key from the keystore, then uses the private key to generate a RSA PKCS#1v1.5 SHA 256 signature. Returns a Promise that resolves to an object with details about the signature.

**NOTE: No biometric prompt is displayed in iOS simulators when attempting to retrieve keys for signature generation, it only occurs on actual devices.

Options Object

ParameterTypeDescriptioniOSAndroid
promptMessagestringMessage that will be displayed in the fingerprint or face id prompt
payloadstringString of data to be signed by the RSA signature
cancelButtonTextstringText to be displayed for the cancel button on biometric prompts, defaults to Cancel

Result Object

PropertyTypeDescription
successboolA boolean indicating if the process was successful, false if the users cancels the biometrics prompt
signaturestringA base64 encoded string representing the signature. undefined if the process was not successful.
errorstringAn error message indicating reasons why signature creation failed. undefined if there is no error.

Example

import ReactNativeBiometrics from 'react-native-biometrics'

let epochTimeSeconds = Math.round((new Date()).getTime() / 1000).toString()
let payload = epochTimeSeconds + 'some message'

const rnBiometrics = new ReactNativeBiometrics()

rnBiometrics.createSignature({
    promptMessage: 'Sign in',
    payload: payload
  })
  .then((resultObject) => {
    const { success, signature } = resultObject

    if (success) {
      console.log(signature)
      verifySignatureWithServer(signature, payload)
    }
  })

simplePrompt(options)

Prompts the user for their fingerprint or face id. Returns a Promise that resolves if the user provides a valid biometrics or cancel the prompt, otherwise the promise rejects.

**NOTE: This only validates a user's biometrics. This should not be used to log a user in or authenticate with a server, instead use createSignature. It should only be used to gate certain user actions within an app.

Options Object

ParameterTypeDescriptioniOSAndroid
promptMessagestringMessage that will be displayed in the biometrics prompt
fallbackPromptMessagestringMessage that will be shown when FaceID or TouchID has failed and a passcode has been set on the device.
cancelButtonTextstringText to be displayed for the cancel button on biometric prompts, defaults to Cancel

Result Object

PropertyTypeDescription
successboolA boolean indicating if the biometric prompt succeeded, false if the users cancels the biometrics prompt
errorstringAn error message indicating why the biometric prompt failed. undefined if there is no error.

Example

import ReactNativeBiometrics from 'react-native-biometrics'

const rnBiometrics = new ReactNativeBiometrics()

rnBiometrics.simplePrompt({promptMessage: 'Confirm fingerprint'})
  .then((resultObject) => {
    const { success } = resultObject

    if (success) {
      console.log('successful biometrics provided')
    } else {
      console.log('user cancelled biometric prompt')
    }
  })
  .catch(() => {
    console.log('biometrics failed')
  })

Troubleshooting

  • Because of this library's dependency on androidx.biometric:biometric:1.0.0 it can cause transitive dependency resolution to change on certain version of React Native and androidx.swiperefreshlayout may no longer be able to be resolved. This can be fixed by adding an explicit dependency on the library in your android/app/build.gradle:

    dependencies {
        implementation fileTree(dir: "libs", include: ["*.jar"])
        implementation "com.facebook.react:react-native:+"  // From node_modules
        implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" // temp fix
        ...
    }
    
  • There is a known issue on the iOS 13.x simulators where keys generated with access control flags cannot be queried and found properly. This results in key not found errors in biometricKeysExist and createSignature on those simulators. However, it works correctly on actual devices running iOS 13.

Keywords

FAQs

Package last updated on 06 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc