我正在使用Firebase身份验证和函数来完成我的项目。我的数据库API是由另一个提供商提供的。我需要以“管理员”的身份从函数中发出一些对我的数据库的调用。我的服务器已设置为通过以下配置验证Firebase的JWT令牌(自定义验证,无法使用Firebase管理员):
这可以正确验证ID令牌,但无法解析由
{
"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中的说明发送到我的hasura服务器。
上述指南适用于id令牌,但不适用于自定义令牌。有关hasura服务器如何处理jwt验证的更详细说明,请参见https://github.com/hasura/graphql-engine/blob/dcab20a5ee388ebd754a7828de1309a3a2e0eaee/docs/graphql/manual/auth/authentication/jwt.rst#generating-jwt-config。