gpt4 book ai didi

android - Firebase Functions AppCheck 总是让我的设备失败

转载 作者:行者123 更新时间:2023-12-04 17:15:46 24 4
gpt4 key购买 nike

我一直在尝试将 AppCheck 与我的 Android 应用程序集成,但我似乎无法发出有效请求。

至于测试目的,我一直在使用以下代码:

安卓代码

class Application : MultiDexApplication() {

override fun onCreate() {
FirebaseApp.initializeApp(this)

val appCheck = FirebaseAppCheck.getInstance()
if (BuildConfig.DEBUG) appCheck.installAppCheckProviderFactory(DebugAppCheckProviderFactory.getInstance(), true)
else appCheck.installAppCheckProviderFactory(SafetyNetAppCheckProviderFactory.getInstance(), true)

super.onCreate()
}
}

class TestActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
val data = "This is a test!"
Firebase.functions.getHttpsCallable("test").call(data).addOnCompleteListener {
if (it.isSuccessful) {
val result = it.result.data
print(result)
}

else {
val exception = it.exception
print(exception)
}
}
}
}

函数代码

const functions = require("firebase-functions")

exports.test = functions.https.onCall((data, context) => {
if (context.app == undefined) {
functions.logger.error("Test - Failed validating device integrity!")
throw new functions.https.HttpsError("failed-precondition", "The function must be called from an App Check verified app")
}

return data
})

我已将 DebugAppCheckProviderFactory token 添加到 Firebase 控制台,但无论我做什么,如果它是模拟器或物理设备,当我调用该函数时,失败前提条件 总是抛出异常。

检查功能日志,我发现该应用程序丢失了:

enter image description here

我已经多次阅读文档,我似乎不能遗漏任何步骤。我是不是遗漏了什么,或者我能做些什么来找到造成这种情况的根本原因?

感谢您的宝贵时间!

编辑:

根据 Martin 的建议,我创建了一个新的 OnRequest 函数并添加了 X-Firebase-AppCheck header 。我正确地收到了 token ,并且能够通过以下方式成功验证它:

firebaseAdmin.appCheck().verifyToken(appCheckToken)

所以,我的猜测是 Android 没有像它应该的那样自动将 X-Firebase-AppCheck 添加到 OnCall 函数。

我运行代码并在代码中设置了一些断点,并注意到以下内容。 Firebase 的 call 方法正在添加 Firebase-Instance-ID-Token 但我似乎找不到 X-Firebase-AppCheck任何地方的标题。也许我不应该看到这个值,或者我只是找不到添加它的位置。或者它可能根本没有添加,因此我根本无法验证我的 context.app

最佳答案

可能需要获得 AppCheckToken :

FirebaseAppCheck
.getInstance()
.getAppCheckToken(false)
.addOnSuccessListener(new OnSuccessListener<AppCheckToken>() {
@Override
public void onSuccess(@NonNull AppCheckToken tokenResponse) {

String appCheckToken = tokenResponse.getToken();
...
}
});

必须与 HTTP 请求一起传递(示例显示 Retrofit,它也可以用来代替 HttpsCallable:

@GET("yourExampleEndpoint")
Call<List<String>> exampleData(
@Header("X-Firebase-AppCheck") String appCheckToken,
...
);

在哪里 FirebaseFunctions.getInstance().getHttpsCallable().call() 没有说明如何或是否必须明确设置 X-Firebase-AppCheck header 。 Cloud Functions 通常应该收到 X-Firebase-AppCheck header - 带有先前检索到的 AppCheck token :

const appCheckToken = req.header('X-Firebase-AppCheck');

if (!appCheckToken) {
res.status(401);
return next('Unauthorized');
}

try {
const appCheckClaims = await firebaseAdmin.appCheck().verifyToken(appCheckToken);
// If verifyToken() succeeds, continue with the next middleware function in the stack.
return next();
} catch (err) {
res.status(401);
return next('Unauthorized');
}

人们也可以发行自己的代币......和checking for context-app 也是有效的。


更新:Protocol specification for https.onCall阅读:

Optional: X-Firebase-AppCheck: <token>

The Firebase App Check token provided by the client app making the request. The backend automatically verifies this token and decodes it, injecting the appId in the handler's context. If the token cannot be verified, the request is rejected.

Available for SDK >=3.14.0

安装所需的最低 NodeJS 依赖项:

npm install firebase-functions@">=3.14.0"
npm install firebase-admin@">=9.8.0"

最后但并非最不重要的一点……甚至还有一个调试助手:

dependencies {
debugImplementation "com.google.firebase:firebase-appcheck-debug:16.0.0-beta02"
}

关于android - Firebase Functions AppCheck 总是让我的设备失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68771904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com