如何在多租户设置中生成 firebase 自定义令牌

2024-05-24

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

我像这样初始化

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 https://firebase.google.com/docs/reference/rest/auth#section-verify-custom-token.

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

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

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

Specified tenant ID does not match the custom token

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

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


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

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在有效负载中。

现在,当将自定义令牌交换为 firebase 颁发的令牌时POSTing to

`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 https://cloud.google.com/identity-platform/docs/reference/rest/client/#section-verify-custom-token(但在撰写本文时原始 firebase 身份验证文档中并未包含此内容)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在多租户设置中生成 firebase 自定义令牌 的相关文章

随机推荐