Skip to content

Commit a7b79ae

Browse files
authored
Merge pull request #8 from jay0lee/master
Add headers parameter to generate()
2 parents 4da5380 + 2f41417 commit a7b79ae

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

adafruit_jwt.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def validate(jwt):
7777
# Attempt to decode JOSE header
7878
try:
7979
jose_header = STRING_TOOLS.urlsafe_b64decode(jwt.split(".")[0])
80-
except UnicodeError:
81-
raise UnicodeError("Unable to decode JOSE header.")
80+
except UnicodeError as unicode_error:
81+
raise UnicodeError("Unable to decode JOSE header.") from unicode_error
8282
# Check for typ and alg in decoded JOSE header
8383
if "typ" not in jose_header:
8484
raise TypeError("JOSE Header does not contain required type key.")
@@ -87,17 +87,19 @@ def validate(jwt):
8787
# Attempt to decode claim set
8888
try:
8989
claims = json.loads(STRING_TOOLS.urlsafe_b64decode(jwt.split(".")[1]))
90-
except UnicodeError:
91-
raise UnicodeError("Invalid claims encoding.")
90+
except UnicodeError as unicode_error:
91+
raise UnicodeError("Invalid claims encoding.") from unicode_error
9292
if not hasattr(claims, "keys"):
9393
raise TypeError("Provided claims is not a JSON dict. object")
9494
return (jose_header, claims)
9595

9696
@staticmethod
97-
def generate(claims, private_key_data=None, algo=None):
97+
def generate(claims, private_key_data=None, algo=None, headers=None):
9898
"""Generates and returns a new JSON Web Token.
9999
:param dict claims: JWT claims set
100100
:param str private_key_data: Decoded RSA private key data.
101+
:param str algo: algorithm to be used. One of None, RS256, RS384 or RS512.
102+
:param dict headers: additional headers for the claim.
101103
:rtype: str
102104
"""
103105
# Allow for unencrypted JWTs
@@ -108,6 +110,8 @@ def generate(claims, private_key_data=None, algo=None):
108110
# Create the JOSE Header
109111
# https://tools.ietf.org/html/rfc7519#section-5
110112
jose_header = {"typ": "JWT", "alg": algo}
113+
if headers:
114+
jose_header.update(headers)
111115
payload = "{}.{}".format(
112116
STRING_TOOLS.urlsafe_b64encode(json.dumps(jose_header).encode("utf-8")),
113117
STRING_TOOLS.urlsafe_b64encode(json.dumps(claims).encode("utf-8")),
@@ -139,8 +143,7 @@ def generate(claims, private_key_data=None, algo=None):
139143

140144
# pylint: disable=invalid-name
141145
class STRING_TOOLS:
142-
"""Tools and helpers for URL-safe string encoding.
143-
"""
146+
"""Tools and helpers for URL-safe string encoding."""
144147

145148
# Some strings for ctype-style character classification
146149
whitespace = " \t\n\r\v\f"
@@ -179,8 +182,10 @@ def _bytes_from_decode_data(str_data):
179182
if isinstance(str_data, str):
180183
try:
181184
return str_data.encode("ascii")
182-
except:
183-
raise ValueError("string argument should contain only ASCII characters")
185+
except BaseException as error:
186+
raise ValueError(
187+
"string argument should contain only ASCII characters"
188+
) from error
184189
elif isinstance(str_data, bit_types):
185190
return str_data
186191
else:

0 commit comments

Comments
 (0)