Skip to content

Commit b87bad7

Browse files
committed
refactor decode(), fix setup.py for automated sdist builds
* split decode() internals into load() and verify_signature() * pull code out of read() function in setup.py so it doesn't fail when using distutils.core.run_setup() to build an archive * the setup.py change also uses with so file closing is automatic
1 parent 3bade27 commit b87bad7

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

jwt/__init__.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ def encode(payload, key, algorithm='HS256'):
137137

138138

139139
def decode(jwt, key='', verify=True, verify_expiration=True, leeway=0):
140+
payload, signing_input, header, signature = load(jwt)
141+
142+
if verify:
143+
verify_signature(payload, signing_input, header, signature, key,
144+
verify_expiration, leeway)
145+
146+
return payload
147+
148+
149+
def load(jwt):
140150
if isinstance(jwt, unicode):
141151
jwt = jwt.encode('utf-8')
142152
try:
@@ -168,22 +178,25 @@ def decode(jwt, key='', verify=True, verify_expiration=True, leeway=0):
168178
except (TypeError, binascii.Error):
169179
raise DecodeError("Invalid crypto padding")
170180

171-
if verify:
172-
try:
173-
if isinstance(key, unicode):
174-
key = key.encode('utf-8')
175-
if header['alg'].startswith('HS'):
176-
expected = verify_methods[header['alg']](signing_input, key)
177-
if not constant_time_compare(signature, expected):
178-
raise DecodeError("Signature verification failed")
179-
else:
180-
if not verify_methods[header['alg']](signing_input, key, signature):
181-
raise DecodeError("Signature verification failed")
182-
except KeyError:
183-
raise DecodeError("Algorithm not supported")
184-
185-
if 'exp' in payload and verify_expiration:
186-
utc_timestamp = timegm(datetime.utcnow().utctimetuple())
187-
if payload['exp'] < (utc_timestamp - leeway):
188-
raise ExpiredSignature("Signature has expired")
189-
return payload
181+
return (payload, signing_input, header, signature)
182+
183+
184+
def verify_signature(payload, signing_input, header, signature, key='',
185+
verify_expiration=True, leeway=0):
186+
try:
187+
if isinstance(key, unicode):
188+
key = key.encode('utf-8')
189+
if header['alg'].startswith('HS'):
190+
expected = verify_methods[header['alg']](signing_input, key)
191+
if not constant_time_compare(signature, expected):
192+
raise DecodeError("Signature verification failed")
193+
else:
194+
if not verify_methods[header['alg']](signing_input, key, signature):
195+
raise DecodeError("Signature verification failed")
196+
except KeyError:
197+
raise DecodeError("Algorithm not supported")
198+
199+
if 'exp' in payload and verify_expiration:
200+
utc_timestamp = timegm(datetime.utcnow().utctimetuple())
201+
if payload['exp'] < (utc_timestamp - leeway):
202+
raise ExpiredSignature("Signature has expired")

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from setuptools import setup
44

55

6-
def read(fname):
7-
return open(os.path.join(os.path.dirname(__file__), fname)).read()
6+
with open(os.path.join(os.path.dirname(__file__), 'README.md')) as readme:
7+
long_description = readme.read()
8+
89

910
setup(
1011
name="PyJWT",
@@ -17,7 +18,7 @@ def read(fname):
1718
url="http://github.com/progrium/pyjwt",
1819
packages=['jwt'],
1920
scripts=['bin/jwt'],
20-
long_description=read('README.md'),
21+
long_description=long_description,
2122
classifiers=[
2223
"Development Status :: 3 - Alpha",
2324
"License :: OSI Approved :: MIT License",

0 commit comments

Comments
 (0)