why jwt would generate different token between python 3.4.x and python2.7.x
See original GitHub issueHi ,
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:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Solved my own problem, need to use TOKEN.decode(‘ascii’)
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.