简体   繁体   English

如何阻止未经身份验证的用户使用我的 Cloud Functions?

[英]How can I block unauthenticated user use my Cloud Functions?

My approach to block unauthenticated user to use my Firebase Cloud Functions is to add this boilerplate code in the beginning of all functions.我阻止未经身份验证的用户使用我的 Firebase 云功能的方法是在所有功能的开头添加此样板代码。 Please note that I am using Callable Function functions.https.onCall , NOT HTTP Function functions.https.onRequest请注意,我使用的是 Callable Function functions.https.onCall ,而不是HTTP Function 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."
    );
  }

Is there a better way to block unauthenticated user so that invalid call can NOT even invoke my function?有没有更好的方法来阻止未经身份验证的用户,以便无效调用甚至不能调用我的 function?

If you are using Firebase Authentication and also use custom claims, using context auth might be easiest way to do check that.如果您使用 Firebase 身份验证并且还使用自定义声明,则使用context身份验证可能是最简单的检查方法。 Alternatively, you can allow only allAuthenticatedUsers to invoke the Cloud Function as shown below:或者,您可以仅允许allAuthenticatedUsers调用 Cloud Function,如下所示:

在此处输入图像描述

However, in this case you'll have to add and verify that Authorization header manually.但是,在这种情况下,您必须手动添加并验证授权 header。 You can use gcloud auth print-identity-token to get your token (different that Firebase Auth's ID token) for testing.您可以使用gcloud auth print-identity-token获取您的令牌(不同于 Firebase Auth 的 ID 令牌)进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Firebase 参数化配置与 Typescript 一起用于 Cloud Functions? - How can I use Firebase Parameterized configuration with Typescript for Cloud Functions? 如何通过Cloud Functions上传文件到Cloud Storage,并使用Firestore控制对Cloud Storage的访问? - How can I upload files to Cloud Storage through Cloud Functions and use Firestore to control access to Cloud Storage? 我如何在 Python 中使用我的谷歌云 API 密钥在云功能中 - How to i user my google cloud API key in cloud functions in Python 如何从 Cloud Functions 访问和操作 Firebase 数据库? - How can I reach and manipulate Firebase Database from Cloud Functions? 如何在 Firebase Cloud Functions 中对 npm 模块使用 `import`? - How do I use `import` for npm modules in Firebase Cloud Functions? 如何在我的 Cloud-Run Flask 应用程序中获取用户的 IP 地址? - How can I get the user's IP-Address in my Cloud-Run Flask app? firebase的云函数不能使用serverTimestamp - Cloud functions for firebase can not use serverTimestamp 谷歌云函数的IP怎么改? - How can I change the IP of google cloud functions? 为什么我的 Firebase Cloud Function 不能使用“allAuthenticatedUsers”? - Why can't I use `allAuthenticatedUsers` for my Firebase Cloud Function? 使用 Cloud Functions 访问 firestore 中的用户数据,但如何安全地发送 UID/IDToken? - Accessing user data in firestore using Cloud Functions, but how do I securely send the UID/IDToken?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM