【问题标题】:Swagger definition for firebase authenticationFirebase 身份验证的 Swagger 定义
【发布时间】:2026-02-15 04:15:01
【问题描述】:

谁能提供一个用于 Firebase 身份验证的 Swagger 安全定义的工作示例?

在后端,使用 firebase admin SDK 验证 firebase ID 令牌:

import * as admin from 'firebase-admin';

await admin.auth().verifyIdToken(idToken);

Swagger 安全定义中的值应该是什么才能为 firebase 获取正确的 ID 令牌?

"securityDefinitions": {
        "firebase": {
            "authorizationUrl": "https://accounts.google.com/o/oauth2/v2/auth",
            "flow": "implicit",
            "type": "oauth2",
            "x-google-issuer": "https://securetoken.google.com/MY-PROJECT-ID",
            "x-google-jwks_uri": "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com",
            "x-google-audiences": "MY-CLIENT-ID",
            "scopes": {
                "https://www.googleapis.com/auth/firebase": "Firebase scope"
            }
        }
    }

我确实得到了一个令牌,但是,firebase admin SDK 说它无效:

解码 Firebase ID 令牌失败。确保你通过了整个 代表 ID 令牌的字符串 JWT

不确定这是不是因为错误的范围或令牌类型...

【问题讨论】:

  • 你的配置好像没问题,你是用 user.getIdToken() 方法获取不同于经典 google Oauth 令牌的 firebase 令牌吗?

标签: firebase google-cloud-platform firebase-authentication jwt swagger


【解决方案1】:

使用 Firebase 对用户进行身份验证/Configuring your OpenAPI document 对此进行了说明。
给定的示例肯定没有任何 authorizationUrlscopes 部分:

securityDefinitions:
  firebase:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    # Replace YOUR-PROJECT-ID with your project ID
    x-google-issuer: "https://securetoken.google.com/YOUR-PROJECT-ID"
    x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"
    x-google-audiences: "YOUR-PROJECT-ID"

这个firebase 安全定义也需要添加到security 部分,在API 或方法级别:

security:
  - firebase: []

阅读troubleshooting JWT validation可能会有所帮助。

【讨论】:

    【解决方案2】:

    我把这个放在这里仅供参考

    https://github.com/swagger-api/swagger-ui/pull/7699

    我已经启动了一个 PR,它将启用 swagger 使用的登录/弹出窗口

    使用执行 firebase 特定逻辑的自定义扩展插件插入

    【讨论】: