【问题标题】:PostGraphile "invalid algorithm" error with RS256 encrypted JWT toaken带有 RS256 加密 JWT 的 PostGraphile“无效算法”错误
【发布时间】:2020-08-12 21:58:12
【问题描述】:

我在节点项目中使用带有 PostGraphile 4.6.0 的 JWT 验证。这是sn-p的代码:

createServer(
postgraphile(env.DATABASE_URL, "public", {
  jwtVerifyAlgorithms: ["RS256"],
  jwtSecret: "./publickey.pem",
  jwtPgTypeIdentifier: "public.jwt_token",
  rejectUnauthorized: false,
  graphiql: true,
  enhanceGraphiql: true,
  graphqlRoute: env.POSTGRAPHILE_ROUTE + "/graphql",
  graphiqlRoute: env.POSTGRAPHILE_ROUTE + "/graphiql",
})).listen(port, () => {
console.log("Listening at port:" + port);});

但是当我使用 Postman 发送 RS256 加密的 JWT 令牌时,得到错误:

{
"errors": [
    {
        "message": "invalid algorithm"
    }
]

}

我在 Postgres 中创建了一个函数来返回 JWT 令牌,它总是返回 HS256 加密的 JWT 令牌。我在 Postman 中使用 PostGraphile 返回的 HS256 加密 JWT 令牌,JWT 令牌经过验证,GrqphQL 查询返回正常。

看来“jwtVerifyAlgorithms”选项没有生效。

有没有办法让 RS256 加密的 JWT 令牌工作?

【问题讨论】:

    标签: jwt


    【解决方案1】:

    Postgraphile documentation 声明 jwtPublicKey 是提供密钥的正确设置。现在您正在使用jwtSecret 和字符串"./publickey.pem",这将 加载文件,而是使用文件路径作为密码。 确保使用类似这样的方式自己加载文件:

    ...
    jwtPublicKey: fs.readFileSync("./secretkey.pem", "ascii")
    ...
    

    通过 pgSettings 替代

    在最坏的情况下,您还可以提供使用 jsonwebtoken npm 库的自定义 pgSettings 函数并自己验证令牌并将其写入您的设置。

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 1970-01-01
      • 2018-01-26
      • 2018-12-31
      • 2021-03-02
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多