Overview

HUAWEI FIDO BioAuthn provides your app with powerful local biometric authentication capabilities, including fingerprint authentication and facial authentication. It allows your app to provide secure and easy-to-use password-free authentication for users while ensuring reliable authentication results.

What You Will Create

In this codelab, you will use the demo project to call the FIDO BioAuthn APIs. Through this demo project, you will:

What You Will Learn

In this codelab, you will learn how to:

Other Features

FIDO (FIDO2)

Hardware Requirements

Software Requirements

To integrate FIDO BioAuthn, you must complete the following preparations:

For details, please refer to Preparations for Integrating HUAWEI HMS Core.

Sign in to AppGallery Connect, click My projects, find your project, and click your desired app. Then, go to Project settings > Manage APIs.
Toggle on the FIDO switch.

If you are using Android Studio, you can integrate the HMS Core SDK via the Maven repository. Before you start developing an app, integrate the HMS Core SDK into your Android Studio project.

Adding the AppGallery Connect Configuration File of Your App

If you have enabled certain services in AppGallery Connect, add the agconnect-services.json file to your app.
步骤 1 - Sign in to AppGallery Connect and click My projects.
步骤 2 - Find your app project and click the app that needs to integrate the HMS Core SDK.
步骤 3 - Go to Project settings > General information. In the App information area, download the agconnect-services.json file.

步骤 4 - Copy the agconnect-services.json file to your app's root directory.

—-End

Configuring the Maven Repository Address for the HMS Core SDK

步骤 1 - Open the build.gradle file in the root directory of your Android Studio project.

步骤 2 - Add the AppGallery Connect plugin and the Maven repository.

—-End

Adding Build Dependencies

步骤 1 - Open the app-level build.gradle file of your project.

步骤 2 - Add the AppGallery Connect plugin configuration in either of the following methods:

步骤 3 - Add build dependencies. (Do not add dependencies on both BioAuthn-AndroidX and BioAuthn.)

  1. FIDO2
    dependencies { implementation 'com.huawei.hms:fido-fido2:{version}' }
  2. BioAuthn-AndroidX
    dependencies { implementation 'com.huawei.hms:fido-bioauthn-androidx:{version}' }
  3. BioAuthn
    dependencies { implementation 'com.huawei.hms:fido-bioauthn:{version}' }

—-End

Defining Multi-language Settings

Synchronizing the Project

Open the modified build.gradle file again. Click Sync Now in the upper right corner and wait until synchronization is complete.

Configuring Metadata

In the following scenario, configure metadata to prompt users to download HMS Core (APK):
The app store (such as HUAWEI AppGallery) allows your app to download other apps in the background, and you call relevant APIs through an activity.
In the following scenario, skip steps in this section. Currently, no way is available for prompting users to download HMS Core (APK).
The app store (such as Google Play) does not allow your app to download other apps in the background, or you call relevant APIs through a context.
Add the following code to the application element in the AndroidManifest.xml file, prompting users to download HMS Core (APK):

<application ...> <meta-data android:name="com.huawei.hms.client.channel.androidMarket" android:value="false" /> ... </application>

After HMS Core (APK) is downloaded, the HMS Core SDK will automatically install or update HMS Core (APK).

Configuring the AndroidManifest.xml File

In Android 11, the way for an app to query other apps on the user device and interact with them has been changed. IftargetSdkVersionn is300 or later for your app, add the> element in themanifestt element inAndroidManifest.xmll to allow your app to access HMS Core (APK).

<manifest ...> ... <queries> <intent> <action android:name="com.huawei.hms.core.aidlservice" /> </intent> </queries> ... </manifest>

HUAWEI FIDO provides two SDKs for biometric authentication:

BioAuthn-AndroidX SDK

Fingerprint Authentication

Huawei provides the secure fingerprint authentication capability. If the system is insecure, the callback method BioAuthnCallback.onAuthError() returns the error code BioAuthnPrompt.ERROR_SYS_INTEGRITY_FAILED. If the system is secure, fingerprint authentication is performed. For details about the fingerprint authentication error codes, please refer to the API Reference.

To use the fingerprint authentication capability, perform the following steps:
  1. Create the BioAuthnPrompt and BioAuthnCallback objects.
    Java private BioAuthnPrompt createBioAuthnPrompt() { // Callback. BioAuthnCallback callback = new BioAuthnCallback() { @Override public void onAuthError(int errMsgId, CharSequence errString) { showResult("Authentication error. errorCode=" + errMsgId + ",errorMessage=" + errString); } @Override public void onAuthSucceeded(BioAuthnResult result) { showResult("Authentication succeeded. CryptoObject=" + result.getCryptoObject()); } @Override public void onAuthFailed() { showResult("Authentication failed."); } }; return new BioAuthnPrompt(this, ContextCompat.getMainExecutor(this), callback); } Kotlin private fun createBioAuthnPrompt(): BioAuthnPrompt { // Callback. val callback = object : BioAuthnCallback() { override fun onAuthError(errMsgId: Int, errString: CharSequence) { showResult("Authentication error. errorCode=$errMsgId,errorMessage=$errString") } override fun onAuthSucceeded(result: BioAuthnResult) { if (result.cryptoObject != null) { showResult("Authentication succeeded. CryptoObject=" + result.cryptoObject!!) } else { showResult("Authentication succeeded. CryptoObject=null") } } override fun onAuthFailed() { showResult("Authentication failed.") } } return BioAuthnPrompt(this, ContextCompat.getMainExecutor(this), callback) }
  2. Create prompt information and perform authentication.
Java // Build prompt information. BioAuthnPrompt.PromptInfo.Builder builder = new BioAuthnPrompt.PromptInfo.Builder().setTitle("This is the title.") .setSubtitle("This is the subtitle") .setDescription("This is the description"); // The user will first be prompted to authenticate with biometrics, but also given the option to // authenticate with their device PIN, pattern, or password. setNegativeButtonText(CharSequence) should // not be set if this is set to true. builder.setDeviceCredentialAllowed(true); // Set the text for the negative button. setDeviceCredentialAllowed(true) should not be set if this button text // is set. // builder.setNegativeButtonText ("This is the Cancel button."); BioAuthnPrompt.PromptInfo info = builder.build(); resultTextView.setText("Start fingerprint authentication without CryptoObject.\nAuthenticating......\n"); bioAuthnPrompt.auth(info); Kotlin // Build prompt information. val builder = BioAuthnPrompt.PromptInfo.Builder().setTitle("This is the title.") .setSubtitle("This is the subtitle") .setDescription("This is the description") // The user will first be prompted to authenticate with biometrics, but also given the option to // authenticate with their device PIN, pattern, or password. setNegativeButtonText(CharSequence) should // not be set if this is set to true. builder.setDeviceCredentialAllowed(true) // Set the text for the negative button. setDeviceCredentialAllowed(true) should not be set if this button text // is set. // builder.setNegativeButtonText ("This is the Cancel button."); val info = builder.build() resultTextView!!.text = "Start fingerprint authentication without CryptoObject.\nAuthenticating......\n" bioAuthnPrompt!!.auth(info)

Facial Authentication

Huawei provides the secure facial authentication capability. If the system is insecure, the callback method BioAuthnCallback.onAuthError returns the error code FaceManager.FACE_ERROR_SYS_INTEGRITY_FAILED. If the system is secure, facial authentication is performed. For details about the facial authentication error codes, please refer to the API Reference.

To use the facial authentication capability, perform the following steps:

  1. Build a BioAuthnCallback object.
    Java // Callback. BioAuthnCallback callback = new BioAuthnCallback() { @Override public void onAuthError(int errMsgId, CharSequence errString) { showResult("Authentication error. errorCode=" + errMsgId + ",errorMessage=" + errString + (errMsgId == 1012 ? " The camera permission may not be enabled." : "")); } @Override public void onAuthHelp(int helpMsgId, CharSequence helpString) { resultTextView .append("Authentication help. helpMsgId=" + helpMsgId + ",helpString=" + helpString + "\n"); } @Override public void onAuthSucceeded(BioAuthnResult result) { showResult("Authentication succeeded. CryptoObject=" + result.getCryptoObject()); } @Override public void onAuthFailed() { showResult("Authentication failed."); } }; Kotlin // Callback. val callback = object : BioAuthnCallback() { override fun onAuthError(errMsgId: Int, errString: CharSequence) { showResult("Authentication error. errorCode=" + errMsgId + ",errorMessage=" + errString + if (errMsgId == 1012) " The camera permission may not be enabled." else "") } override fun onAuthHelp(helpMsgId: Int, helpString: CharSequence) { resultTextView!! .append("Authentication help. helpMsgId=$helpMsgId,helpString=$helpString\n") } override fun onAuthSucceeded(result: BioAuthnResult) { showResult("Authentication succeeded.") } override fun onAuthFailed() { showResult("Authentication failed.") } }
  2. Build a FaceManager object and call the auth method.
    Java // Cancellation signal. CancellationSignal cancellationSignal = new CancellationSignal(); FaceManager faceManager = new FaceManager(this); // Flags. int flags = 0; // Authentication message handler. Handler handler = null; // You are advised to set CryptoObject to null. The KeyStore is not associated with face authentication in the current // version. KeyGenParameterSpec.Builder.setUserAuthenticationRequired() must be set to false in this scenario. CryptoObject crypto = null; faceManager.auth(crypto, cancellationSignal, flags, callback, handler); Kotlin // Cancellation signal. val cancellationSignal = CancellationSignal() val faceManager = FaceManager(this) // Flags. val flags = 0 // Authentication message handler. val handler: Handler? = null // You are advised to set CryptoObject to null. The KeyStore is not associated with face authentication in the current // version. KeyGenParameterSpec.Builder.setUserAuthenticationRequired() must be set to false in this scenario. val crypto: CryptoObject? = null faceManager.auth(crypto, cancellationSignal, flags, callback, handler)

BioAuthn SDK

Fingerprint Authentication

Huawei provides the secure fingerprint authentication capability. If the system is insecure, the callback method BioAuthnCallback.onAuthError() returns the error code FingerprintManager.ERROR_SYS_INTEGRITY_FAILED. If the system is secure, fingerprint authentication is performed. For details about the fingerprint authentication error codes, please refer to the API Reference.

To use the fingerprint authentication capability, perform the following steps:

  1. Create the FingerprintManager and BioAuthnCallback objects.
    Java private FingerprintManager createFingerprintManager() { // Callback. BioAuthnCallback callback = new BioAuthnCallback() { @Override public void onAuthError(int errMsgId, CharSequence errString) { showResult("Authentication error. errorCode=" + errMsgId + ",errorMessage=" + errString); } @Override public void onAuthSucceeded(BioAuthnResult result) { showResult("Authentication succeeded. CryptoObject=" + result.getCryptoObject()); } @Override public void onAuthFailed() { showResult("Authentication failed."); } }; return new FingerprintManager(this, Executors.newSingleThreadExecutor(), callback); } Kotlin // Callback. val callback = object : BioAuthnCallback() { override fun onAuthError(errMsgId: Int, errString: CharSequence?) { showResult("Authentication error. errorCode=$errMsgId,errorMessage=$errString") } override fun onAuthSucceeded(result: BioAuthnResult) { showResult("Authentication succeeded. CryptoObject=" + result.cryptoObject) } override fun onAuthFailed() { showResult("Authentication failed.") } } return FingerprintManager(this, Executors.newSingleThreadExecutor(), callback) }
  2. Perform authentication.
    Java fingerprintManager.auth(); Kotlin fingerprintManager!!.auth()

Facial Authentication

Huawei provides the secure facial authentication capability. If the system is insecure, the callback method BioAuthnCallback.onAuthError returns the error code FaceManager.FACE_ERROR_SYS_INTEGRITY_FAILED. If the system is secure, facial authentication is performed. For details about the facial authentication error codes, please refer to the API Reference.

To use the facial authentication capability, perform the following steps:

  1. Build a BioAuthnCallback object.
    Java // Callback. BioAuthnCallback callback = new BioAuthnCallback() { @Override public void onAuthError(int errMsgId, CharSequence errString) { showResult("Authentication error. errorCode=" + errMsgId + ",errorMessage=" + errString + (errMsgId == 1012 ? " The camera permission may not be enabled." : "")); } @Override public void onAuthHelp(int helpMsgId, CharSequence helpString) { resultTextView .append("Authentication help. helpMsgId=" + helpMsgId + ",helpString=" + helpString + "\n"); } @Override public void onAuthSucceeded(BioAuthnResult result) { showResult("Authentication succeeded. CryptoObject=" + result.getCryptoObject()); } @Override public void onAuthFailed() { showResult("Authentication failed."); } }; Kotlin // Callback. val callback = object : BioAuthnCallback() { override fun onAuthError(errMsgId: Int, errString: CharSequence?) { showResult("Authentication error. errorCode=" + errMsgId + ",errorMessage=" + errString + if (errMsgId == 1012) " The camera permission may not be enabled." else "") } override fun onAuthHelp(helpMsgId: Int, helpString: CharSequence?) { resultTextView!! .append("Authentication help. helpMsgId=$helpMsgId,helpString=$helpString\n") } override fun onAuthSucceeded(result: BioAuthnResult) { showResult("Authentication succeeded. CryptoObject=" + result.cryptoObject) } override fun onAuthFailed() { showResult("Authentication failed.") } }
  2. Build a FaceManager object and call the auth method.
    Java // Cancellation signal. CancellationSignal cancellationSignal = new CancellationSignal(); FaceManager faceManager = new FaceManager(this); // Flags. int flags = 0; // Authentication message handler. Handler handler = null; // You are advised to set CryptoObject to null. The KeyStore is not associated with face authentication in the current // version. KeyGenParameterSpec.Builder.setUserAuthenticationRequired() must be set to false in this scenario. CryptoObject crypto = null; faceManager.auth(crypto, cancellationSignal, flags, callback, handler); Kotlin // Cancellation signal. val cancellationSignal = CancellationSignal() val faceManager = FaceManager(this) // Flags. val flags = 0 // Authentication message handler. val handler: Handler? = null // You are advised to set CryptoObject to null. The KeyStore is not associated with face authentication in the current // version. KeyGenParameterSpec.Builder.setUserAuthenticationRequired() must be set to false in this scenario. val crypto: CryptoObject? = null resultTextView!!.text = "Start face authentication.\nAuthenticating......\n" faceManager.auth(crypto, cancellationSignal, flags, callback, handler)

Install the test APK, launch the app, and tap relevant buttons to trigger fingerprint authentication or facial authentication.

Well done. You have successfully completed this codelab and learned how to:

For more information, please click the following link:

Related documents

BioAuthn-AndroidX SDK

To download the sample code, click the link below:

Download

BioAuthn SDK

To download the sample code, click the link below:

Download

Code copied