【问题标题】:How to generate firebase custom tokens in multi tenant setup如何在多租户设置中生成 Firebase 自定义令牌
【发布时间】:2023-04-04 04:55:01
【问题描述】:

我一直在尝试为我的应用程序添加对多租户的支持。

我是这样初始化的

const app = firebase.initializeApp();
const tenantManager = app.auth().tenantManager();
const tenant = await tenantManager.createTenant({ displayName: `test- tenant` });
const auth = tenantManager.authForTenant(tenantId);

我的应用程序的一部分然后使用auth.createCustomToken(uid) 来创建一个令牌,然后可以将其交换为标准 id 令牌(使用其余端点 /accounts:signInWithCustomToken

尝试创建自定义令牌时出现以下错误

Error: This operation is not supported in a multi-tenant context

此外,手动创建令牌(使用jsonwebtoken 和服务帐户密钥)时会出现错误

Specified tenant ID does not match the custom token

在尝试验证令牌时出现(通过 REST API)

是否有其他人遇到此错误,或者是否有人知道在多租户环境中生成和验证自定义令牌的另一种方法(或者,知道某种方法可以仅在给定 uid 的情况下让用户登录)?

【问题讨论】:

  • 你是通过什么方式指定谷歌服务帐号的?
  • 通过 GOOGLE_ACCOUNT_CREDENTIALS 环境变量

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


【解决方案1】:

不要使用 API 生成自定义令牌,而是使用服务帐户中的 private_key 生成 JWT 以进行签名并确保您具有下面定义的值

const jwt = require(`jsonwebtoken`);
const payload = {
    uid,
    sub: serviceAccount.client_email,
    tenant_id: tenantId
};
jwt.sign(payload, serviceAccount.private_key, {
    audience: `https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit`,
    issuer: serviceAccount.client_email,
    algorithm: `RS256`,
    expiresIn: 0
});

注意:负载中的tenant_id

现在,在将自定义令牌交换为由 POSTing 发布的 firebase 令牌时

`https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=${encodeURIComponent(webApiKey)}`

确保 tenantId 是请求 JSON 正文中的一个属性,并且与令牌中的 tenant_id 匹配。

{
    tenantId, // Make sure this matches the "tenant_id" claim in the idToken
    token: idToken,
    returnSecureToken: true
}

第二部分记录在 https://cloud.google.com/identity-platform/docs/reference/rest/client/#section-verify-custom-token (但在撰写本文时不在原始的 firebase auth 文档中)

【讨论】:

    【解决方案2】:

    目前在多租户上下文中不支持自定义令牌身份验证。这个功能仍然是under construction。您可以查看支持功能的完整列表here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      • 2017-04-25
      • 2012-12-20
      • 2021-10-10
      • 2020-06-23
      相关资源
      最近更新 更多