目录

1. 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.

Notice:

· Only EMUI 4 (API level 23) and later versions support fingerprint authentication. In addition, the device hardware must support fingerprint authentication.

· When you create PromptInfo using PromptInfo.Builder, you can only select either setDeviceCredentialAllowed(true) or setNegativeButtonText(). setDeviceCredentialAllowed(true) allows change from fingerprint authentication to another authentication mode, for example, lock screen password authentication.

· When using BioAuthnPrompt.PromptInfo.Builder.setDeviceCredentialAllowed(true), pay attention to the following:

1. This function does not support devices with in-screen fingerprint sensors and running EMUI 9.x, for example, Mate 20 Pro, P30, P30 Pro, and Magic 2. If the function is used on these devices, the authentication will fail immediately when users change the authentication mode to lock screen password authentication.

2. In EMUI 9.x or earlier, fingerprint authentication may work once only. You can solve this problem in one of the following ways:

1) Do not create the BioAuthnPrompt object for the activity repeatedly.

2) After the authentication is complete, call the Activity.recreate() method to recreate the UI.

3) Close the activity and open it again.

The following is the sample code for using the fingerprint authentication capability:

  1. import android.view.View;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.core.content.ContextCompat;
  4. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnCallback;
  5. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnManager;
  6. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnPrompt;
  7. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnResult;
  8. public class MainActivity extends AppCompatActivity {
  9.    public void btnFingerAuthenticateWithoutCryptoObjectClicked(View view) {
  10.        // Whether fingerprint authentication is supported.
  11.        BioAuthnManager bioAuthnManager = new BioAuthnManager(this);
  12.        int errorCode = bioAuthnManager.canAuth();
  13.        if (errorCode != 0) {
  14.            // Fingerprint authentication is not supported.
  15.            return;
  16.        }
  17.        // Callback.
  18.        BioAuthnCallback callback = new BioAuthnCallback() {
  19.            @Override
  20.            public void onAuthError(int errMsgId, CharSequence errString) {
  21.                // Authentication error.
  22.            }
  23.            @Override
  24.            public void onAuthSucceeded(BioAuthnResult result) {
  25.                // Authentication successful.
  26.            }
  27.            @Override
  28.            public void onAuthFailed() {
  29.                // Authentication failed.
  30.            }
  31.        };
  32.        BioAuthnPrompt bioAuthnPrompt = new BioAuthnPrompt(this, ContextCompat.getMainExecutor(this), callback);
  33.        // Construct the prompt information.  
  34.        BioAuthnPrompt.PromptInfo.Builder builder =
  35.            new BioAuthnPrompt.PromptInfo.Builder().setTitle("This is the title.")
  36.                .setSubtitle("This is the subtitle")
  37.                .setDescription("This is the description");
  38.        // Prompt the user to use the fingerprint for authentication, and also provide options for the user to use the PIN, lock screen pattern, or lock screen password for authentication.  
  39.        // If the parameter is set to true here, setNegativeButtonText(CharSequence) is not supported.  
  40.        builder.setDeviceCredentialAllowed(true);
  41.        // Set the cancellation button title. If a title is set, setDeviceCredentialAllowed(true) is not supported.
  42.        // builder.setNegativeButtonText("This is the 'Cancel' button.");
  43.        BioAuthnPrompt.PromptInfo info = builder.build();
  44.        bioAuthnPrompt.auth(info);
  45.    }
  46. }

2. 3D Facial Authentication

Huawei provides the secure 3D 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, 3D facial authentication is performed. For details about the 3D facial authentication error codes, please refer to the API Reference.

Notice:

1. Only EMUI 10 (API level 29) and later versions support 3D facial authentication. In addition, the device hardware must support 3D facial authentication.

2. When CryptoObject is used for authentication and KeyGenParameterSpec.Builder is used to generate a key, setUserAuthenticationRequired() must be set to false. In this version, the KeyStore is not associated with the 3D facial authentication result.

The following is the sample code for using the 3D facial authentication capability:

  1. import android.os.CancellationSignal;
  2. import android.os.Handler;
  3. import android.view.View;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnCallback;
  6. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnResult;
  7. import com.huawei.hms.support.api.fido.bioauthn.CryptoObject;
  8. import com.huawei.hms.support.api.fido.bioauthn.FaceManager;
  9. public class MainActivity extends AppCompatActivity {
  10.    public void btnFaceAuthenticateWithoutCryptoObjectClicked(View view) {
  11.        // Callback.
  12.        BioAuthnCallback callback = new BioAuthnCallback() {
  13.            @Override
  14.            public void onAuthError(int errMsgId, CharSequence errString) {
  15.                // Authentication error.
  16.            }
  17.            @Override
  18.            public void onAuthHelp(int helpMsgId, CharSequence helpString) {
  19.                // Help.
  20.            }
  21.            @Override
  22.            public void onAuthSucceeded(BioAuthnResult result) {
  23.                // Authentication successful.
  24.            }
  25.            @Override
  26.            public void onAuthFailed() {
  27.                // Authentication failed.
  28.            }
  29.        };
  30.        // Cancellation signal.
  31.        CancellationSignal cancellationSignal = new CancellationSignal();
  32.        FaceManager faceManager = new FaceManager(this);
  33.        int errorCode = faceManager.canAuth();
  34.        if (errorCode != 0) {
  35.            // Authentication is not supported.
  36.            return;
  37.        }
  38.        // Flags.
  39.        int flags = 0;
  40.        // Authentication message handler.
  41.        Handler handler = null;
  42.        // It is recommended that CryptoObject be set to null. In this version, KeyStore is not associated with facial authentication.  
  43.        // The value false must be passed to KeyGenParameterSpec.Builder.setUserAuthenticationRequired().
  44.        CryptoObject crypto = null;
  45.        faceManager.auth(crypto, cancellationSignal, flags, callback, handler);
  46.    }
  47. }
Note:

For more sample information, click fido-bioauthn-androidx-sample.zip to download the sample code.