Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

JWT signing with private key & passphrase throws error

未关闭
#860 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞

调研方向

使用 Node.js v12.20.0 重现该示例,然后沿着堆栈跟踪中显示的 jsonwebtoken/sign.js、jwa/index.js 和 jws/lib/sign-stream.js 追踪签名路径。当受密码短语保护的 PEM 私钥能够在不出现 key.key 类型错误的情况下签名 PS512 令牌,并且为此情况添加回归覆盖时,修复才算完成。

由索引模型根据 Issue 内容生成。

描述

Description

Signing jwt token with private key & passphrase results in error. Generated private key as below

ssh-keygen -t rsa -b 2048 -m PEM -f PS512.key and add passphrase

Reproduction
const jwt = require('jsonwebtoken');

const privateKey = `<copy & paste the generated private key here>`;

const token = jwt.sign(
    { test: 12 }, 
    { key: privateKey , passphrase: '...' }, {
    algorithm: 'PS512'
});

console.log(token);
Error
internal/crypto/keys.js:288
      throw new ERR_INVALID_ARG_TYPE(
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The "key.key" property must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received an instance of Object
    at prepareAsymmetricKey (internal/crypto/keys.js:288:13)
    at preparePrivateKey (internal/crypto/keys.js:308:10)
    at Sign.sign (internal/crypto/sig.js:94:46)
    at Object.sign (jwt-playground/node_modules/jwa/index.js:174:45)
    at Object.jwsSign [as sign] (jwt-playground/node_modules/jws/lib/sign-stream.js:32:24)
    at Object.module.exports [as sign] (jwt-playground/node_modules/jsonwebtoken/sign.js:204:16)
    at Object.<anonymous> (jwt-playground/pem-format-passphrase/jwt-pem-formar-passphrase.js:75:19)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32) {
  code: 'ERR_INVALID_ARG_TYPE'
}
Environment
  • Version of the library used: 8.5.1
  • Version of the Node.js: v12.20.0
  • OS: macOS Monterey - 12.6
主要语言
JavaScript
星标
18.2k
派生
1.3k
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

auth0/node-jsonwebtoken 的其他 Issue

查看 auth0/node-jsonwebtoken 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。