【问题标题】:How to verify a custom token?如何验证自定义令牌?
【发布时间】:2020-08-29 05:40:52
【问题描述】:

我正在为我的项目使用 firebase 身份验证和功能。我的数据库 api 使用不同的提供程序。我需要从“管理员”功能对我的数据库进行一些调用。我的服务器设置为通过以下配置验证 firebase 的 jwt 令牌(自定义验证,不能使用 firebase 管理员):

{
   "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

【问题讨论】:

  • 请编辑问题以显示您正在使用的代码不能按您预期的方式工作。
  • @DougStevenson 添加了
  • 你如何验证它?我有点期待看到有人调用 verifyIdToken
  • @DougStevenson 验证由 hasura graphql 服务器完成,无需使用 admin sdk,他们所要求的只是提供我上面发布的 json 配置。我相信验证已在此处完成(这不是 firebase 支持的语言)github.com/hasura/graphql-engine/blob/…
  • @DougStevenson 这里详细解释了他们是如何做到的github.com/hasura/graphql-engine/blob/…

标签: firebase firebase-authentication firebase-admin hasura


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在 firebase 函数上生成一个 id 令牌

    1 - REST API

    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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2019-07-04
      • 2016-08-06
      • 2021-01-10
      • 2017-12-19
      • 2017-03-10
      • 1970-01-01
      相关资源
      最近更新 更多