5 steps to implement Biometric authentication in Android

Anitaa Murthy
ProAndroidDev
Published in
5 min readJul 11, 2018

--

I recently got a chance to work with the new BiometricPrompt API feature released by Google this year. While fingerprint authentication support has been available since Android 6.0, the new BiometricPrompt promises more accuracy and a consistent level of security across all devices that run our application.

Having implemented biometric authentication in an android app recently, I thought I would highlight some of the basic steps required to implement this feature in an android app.

So let’s begin!

Step 1: Add the required permissions in the AndroidManifest.xml

AndroidManifest.xml

Step 2: Check if the device supports Biometric authentication

Specifically, we are going to check if the following conditions are met:

  • The device is running Android 6.0 or higher
  • The device features a fingerprint sensor
  • The user has granted your app permission to access the fingerprint sensor.
  • The user has registered at least one fingerprint on their device.

We can create a separate util class to check if the above conditions are met:

Step 3: Display BiometricPrompt dialog

Once the above conditions are checked, we can check if the android version in the device is Android P. The Biometric dialog is only supported in Android P. Let’s take a look at that first.

Below code is to display a biometricPrompt dialog:

Using the BiometricPrompt builder we can:

  • setTitle() — Set the title to display (Required)
  • setSubtitle() — Set the subtitle to display (Optional)
  • setDescription() — Set the description to display(Optional)
  • setNegativeButton() — Set the text for the negative button(Required). You must also provide an Executor instance and a click listener for the negative button.

Note: You can’t customise the icon or error message that are used within the dialog.

A typical BiometricPrompt dialog

Step 4: Handle authentication Callback

Next we use the BiometricPrompt.AuthenticationCallback to listen for authentication events from the users. It includes 4 methods:

onAuthenticationSucceeded

When the fingerprint is has been successfully matched with one of the fingerprints registered on the device, then this callback will be triggered. An AuthenticationResult object will be passed the the callback.

onAuthenticationFailed

When the fingerprint doesn’t match with any of the fingerprints registered on the device, then this callback will be triggered.

onAuthenticationError

When an unrecoverable error has been encountered and the authentication process has completed without success, then this callback will be triggered. The callback is provided with an error code to identify the cause of the error, along with the error message. The different types of error codes that can occur are:

onAuthenticationHelp

This method is called when a non-fatal error has occurred during the authentication process. The callback will be provided with an help code to identify the cause of the error, along with a help message. The different types of help codes that can occur are:

Custom Biometric Callback class

If you have an Android P device, then that’s it!

But what happens if the device does not support Android P?

So all versions below Android P do not support the BiometricPrompt api. Luckily, we can still use the FingerprintManagerCompat API to authenticate our users. This involves:

  1. Initialising KeyStore and generating key — The Android keystore allows you to store cryptographic keys in a way that makes them more difficult to extract from the device. It also restricts how and when each key can be used. Once the key is generated, it will be stored securely on device by using KeyStore instance and used for initialising the cipher object in the next step.

2. Initialising the cipher — This initialisation of Cipher object will be used to create CryptoObject instance. While initialising cipher , the generated and the stored key in the keystore container is used. If the cipher is successfully initialised, then we can assume that the previously stored key is not invalidated and it can still be used.

3. Creating a CrytoObject — The fingerprint scanner will use the CryptoObject to help authenticate the results of a fingerprint scan. The CryptoObject is used to ensure that the fingerprint authentication result was not tampered with. In order to create an instance of the CryptoObject:

4. Assigning the CryptoObject to the FingerprintManagerCompat — Instantiate a FingerprintManagerCompat and call the authenticate method.

The authenticate method requires the following parameters:

  • cryptoObject
  • The second parameter is always zero — The Android documentation identifies this as set of flags and is most likely reserved for future use.
  • The third parameter, cancellationSignal — is an object used to turn off the fingerprint scanner and cancel the current request.
  • The fourth parameter, AuthenticationCallback class — This will be the same as the BiometricAuthenticationCallback.
  • The fifth parameter, optional Handler instance — If a Handler object is provided, the FingerprintManagerCompat will use the Looper from that object when processing the messages from the fingerprint hardware.

5. Creating a UI similar to the BiometricPrompt Dialog — So now that we have enabled the fingerprint authentication in devices above 6.0 and below 8.0, we need to display an user interface to initiate the authentication. I have created a CustomDialog class that replicates the BiometricPrompt dialog (to maintain consistency across devices).

The final output will be like this:

Conclusion

I hope this post was helpful to provide an insight into the new BiometricPrompt API. You can checkout the full code in Github.

I have also developed a small SDK that allows for easy implementation of fingerprint authentication. It can be added to any android app.

Please check it out and let me know your thoughts!

Happy coding!

--

--