Overview

HUAWEI FIDO2 provides your app with FIDO2 based on the WebAuthn standard. It provides Android Java APIs for apps and browsers, and allows users to complete authentication through roaming authenticators (USB, NFC, and Bluetooth authenticators) and platform authenticators (fingerprint and 3D face authenticators).

What You Will Create

In this codelab, you will use the demo project that has been created for you to call HUAWEI FIDO2 client APIs. Through the demo project, you will:

What You Will Learn

In this codelab, you will learn how to:

Other Features

FIDO (BioAuthn)

Hardware Requirements

Software Requirements

To integrate FIDO2 client, 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>

FIDO2 provides the WebAuthn-based FIDO2 client. This codelab only describes how to use FIDO2 client APIs. For details about operations related to the app server and FIDO server, please refer to related standards.
To integrate the FIDO2 client, perform the following steps:

  1. Initialize a Fido2Client object using an activity instance. The object can be written in the onCreate method of the activity.
    Java Fido2Client fido2Client = Fido2.getFido2Client(this); Kotlin Fido2Client fido2Client = Fido2.getFido2Client(this)
  2. Obtain a challenge value and the registration or authentication policy from the FIDO server, and initiate an authentication or registration request based on the obtained information.
    Java // Obtain the challenge value and related policy from the FIDO server, and initiate a Fido2RegistrationRequest // request. ServerPublicKeyCredentialCreationOptionsResponse response = fidoServer.getAttestationOptions(request); if (!ServerStatus.OK.equals(response.getStatus())) { Log.e(TAG, getString(R.string.reg_fail) + response.getErrorMessage()); showError(getString(R.string.reg_fail) + response.getErrorMessage()); } PublicKeyCredentialCreationOptions publicKeyCredentialCreationOptions = ServerUtils.convert2PublicKeyCredentialCreationOptions(fido2Client,response); Kotlin // Obtain the challenge value and related policy from the FIDO server, and initiate a Fido2RegistrationRequest // request. val response = fidoServer.getAttestationOptions(request) if (ServerStatus.OK != response.status) { Log.e(TAG, getString(R.string.reg_fail) + response.errorMessage) showError(getString(R.string.reg_fail) + response.errorMessage) } val fido2ClientTmp = fido2Client; if (fido2ClientTmp != null) { val publicKeyCredentialCreationOptions = ServerUtils.convert2PublicKeyCredentialCreationOptions(fido2ClientTmp, response) reg2Fido2Client(publicKeyCredentialCreationOptions) }
  3. Call getRegistrationIntent() or getAuthenticationIntent() to initiate registration or authentication.
  4. Call launchFido2Activity in Fido2IntentCallback to launch the registration or authentication page.
    Java fido2Client.getRegistrationIntent(registrationRequest, registrationOptions, new Fido2IntentCallback() { @Override public void onSuccess(Fido2Intent fido2Intent) { fido2Intent.launchFido2Activity(Fido2DemoMainActivity.this, Fido2Client.REGISTRATION_REQUEST); } @Override public void onFailure(int errorCode, CharSequence errString) { showError(getString(R.string.reg_fail) + errorCode + "=" + errString); } }); Kotlin fido2Client!!.getRegistrationIntent(registrationRequest, registrationOptions, object : Fido2IntentCallback { override fun onSuccess(fido2Intent: Fido2Intent) { fido2Intent.launchFido2Activity(this@Fido2DemoMainActivity, Fido2Client.REGISTRATION_REQUEST) } override fun onFailure(errorCode: Int, errString: CharSequence) { showError(getString(R.string.reg_fail) + errorCode + "=" + errString) } })
  5. Call getFido2RegistrationResponse or getFido2AuthenticationResponse in activity.onActivityResult to obtain the registration or authentication response.
    Java switch (requestCode) { // Receive the registration response. case Fido2Client.REGISTRATION_REQUEST: Fido2RegistrationResponse fido2RegistrationResponse = fido2Client.getFido2RegistrationResponse(data); reg2Server(fido2RegistrationResponse); break; // Receive the authentication response. case Fido2Client.AUTHENTICATION_REQUEST: Fido2AuthenticationResponse fido2AuthenticationResponse = fido2Client.getFido2AuthenticationResponse(data); auth2Server(fido2AuthenticationResponse); break; default: break; } Kotlin when (requestCode) { // Receive the registration response. Fido2Client.REGISTRATION_REQUEST -> { val fido2RegistrationResponse = fido2Client!!.getFido2RegistrationResponse(data) reg2Server(fido2RegistrationResponse) } // Receive the authentication response. Fido2Client.AUTHENTICATION_REQUEST -> { val fido2AuthenticationResponse = fido2Client!!.getFido2AuthenticationResponse(data) auth2Server(fido2AuthenticationResponse) } else -> { } }

Install the test APK, launch the app, and tap Register or Authenticate.

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

For more information, please click the following link:

Related documents
To download the sample code, click the link below:

Download

Code copied