android - 自定义 biometricPrompt

标签 android biometrics android-9.0-pie android-fingerprint-api facial-identification

我在 android 设备中自定义 biometricPrompt 有问题。我只使用指纹授权,但一些具有 android 9.0 的设备(例如三星 Galaxy S10+)授权使用指纹(如果允许)但与面部身份验证相同。如果用户同时允许面部和指纹认证 biometricPrompt 用于认证面部识别。我只需要允许指纹,如果用户不允许指纹但面部是的,我需要阻止它。

文档告诉我这个(来自 docs)
enter image description here

...但没有任何指示,我在源代码中找不到任何关于自定义的信息。

我的启动代码身份验证对话框 在这儿

 BiometricPrompt.Builder(context)
                    .setTitle(biometricBuilder.title ?: "")
                    .setSubtitle(biometricBuilder.subtitle ?: "")
                    .setDescription(biometricBuilder.description ?: "")
                    .setNegativeButton(biometricBuilder.negativeButtonText ?: "",
                            context.mainExecutor, DialogInterface.OnClickListener { dialogInterface, i -> biometricCallback.onAuthenticationCancelled() })
                    .build()
                    .authenticate(CancellationSignal(), context.mainExecutor,
                            BiometricCallbackV28(biometricCallback))

谢谢你的帮助

最佳答案

有点晚了,但我希望这个答案可以帮助其他开发人员。
我也试图实现同样的目标,最终得到了一个简单的解决方案:

调用 .authenticate() 时提供加密对象像这样:

/**
 * Prerequisites:
 * 1. Add
 * `implementation "androidx.biometric:biometric:1.0.1"` in build.gradle
 * 2. Add
 * `    <uses-permission android:name="android.permission.USE_BIOMETRIC" android:requiredFeature="false"/>`
 * in AndroidManifest.xml
 */
object BiometricHelper {
    private const val ENCRYPTION_BLOCK_MODE = KeyProperties.BLOCK_MODE_GCM
    private const val ENCRYPTION_PADDING = KeyProperties.ENCRYPTION_PADDING_NONE
    private const val ENCRYPTION_ALGORITHM = KeyProperties.KEY_ALGORITHM_AES
    private const val KEY_SIZE = 128

    private lateinit var biometricPrompt: BiometricPrompt

    fun authenticate(fragmentActivity: FragmentActivity, authCallback: BiometricPrompt.AuthenticationCallback){
        try {
            if (!fragmentActivity.supportFragmentManager.executePendingTransactions()) {
                biometricPrompt = createBiometricPrompt(fragmentActivity, authCallback)
                val promptInfo = createPromptInfo()
                biometricPrompt.authenticate(
                    promptInfo,
                    cryptoObject //Providing crypto object here will block Iris and Face Scan
                )
            }
        }
        catch (e: KeyPermanentlyInvalidatedException) {
            e.printStackTrace()
        }
        catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun createBiometricPrompt(fragmentActivity: FragmentActivity, authCallback: BiometricPrompt.AuthenticationCallback): BiometricPrompt {
        val executor = ContextCompat.getMainExecutor(fragmentActivity)
        return BiometricPrompt(fragmentActivity,  executor, authCallback)
    }

    private fun createPromptInfo(): BiometricPrompt.PromptInfo {
        return BiometricPrompt.PromptInfo.Builder()
            .setTitle("Authentication")
            .setConfirmationRequired(false)
            .setNegativeButtonText("Cancel")
            .setDeviceCredentialAllowed(false) //Don't Allow PIN/pattern/password authentication.
            .build()
    }
    //endregion


    //====================================================================================
    //region Dummy crypto object that is used just to block Face, Iris scan
    //====================================================================================
    /**
     * Crypto object requires STRONG biometric methods, and currently Android considers only
     * FingerPrint auth is STRONG enough. Therefore, providing a crypto object while calling
     * [androidx.biometric.BiometricPrompt.authenticate] will block Face and Iris Scan methods
     */
    private val cryptoObject by lazy {
        getDummyCryptoObject()
    }

    private fun getDummyCryptoObject(): BiometricPrompt.CryptoObject {
        val transformation = "$ENCRYPTION_ALGORITHM/$ENCRYPTION_BLOCK_MODE/$ENCRYPTION_PADDING"
        val cipher = Cipher.getInstance(transformation)
        var secKey = getOrCreateSecretKey(false)
        try {
            cipher.init(Cipher.ENCRYPT_MODE, secKey)
        }
        catch (e: KeyPermanentlyInvalidatedException) {
            e.printStackTrace()
            secKey = getOrCreateSecretKey(true)
            cipher.init(Cipher.ENCRYPT_MODE, secKey)
        }
        catch (e: Exception) {
            e.printStackTrace()
        }
        return BiometricPrompt.CryptoObject(cipher)
    }

    private fun getOrCreateSecretKey(mustCreateNew: Boolean): SecretKey {
        val keyStore = KeyStore.getInstance("AndroidKeyStore")
        keyStore.load(null)
        if (!mustCreateNew) {
            keyStore.getKey("dummyKey", null)?.let { return it as SecretKey }
        }

        val paramsBuilder = KeyGenParameterSpec.Builder("dummyKey",
            KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
        paramsBuilder.apply {
            setBlockModes(ENCRYPTION_BLOCK_MODE)
            setEncryptionPaddings(ENCRYPTION_PADDING)
            setKeySize(KEY_SIZE)
            setUserAuthenticationRequired(true)
        }

        val keyGenParams = paramsBuilder.build()
        val keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,
            "AndroidKeyStore")
        keyGenerator.init(keyGenParams)
        return keyGenerator.generateKey()
    }
    //endregion
}

Gist

编辑:
仅当该设备上的面部扫描和/或虹膜扫描身份验证被视为 WEAK 方法时,此解决方案才有效。

关于android - 自定义 biometricPrompt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55352879/

相关文章:

android - 无法使用文件提供程序从内部存储打开 PDF 以便在 Android 8 和 9 上查看

Android Studio 3.1 EditText StackOverflowError

android - 如何将可绘制的矢量转换为PNG文件?

ios - 我们可以在模拟器中测试 Face ID 吗?

android - 在没有互联网的情况下从 WLAN 请求下载时,下载不会在 Android 9 上启动

android - 带 DisplayCutout 的全屏应用程序

android - 使用库项目和 android.view.InflateException 二进制 XML 文件行 #7 错误膨胀类

java - 如何将新的持续时间与存储的持续时间进行比较并替换为两者中较短的一个?

c - 用 C 语言开发 API

python - OpenCV 中用于虹膜检测的 HoughCircles 的正确用法/参数值是多少?