首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何验证自定义令牌?

如何验证自定义令牌?
EN

Stack Overflow用户
提问于 2020-05-13 06:21:44
回答 2查看 2.9K关注 0票数 1

在我的项目中,我正在使用firebase和函数。我的数据库api使用的是不同的提供者。我需要从函数"admin“调用我的数据库。我的服务器是通过以下配置(自定义验证,不能使用firebase管理)来验证firebase的jwt令牌的:

代码语言:javascript
运行
复制
{
   "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

因此,我不能使用自定义令牌来验证我的云功能,除非我能够以某种方式验证它们?

我的函数令牌就是这样生成的:

代码语言:javascript
运行
复制
  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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-13 14:15:24

您可以使用Firebase REST来生成服务器端的id令牌。https://firebase.google.com/docs/reference/rest/auth

票数 6
EN

Stack Overflow用户

发布于 2021-04-21 02:11:05

在firebase函数上生成id令牌

1 - REST

代码语言:javascript
运行
复制
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客户端

代码语言:javascript
运行
复制
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));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61767695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档