Firebase Cloud Functions 可调用函数是否强制执行最终用户身份验证?

Umb*_*ent 5 javascript firebase-authentication google-cloud-functions

我想知道 Firebase Cloud Functions 本身是否具有可调用函数,例如:

exports.myCallableFunction = functions.https.onCall((data, context) => { 
  //... 
});
Run Code Online (Sandbox Code Playgroud)

本身是安全的,或者如果我需要在其中实现一些代码以确保只有经过身份验证的用户调用它们才能从中获取某些内容。根据我的理解,它是通过 API 端点调用的,所以我有点担心每个人都能够调用它。

如果需要实现某些逻辑,什么逻辑可以确保它是安全的?

Dou*_*son 5

如果您希望只有经过身份验证的用户才能调用您的函数,那么可调用函数本身在这方面并不“安全”。您将需要添加代码以确保用户已通过身份验证,然后决定在未通过身份验证时要发送的内容。

exports.myCallableFunction = functions.https.onCall((data, context) => {
    if (!context.auth) {
        throw new functions.https.HttpsError("unauthenticated", "You must be authenticated");
    }

    // continue here if they are auth'd
});
Run Code Online (Sandbox Code Playgroud)

  • 支持内置身份验证强制的任何机会/时间表(而不是必须编写自定义代码)?因为在这种情况下,该函数实际上是通过未经身份验证的请求调用的;这违背了首先设置身份验证的意义(与基于 AWS Cognito 的 Lambda 身份验证不同) (3认同)