【问题标题】:How can I block unauthenticated user use my Cloud Functions?如何阻止未经身份验证的用户使用我的 Cloud Functions?
【发布时间】:2022-11-21 11:23:11
【问题描述】:

我阻止未经身份验证的用户使用我的 Firebase Cloud Functions 的方法是在所有函数的开头添加此样板代码。请注意,我正在使用 Callable Function functions.https.onCall不是HTTP函数functions.https.onRequest

if (!context.auth) {
    // Throwing an HttpsError so that the client gets the error details.
    throw new functions.https.HttpsError(
      "failed-precondition",
      "The function must be called while authenticated."
    );
  }

有没有更好的方法来阻止未经身份验证的用户,以便无效调用甚至不能调用我的函数?

【问题讨论】:

    标签: firebase google-cloud-functions


    【解决方案1】:

    如果您正在使用 Firebase 身份验证并且还使用自定义声明,那么使用 context auth 可能是最简单的检查方法。或者,您可以只允许 allAuthenticatedUsers 调用 Cloud Function,如下所示:

    但是,在这种情况下,您必须手动添加和验证授权标头。您可以使用 gcloud auth print-identity-token 获取您的令牌(不同于 Firebase Auth 的 ID 令牌)进行测试。

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 2019-04-04
      • 1970-01-01
      • 2020-11-15
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      • 2019-08-25
      相关资源
      最近更新 更多