且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么在通过 Firebase 身份验证传递 id 令牌时,此 CORS 身份验证的 Google Cloud 函数会返回 403?

更新时间:2023-12-05 19:56:52

将 Firebase 身份验证与 Google Cloud 功能结合使用时,用户不会通过 IAM 角色进行身份验证.相反,它们在函数本身内进行身份验证.请注意,根据 Google 的文档,您将为未经身份验证的请求付费,因为该功能必须执行验证令牌的工作."要进行设置:

When using Firebase Authentication with Google Cloud functions, users are not authenticated via IAM roles. Instead, they are authenticated within the function itself. Note, per Google's docs, "you will be billed for unauthenticated requests since the function must do work to validate the token." To set this up:

(1) 函数上Cloud Functions Invoker"的 IAM 权限需要向 allUsers 开放(而不是我拥有的 allAuthenticatedUsers).

(1) The IAM permissions for "Cloud Functions Invoker" on the function need to be opened up to allUsers (as opposed to allAuthenticatedUsers which I had).

(2) 然后使用 firebase_admin 的身份验证库手动完成令牌验证,(docs,供参考和其他语言)像这样:

(2) The token verification is then done manually using firebase_admin's auth library, (docs, for reference & other languages) like so:

from firebase_admin import auth

def your_function(request):
    decoded_token = auth.verify_id_token(id_token)
    uid = decoded_token['uid']

感谢 Juancki 为我指明了正确的方向.

Thanks to Juancki for pointing me in the right direction.