不使用 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 颁发的令牌时POST
ing 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 身份验证文档中并未包含此内容)