Skip to content

Commit b4b871f

Browse files
author
Michael Davis
committed
Merge pull request #18 from 0x64746b/feature/return_unverified_claims_as_dict
Make `get_unverified_claims()` return a dict
2 parents ff5bf5c + 4545536 commit b4b871f

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

jose/jws.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def get_unverified_claims(token):
118118
token (str): A signed JWS to decode the headers from.
119119
120120
Returns:
121-
dict: The dict representation of the token claims.
121+
str: The str representation of the token claims.
122122
123123
Raises:
124124
JWSError: If there is an exception decoding the token.

jose/jwt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ def get_unverified_claims(token):
184184
except:
185185
raise JWTError('Error decoding token claims.')
186186

187+
try:
188+
claims = json.loads(claims.decode('utf-8'))
189+
except ValueError as e:
190+
raise JWTError('Invalid claims string: %s' % e)
191+
192+
if not isinstance(claims, Mapping):
193+
raise JWTError('Invalid claims string: must be a json object')
194+
187195
return claims
188196

189197

tests/test_jwt.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,17 @@ def test_jti_invalid(self, key):
402402
token = jwt.encode(claims, key)
403403
with pytest.raises(JWTError):
404404
jwt.decode(token, key)
405+
406+
def test_unverified_claims_string(self):
407+
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.aW52YWxpZCBjbGFpbQ.iOJ5SiNfaNO_pa2J4Umtb3b3zmk5C18-mhTCVNsjnck'
408+
with pytest.raises(JWTError):
409+
jwt.get_unverified_claims(token)
410+
411+
def test_unverified_claims_list(self):
412+
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.WyJpbnZhbGlkIiwgImNsYWltcyJd.nZvw_Rt1FfUPb5OiVbrSYZGtWSE5c-gdJ6nQnTTBkYo'
413+
with pytest.raises(JWTError):
414+
jwt.get_unverified_claims(token)
415+
416+
def test_unverified_claims_object(self, claims, key):
417+
token = jwt.encode(claims, key)
418+
assert jwt.get_unverified_claims(token) == claims

0 commit comments

Comments
 (0)