在我的项目中,我正在使用firebase和函数。我的数据库api使用的是不同的提供者。我需要从函数"admin“调用我的数据库。我的服务器是通过以下配置(自定义验证,不能使用firebase管理)来验证firebase的jwt令牌的:
{
"type":"RS256",
"jwk_url":"https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com",
"audience":"<firebase-project-id>",
"issuer":"https://securetoken.google.com/<firebase-project-id>"
}
这将验证ID令牌的正确性,但是无法解析admin.auth().createCustomToken
创建的自定义令牌,并出现以下错误:
无法验证JWT: JWSError JWSInvalidSignature
因此,我不能使用自定义令牌来验证我的云功能,除非我能够以某种方式验证它们?
我的函数令牌就是这样生成的:
const uid = "function-worker";
const claims = {
"https://hasura.io/jwt/claims": {
"x-hasura-default-role": "function",
"x-hasura-allowed-roles": ["function"],
"x-hasura-user-id": uid,
},
};
const jwt = await admin.auth().createCustomToken(uid, claims);
生成的jwt
然后按照哈苏拉服务器发送到https://github.com/hasura/graphql-engine/tree/master/community/sample-apps/firebase-jwt
下面的指南适用于id令牌,但不适用于自定义令牌。有关哈苏拉服务器如何处理jwt验证的更详细说明,请参见https://github.com/hasura/graphql-engine/blob/dcab20a5ee388ebd754a7828de1309a3a2e0eaee/docs/graphql/manual/auth/authentication/jwt.rst#generating-jwt-config。
发布于 2020-05-13 14:15:24
您可以使用Firebase REST来生成服务器端的id令牌。https://firebase.google.com/docs/reference/rest/auth
发布于 2021-04-21 02:11:05
在firebase函数上生成id令牌
1 - REST
import fetch from 'node-fetch';
...
const customToken = await admin.auth().createCustomToken(user.uid);
const tokenURL = 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=';
const response = await fetch(tokenURL + API_KEY, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: customToken,
returnSecureToken: true
})
}).then(r => r.json());
console.log(response.idToken);
2 -服务器上的Firebase客户端
import firebase from "firebase/app";
import "firebase/auth";
admin.initializeApp();
firebase.initializeApp(firebase_config);
...
const token: any = await admin.auth().createCustomToken(user.uid)
.then((customToken: string) =>
// use custom token to get firebase token
firebase.auth().signInWithCustomToken(customToken)
.then((cred: firebase.auth.UserCredential) => cred.user?.getIdToken()))
.catch((e: string) => console.error(e));
https://stackoverflow.com/questions/61767695
复制相似问题