仅安装了设备凭据的Androidx.biometric不会在装有Android 9和以下设备的设备上调用回调

问题描述

我们使用androidx支持库实现了身份验证(和签名)。它可以在具有Android 10(也为DeviceCredentialUnlock)和生物识别(代码几乎没有什么不同的设备,因为我们想知道它是否使用生物识别或设备凭据进行签名)上完美运行。

在Android 8及更高版本中,它从不调用回调。没有错误消息记录,CompletableFuture永远不会完成。

   CompletableFuture<String> signingResult = new CompletableFuture<>();

    final String title = fragmentActivity.getString(R.string.LockScreenTitle);

    final String description = fragmentActivity.getString(R.string.LockScreenDescription);
    
    final String data = "testData";

    Executor executor = ContextCompat.getMainExecutor(fragmentActivity);

    final BiometricPrompt.AuthenticationCallback callback = new BiometricPrompt.AuthenticationCallback() {

        @Override
        public void onAuthenticationError(int errorCode,@NonNull CharSequence errString) {
            LOGGER.info("onAuthenticationError");
            Exception exception = mapAuthenticationError(errorCode,errString);
            signingResult.completeExceptionally(exception);
        }

        @Override
        public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            try {
                LOGGER.info("onAuthenticationSucceeded");
                String signedData = SignInternal(data);
                signingResult.complete(signedData);
            } catch (SecuretokenException e) {
                signingResult.completeExceptionally(e);
            }
        }

        @Override
        public void onAuthenticationFailed() {
            LOGGER.info("onAuthenticationFailed");
            signingResult.completeExceptionally(new SecuretokenException(SecuretokenException.ErrorCode.ABORTED_BY_USER));
        }
    };

    BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
            .setTitle(title)
            .setDescription(description)
            .setDeviceCredentialAllowed(true)
            .build();

    final BiometricPrompt bp = new BiometricPrompt(fragmentActivity,executor,callback);


    fragmentActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            bp.authenticate(promptInfo);
        }
    });

    try {
        String signed = signingResult.get();

使用.setUserAuthenticationValidityDurationSeconds(5);生成密钥。属性。 sign方法使用经过身份验证的签名。但是回调从未被调用过,所以我们到了这一点(在Android 9上……)。

谢谢您的帮助!

解决方法

我最近在我的应用中实现了 androidx 生物识别库。首先,BiometricPrompt.PromptInfo.Builder.setDeviceCredentialAllowed() 已被弃用,我们现在应该改用 BiometricPrompt.PromptInfo.Builder.setAllowedAuthenticators(int)。传递给该函数的 int 参数必须是 BiometricManager.Authenticators 中的枚举值之一或其组合:

  • BIOMETRIC_STRONG
  • BIOMETRIC_WEAK
  • DEVICE_CREDENTIAL

此 Android 开发人员文档描述了不同的枚举: https://developer.android.com/reference/androidx/biometric/BiometricManager.Authenticators

来自 Android Developer docs on Biometric Authentication(顺便说一句,我建议阅读本指南):

注意:Android 10(API 级别 29)及更低版本不支持以下身份验证器类型组合:DEVICE_CREDENTIAL 和 BIOMETRIC_STRONG | DEVICE_CREDENTIAL。要在 Android 10 及更低版本上检查 PIN、图案或密码是否存在,请使用 KeyguardManager.isDeviceSecure() 方法。

下面是一小段代码,我用它来设置我的生物识别提示,希望这和上面的信息能指导您朝着正确的方向前进。

val bioPromptInfoBuilder = BiometricPrompt.PromptInfo.Builder()
    .setTitle(authPromptParams.promptTitle)
    .setSubtitle(authPromptParams.promptSubtitle)

when {
    Build.VERSION.SDK_INT > Build.VERSION_CODES.Q -> {
        bioPromptInfoBuilder.setAllowedAuthenticators(
            BiometricManager.Authenticators.BIOMETRIC_STRONG or BiometricManager.Authenticators.DEVICE_CREDENTIAL
        )
    }
    kgm.isDeviceSecure -> {
        bioPromptInfoBuilder.setAllowedAuthenticators(
            BiometricManager.Authenticators.BIOMETRIC_WEAK or
                BiometricManager.Authenticators.DEVICE_CREDENTIAL)
    }
    else -> return null
}

基本上 Android 10 及以下版本支持 BiometricManager.Authenticators 枚举的不同组合。