如何验证最终用户身份验证令牌(使用Firebase身份验证)以调用Google Cloud Run端点?

时间:2020-10-29 12:30:02

标签: node.js firebase authentication firebase-authentication google-cloud-run

请帮助使用Firebase身份验证的最终用户在云中进行身份验证。

简短说明: 我从Firebase函数提交了授权:Bearer + idToken标头,其中idToken已通过电子邮件/密码Firebase用户 user1 进行了身份验证。 我将其提交给为此 user1 设置了Cloud Run Invoker角色的Cloud Run实例。 但是在云运行日志中,我看到以下错误:

该请求未被授权调用此服务。在https://cloud.google.com/run/docs/securing/authenticating

中了解更多信息

我在做什么错?如何通过已验证用户调用的firebase函数调用云运行端点调用?


更多详细信息:

我有一个具有电子邮件/密码Firebase身份验证的简单Firebase应用程序。 当 user1 (例如test@test.com)已使用电子邮件/密码进行身份验证时,我使用firebase.User.getIdToken()获取该用户的ID令牌,并将其提交给firebase函数。

在firebase函数端,我提取用户令牌,并将其作为Authorization:Bearer + token header提交给google cloud run端点:

export const getData = functions.https.onRequest(
  async (req, response) => {
    cors(req, response, async () => {
      getToken(req)
        .then(async (token: any) => {
              const options = {
                url: cloud_run.CLOUD_RUN_ENDPOINT,
                headers: {
                  Authorization: `Bearer ${token}`,
                },
              };
           request.get(options, async function (err, resp) {
             //Processing the result
           });
    //etc
    

在google cloud run上,我已经部署了cloud run端点。 在“云运行权限”选项卡上,我已为 user1 test@test.com设置了“云运行调用者”角色。

但是当我执行上述firebase函数getData时,我在云运行日志中看到以下错误:

2020-10-29 14:30:13.498 MSK GET 401 0 B 0 ms@root+request/1.6.1节点/v10.22.0 linux / 4.4.0 Linux / x64 https:// 该请求无权调用此服务。在https://cloud.google.com/run/docs/securing/authenticating

中了解更多信息

根据本手册: https://cloud.google.com/run/docs/authenticating/end-users的 Firebase身份验证:https://cloud.google.com/run/docs/authenticating/end-users#cicp-firebase-auth

我需要实施Identity Platform或Firebase身份验证(完成)并手动验证其凭据。 如何手动验证凭据?提交不记名授权令牌后该怎么办?

1 个答案:

答案 0 :(得分:2)

要实现这一目标,您有两种解决方案:

  • 请自行检查未经身份验证的Cloud Run服务中的令牌。最近出现了很棒的Google Cloud post on this。我个人不喜欢这种解决方案,因为如果发生攻击,则取决于您的服务来管理这种高流量,而您要付费!
  • 使用代理。我(wrote an article on this)在1年前(使用API​​密钥安全性定义,但使用Firebase Auth security definition进行了更改并使用它),这是(旧的)Cloud Endpoint可以实现的。它已经很老了,因为今年夏天已经发布了名为API Gateway的新服务,即今天由Google完全管理的Cloud Endpoint(今天的功能相同,但是API Gateway会发展;不确定Cloud端点!)