Show a biometric authentication dialog
.jpg)
Show a biometric authentication conversation
One method of shielding sensitive information or top rate
content material within your app is to request biometric authentication,
together with using face reputation or fingerprint popularity. This manual
explains how to assist biometric login flows in your app.
Declare the forms of authentication that your app supports
To outline the varieties of authentication that your app
supports, use the BiometricManager.Authenticators interface. The gadget lets
you declare the following forms of authentication:
To begin the use of an authenticator, the user wishes to
create a PIN, sample, or password. If the user does not already have one, the
biometric enrollment glide activates them to create one.
To define the styles of biometric authentication that your
app accepts, skip an authentication kind or a bitwise mixture of kinds into the
setAllowedAuthenticators() method. The following code snippet suggests the way
to guide authentication the usage of either a Class three biometric or a screen
lock credential.
Kotlin
Java
The following mixtures of authenticator types aren't
supported on Android 10 (API stage 29) and lower: DEVICE_CREDENTIAL and
BIOMETRIC_STRONG check for the presence of a PIN, sample, or password on
Android 10 and lower, use the KeyguardManager.IsDeviceSecure() approach.
Check that biometric authentication is available
After making a decision which authentication factors your
app supports, test whether these factors are available. To accomplish that,
bypass the equal bitwise mixture of kinds that you declared using the
setAllowedAuthenticators() approach into the canAuthenticate() method. If
necessary, invoke the ACTION_BIOMETRIC_ENROLL motive movement. In the cause
greater, provide the set of authenticators that your app accepts. This cause
prompts the person to register credentials for an authenticator that your app
accepts.
Kotlin
Java
Determine how the person authenticated
After the user authenticates, you may take a look at whether
or not the consumer authenticated the use of a tool credential or a biometric
credential by using calling getAuthenticationType().
Display the login activate
To show a device set off that requests the person to
authenticate the usage of biometric credentials, use the Biometric library.
This gadget-supplied conversation is steady throughout the apps that use it,
creating a extra straightforward user revel in. An example dialog seems in
determine 1.
To upload biometric authentication on your app using the Biometric
library, entire the subsequent steps:
In your app module's build.Gradle file, upload a dependency
on the androidx.Biometric library.
In the pastime or fragment that hosts the biometric login
conversation, display the dialog the use of the common sense proven inside the
following code snippet:
Kotlin
Java
Use a cryptographic answer that depends on authentication
To similarly defend touchy information inside your app, you
could contain cryptography into your biometric authentication workflow the usage
of an example of CryptoObject. The framework supports the following
cryptographic gadgets: Signature, Cipher, and Mac.
After the person authenticates correctly the use of a
biometric activate, your app can perform a cryptographic operation. For instance,
if you authenticate the usage of a Cipher object, your app can then carry out
encryption and decryption the use of a SecretKey item.
The following sections go through examples of using a Cipher
object and a SecretKey object to encrypt statistics. Each instance uses the
following strategies:
Kotlin
Java
Authenticate the usage of handiest biometric credentials
If your app uses a mystery key that calls for biometric
credentials to free up, the person need to authenticate their biometric
credentials on every occasion earlier than your app accesses the important
thing.
To encrypt touchy information handiest after the user
authenticates the use of biometric credentials, whole the subsequent steps:
Kotlin
Java
Start a biometric authentication workflow that consists of a
cipher:
Kotlin
Java
Within your biometric authentication callbacks, use the name
of the game key to encrypt the touchy facts:
Kotlin
Java
Authenticate the usage of either biometric or lock screen
credentials
You can use a mystery key that allows for authentication
using either biometric credentials or lock display screen credentials (PIN,
pattern, or password). When configuring this key, specify a validity term.
During this term, your app can carry out more than one cryptographic operations
without the user desiring to re-authenticate.
To encrypt touchy facts after the person authenticates the
usage of biometric or lock display credentials, whole the following steps:
Kotlin
Java
Within a term of VALIDITY_DURATION_SECONDS after the
consumer authenticates, encrypt the sensitive information:
Kotlin