Skip to content

Commit 78609d7

Browse files
author
Michael Davis
committed
Add methods to get unverified data
1 parent ba72b14 commit 78609d7

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

jose/jws.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,38 @@ def verify(token, key, algorithms, verify=True):
7676
return claims
7777

7878

79+
def get_unverified_headers(token):
80+
"""Returns the decoded headers without verification of any kind.
81+
82+
Args:
83+
token (str): A signed JWS to decode the headers from.
84+
85+
Returns:
86+
dict: The dict representation of the token headers.
87+
88+
Raises:
89+
JWSError: If there is an exception decoding the token.
90+
"""
91+
header, claims, signing_input, signature = _load(token)
92+
return header
93+
94+
95+
def get_unverified_claims(token):
96+
"""Returns the decoded claims without verification of any kind.
97+
98+
Args:
99+
token (str): A signed JWS to decode the headers from.
100+
101+
Returns:
102+
dict: The dict representation of the token claims.
103+
104+
Raises:
105+
JWSError: If there is an exception decoding the token.
106+
"""
107+
header, claims, signing_input, signature = _load(token)
108+
return claims
109+
110+
79111
def _encode_header(algorithm, additional_headers=None):
80112
header = {
81113
"typ": "JWT",

jose/jwt.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,46 @@ def decode(token, key, algorithms=None, options=None, audience=None, issuer=None
115115
return token_info
116116

117117

118+
def get_unverified_headers(token):
119+
"""Returns the decoded headers without verification of any kind.
120+
121+
Args:
122+
token (str): A signed JWT to decode the headers from.
123+
124+
Returns:
125+
dict: The dict representation of the token headers.
126+
127+
Raises:
128+
JWTError: If there is an exception decoding the token.
129+
"""
130+
try:
131+
headers = jws.get_unverified_headers(token)
132+
except:
133+
raise JWTError('Error decoding token headers.')
134+
135+
return headers
136+
137+
138+
def get_unverified_claims(token):
139+
"""Returns the decoded claims without verification of any kind.
140+
141+
Args:
142+
token (str): A signed JWT to decode the headers from.
143+
144+
Returns:
145+
dict: The dict representation of the token claims.
146+
147+
Raises:
148+
JWTError: If there is an exception decoding the token.
149+
"""
150+
try:
151+
claims = jws.get_unverified_claims(token)
152+
except:
153+
raise JWTError('Error decoding token claims.')
154+
155+
return claims
156+
157+
118158
def _validate_iat(claims):
119159
"""Validates that the 'iat' claim is valid.
120160

0 commit comments

Comments
 (0)