question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

why jwt would generate different token between python 3.4.x and python2.7.x

See original GitHub issue

Hi ,

Thanks for you develop such a wonderful lib. Here I encountered a problem when using jwt, i notice the same code in python2.7 and python3.4 would generate a different result token, and I check the .net wersion jwt also generate the same token as the python3, because we just developing sso fuction in python2.7 django, could you share me how I can get the same token like python3.0 when using python2.7? Thanks in advance.

Python 2.7 and jwt1.3:


import sys import time import jwt
import uuid import urllib

#import urllib2.parse

payload = {

u"iat": 1436754786,
u"sub": u"sunsw@ebscn.com",
#"exp" : int(time.time()) + 10000, #optional- expiration time
#"aud": "sisense"   # optional- audiences parameter. can be either string or array of strings. Must contain the value "sisense"

}

shared_key = u"f8ab9dbb04441f985ff81985ea14a0c0" print isinstance(shared_key,unicode) jwt_string = jwt.encode(payload, shared_key, algorithm=‘HS256’) print jwt_string print (jwt.decode(jwt_string, shared_key, algorithm=‘HS256’))


Output:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0MzY3NTQ3ODYsInN1YiI6InN1bnN3QGVic2NuLmNvbSJ9.4F16dm8EyiWkIoaBQDTca45YQeY5RIhBAq7dcLKWbkk {u’iat’: 1436754786, u’sub’: u’sunsw@ebscn.com’}

Python3.4 and jwt1.3:


import sys import time import jwt
import uuid import urllib import lib3to2

#import urllib2.parse

payload = {

"iat": 1436754786,
"sub": "sunsw@ebscn.com",
#"exp" : int(time.time()) + 10000, #optional- expiration time
#"aud": "sisense"   # optional- audiences parameter. can be either string or array of strings. Must contain the value "sisense"

}

shared_key = “f8ab9dbb04441f985ff81985ea14a0c0” print (isinstance(shared_key,str)) jwt_string = jwt.encode(payload, shared_key, algorithm=‘HS256’) print (jwt_string) print (jwt.decode(jwt_string, shared_key, algorithm=‘HS256’))


Output:

b’eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE0MzY3NTQ3ODYsInN1YiI6InN1bnN3QGVic2NuLmNvbSJ9.P2-dGdFZZLZRW9xfbZt0qT_Q0QfoRIBwo2K37ysonew’ {‘iat’: 1436754786, ‘sub’: ‘sunsw@ebscn.com’}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
dmultercommented, Oct 16, 2017

Solved my own problem, need to use TOKEN.decode(‘ascii’)

0reactions
dmultercommented, Oct 16, 2017

One more question if I can… I just switched to Python 3 and see the same thing, but my question is why it’s wrapping the returned token as the returned string b’TOKEN’. Is there a way to just get the raw token back without the wrapping? My decode is failing with invalid header padding now and I want clients to just see the raw token. Hope this makes sense.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Handle JWTs in Python - Auth0
I'm going to teach you how to create a JWT because by understanding how a token is created, you'll better understand how to...
Read more >
Changelog — PyJWT 2.6.0 documentation - Read the Docs
Introduce PyJWK , PyJWKSet , and PyJWKClient . import jwt from jwt import PyJWKClient token = ...
Read more >
JWT hs512 signature slightly different from jwt.io if calculated ...
In your Python code you used Base64 encoding, but the JWT standard requires Base64URL encoding. The differences are that the characters "+" ...
Read more >
Django REST framework JWT - GitHub Pages
Unlike some more typical uses of JWTs, this module only generates authentication tokens that will verify the user who is requesting one of...
Read more >
djangorestframework-simplejwt 3.2.3 - PyPI
A minimal JSON Web Token authentication plugin for Django REST Framework. ... Python (2.7, 3.4, 3.5, 3.6) ... Other versions will probably work....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found