pyjwt: AttributeError: '_RSAPrivateKey' object has no attribute 'sign'

When using a pair of RSA keys to try the example given in the docs:

encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256')
decoded = jwt.decode(encoded, public_key, algorithms='RS256')

Expected Result

I’m expecting to get both variables assigned without problem.

Actual Result

But what happens instead is that I’m getting:

AttributeError: '_RSAPrivateKey' object has no attribute 'sign'

Reproduction Steps

import jwt
private_key = open("private.pem").read()
public_key = open("public.pem").read()
encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256')
decoded = jwt.decode(encoded, public_key, algorithms='RS256')

System Information

$ python -m jwt.help
{
  "cryptography": {
    "version": "1.3.2"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.5.1"
  },
  "platform": {
    "release": "18.0.0",
    "system": "Darwin"
  },
  "pyjwt": {
    "version": "1.6.4"
  }
}

This command is only available on PyJWT v1.6.3 and greater. Otherwise, please provide some basic information about your system.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 12
  • Comments: 16 (1 by maintainers)

Most upvoted comments

Make sure you don’t mix up your private key with your public. Always verify with your public key. I was using my private key to verify and got this message. Struggled with this for about 2 hours…