【问题标题】:Why can't I use `allAuthenticatedUsers` for my Firebase Cloud Function?为什么我的 Firebase 云函数不能使用 `allAuthenticatedUsers`?
【发布时间】:2021-03-26 09:20:18
【问题描述】:

在使用 Firebase CLI 部署 Firebase Functions 时,它们被配置为将 Cloud Functions Invoker 权限授予allUsers。通过这样的设置,下面的代码按预期运行。

Cloud Functions Invoker 权限也可以授予allAuthenticatedUsers。但是,当我为 addMessage 实施此更改时,我只会使用以下代码收到 UNAUTHENTICATED 错误响应。

为什么 allAuthenticatedUsers 不能用于这个 Firebase 云函数?


注意:此问答是 Furkan Yurdakul 发布的一个现已删除的问题的结果,该问题是关于为什么 allAuthenticatedUsers 没有为他的 Firebase 应用程序使用他的 Firebase 可调用函数


MWE 基于documentation,其中addMessage 定义为here

firebase.auth().signInAnonymously() // for the sake of the MWE, this will normally be Facebook, Google, etc
  .then((credential) => {
    // logged in successfully, call my function
    const addMessage = firebase.functions().httpsCallable('addMessage');
    return addMessage({ text: messageText });
  })
  .then((result) => {
    // Read result of the Cloud Function.
    const sanitizedMessage = result.data.text;
    alert('The sanitized message is: ' + sanitizedMessage);
  })
  .catch((error) => {
    // something went wrong, keeping it simple for the MWE
    const errorCode = error.code;
    const errorMessage = error.message;

    if (errorCode === 'auth/operation-not-allowed') {
      alert('You must enable Anonymous auth in the Firebase Console.');
    } else {
      console.error(error);
    }
  });

【问题讨论】:

    标签: javascript firebase firebase-authentication google-cloud-functions


    【解决方案1】:

    简单地说,如果传递给 Cloud Functions 的 ID 令牌代表一个 Google 帐户(通过 FirebaseGoogle itself 使用 Google 登录),则它有效,否则无效。

    allAuthenticatedUsers 视为allAuthenticatedGoogleUsers 而不是allAuthenticatedFirebaseUsers

    背景资料

    对于与 Firebase 客户端 SDK 一起使用的 Callable Firebase Functions,您通常会授予 allUsers 调用它的权限(默认设置 Firebase CLI 部署的函数)。

    Google Cloud Functions 的 valid authenticated client request 必须具有 Authorization: Bearer ID_TOKEN 标头(首选)或 ?access_token=ID_TOKEN。这里,ID_TOKEN 是登录的 Google 用户的 ID 令牌,为 JWT

    当 Firebase 客户端 SDK 调用 Callable Function 时,它们会使用当前用户的 ID token 为您设置 Authorization 标头(如果用户已登录,则为 here)。这样做是为了使用户的身份验证令牌可以在context parameteronCall() 函数中使用。但重要的是,Firebase 用户的 ID 令牌并不总是代表 Google 用户,这使其与 allAuthenticatedUsers 不兼容。

    因此,您必须通过检查context.auth 来在代码中设置可调用函数,它的属性如下所示。

    export const addMessage = functions.https.onCall((data, context) => {
      if (!context.auth) {
        // Throwing a HttpsError so that the client gets the error details.
        throw new functions.https.HttpsError(
          'failed-precondition',
          'The function must be called while authenticated.'
        );
      }
    
      // a valid user is logged in
    
      // do work
    });
    

    关于 403 禁止错误的附录

    如果您的函数在部署后一直抛出 403 错误,这可能是因为您使用的是 Firebase CLI 的过时副本,如 documentation 中突出显示的那样:

    警告:使用低于 7.7.0 版本的任何 Firebase CLI 部署的新 HTTP 和 HTTP 可调用函数默认是私有的,并在调用时引发 HTTP 403 错误。在部署任何新功能之前,请明确make these functions publicupdate your Firebase CLI

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 2020-01-14
      • 2020-06-19
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 2022-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多