Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

JWT signing with private key & passphrase throws error

Open
#860 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, node.js

Research direction

Reproduce the example with Node.js v12.20.0, then trace the signing path through jsonwebtoken/sign.js, jwa/index.js, and jws/lib/sign-stream.js shown in the stack trace. The fix is complete when a passphrase-protected PEM private key can sign a PS512 token without the key.key type error, with regression coverage for this case.

Written by the indexing model from the issue text.

Description

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
Dominant language
JavaScript
Stars
18.2k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from auth0/node-jsonwebtoken

All issues in auth0/node-jsonwebtoken

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.