Firebase 电话身份验证 - 应用验证(SafetyNet)

时间:2021-01-05 08:55:08

标签: android firebase-authentication safetynet

我想使用 SafetyNet 验证电话号码 Firebase,而不是 reCAPTCHA 验证。我正在关注此处的 Firebase 文档:https://firebase.google.com/docs/auth/android/phone-auth

  • 在 Google API 控制台中,我启用了 Android 设备验证 API。
  • 在 Firebase 控制台中,我添加了 SHA-256 指纹。
  • 重新安装 google-service.json 并将其添加到项目中。

它总是将我重定向到一个网页以验证我不是机器人。我尝试删除

implementation 'androidx.browser:browser:1.3.0'

但是应用程序崩溃了。

这是我用于 SafetyNet 的代码,它总是成功,但随后会显示网页。我想知道如何防止应用程序总是重定向到 reCAPTCHA 验证,尽管 attest 功能工作正常。

private fun checkSafetyNet() {
        if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)
            == ConnectionResult.SUCCESS
        ) {
            val nonce =
                (getString(R.string.app_name) + Random.nextInt(100) + getString(R.string.otp_verification)).toByteArray()
            SafetyNet.getClient(this.requireActivity())
                .attest(nonce, API_KEY)
                .addOnSuccessListener {
                    // Indicates communication with the service was successful
                    setFirebasePhoneVerificationCallbacks()
                    startPhoneNumberVerification()
                }.addOnFailureListener { e ->
                    // An error occurred while communicating with the service.
                    if (e is ApiException) {
                        // An error with the Google Play services API contains some
                        // additional details.
                        val apiException = e as ApiException
                        Log.i("SAFETYERROR", apiException.message.toString())
                        // You can retrieve the status code using the
                        // apiException.statusCode property.
                    } else {
                        // A different, unknown type of error occurred.
                        Log.d("SafetyNetError", "Error: " + e.message)
                    }
                }

        } else {
            // Prompt user to update Google Play services.
        }
    }

1 个答案:

答案 0 :(得分:2)

根据 firebase,只会触发 reCAPTCHA 流程:

  1. SafetyNet 不可用时。

  2. 如果用户没有 Google Play 服务支持或当您在模拟器上测试您的应用时。

  3. 您的设备未通过可疑检查。

如果您的设备已 root 或设备引导加载程序处于 UNLOCKED 状态,则由于第 3 种情况,应用 reCAPTCHA 验证将可见。

检查您的应用日志中是否有“SafetyNet”标签,并确认您的设备引导加载程序是否已锁定或解锁

在解锁引导加载程序的情况下,safetyNet 会像截图中提到的那样给出建议 LOCK_BOOTLOADER。日志示例:

“SafetyNetAttestationVerifier E 没有通过 SafetyNet AttestationResponse。”

“SafetyNetAttestationVerifier E 无法解析 SafetyNet AttestationResponse”

“SafetyNetAttestationVerifier E SafetyNet Attestation 无法实现基本完整性。”

“SafetyNetAttestationVerifier E SafetyNet Attestation 有建议:XXXXXXXX”

In case of unlocked bootloader