【问题标题】:How to verify end user authenticated token (Firebase auth used) to invoke google cloud run endpoint?如何验证最终用户身份验证令牌(使用 Firebase 身份验证)以调用谷歌云运行端点?
【发布时间】:2021-02-11 21:34:53
【问题描述】:

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

简短说明: 我提交 Authorization: Bearer + idToken header from firebase function with idToken of authenticated with email/password firebase user user1。 我将它提交到云运行实例,并为此 user1 设置了 Cloud Run Invoker 角色。 但在云运行日志中,我看到以下错误:

请求未被授权调用此服务。阅读更多https://cloud.google.com/run/docs/securing/authenticating

我做错了什么?如何从经过身份验证的用户调用的 firebase 函数调用云运行端点调用?


更多细节:

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

在firebase功能方面,我提取用户令牌并将其作为授权提交到谷歌云运行端点:承载+令牌头:

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
    

在谷歌云运行中,我已经部署了云运行端点。 在云运行权限选项卡上,我为我的 user1 test@test.com 设置了 Cloud Run Invoker 角色。

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

2020-10-29 14:30:13.498 MSK GET 401 0 B 0 ms@root+request/1.6.1 node/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 身份验证(已完成)并手动验证其凭据。 如何手动验证凭据?提交不记名授权令牌后该怎么办?

【问题讨论】:

  • 您不能使用 Firebase 身份验证凭据来验证对受 Google Cloud IAM 保护的 Cloud Run 的请求。您必须提供 IAM 可识别的身份,这通常意味着服务帐户。如果您想使用 Firebase 身份验证,则必须允许任何人调用端点,并在后端代码中使用 Firebase Admin SDK 检查身份验证凭据。

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


【解决方案1】:

要实现这一点,您有 2 个解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    相关资源
    最近更新 更多