Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unencoded payload option and move JWT type out of JWS #210

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions jose/jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from jose.utils import base64url_decode


def sign(payload, key, headers=None, algorithm=ALGORITHMS.HS256):
def sign(payload, key, headers=None, algorithm=ALGORITHMS.HS256, unencoded=False):
"""Signs a claims set and returns a JWS string.

Args:
Expand All @@ -29,6 +29,8 @@ def sign(payload, key, headers=None, algorithm=ALGORITHMS.HS256):
headers will override the default headers.
algorithm (str, optional): The algorithm to use for signing the
the claims. Defaults to HS256.
unencoded (boolean, optional): If True, the payload is not wrapped
in base64url encoding.

Returns:
str: The string representation of the header, claims, and signature.
Expand All @@ -47,7 +49,7 @@ def sign(payload, key, headers=None, algorithm=ALGORITHMS.HS256):
raise JWSError('Algorithm %s not supported.' % algorithm)

encoded_header = _encode_header(algorithm, additional_headers=headers)
encoded_payload = _encode_payload(payload)
encoded_payload = _encode_payload(payload, unencoded)
signed_output = _sign_header_and_claims(encoded_header, encoded_payload, algorithm, key)

return signed_output
Expand Down Expand Up @@ -135,7 +137,6 @@ def get_unverified_claims(token):

def _encode_header(algorithm, additional_headers=None):
header = {
"typ": "JWT",
"alg": algorithm
}

Expand All @@ -151,7 +152,7 @@ def _encode_header(algorithm, additional_headers=None):
return base64url_encode(json_header)


def _encode_payload(payload):
def _encode_payload(payload, unencoded):
if isinstance(payload, Mapping):
try:
payload = json.dumps(
Expand All @@ -160,8 +161,10 @@ def _encode_payload(payload):
).encode('utf-8')
except ValueError:
pass

return base64url_encode(payload)
if unencoded:
return payload
else:
return base64url_encode(payload)


def _sign_header_and_claims(encoded_header, encoded_claims, algorithm, key):
Expand Down
7 changes: 7 additions & 0 deletions jose/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ def encode(claims, key, algorithm=ALGORITHMS.HS256, headers=None, access_token=N
claims['at_hash'] = calculate_at_hash(access_token,
ALGORITHMS.HASHES[algorithm])

# if a type isn't passed in, set it here
if not headers:
headers = {
'typ': 'JWT'
}
elif 'typ' not in headers:
headers['typ'] = 'JWT'
return jws.sign(claims, key, headers=headers, algorithm=algorithm)


Expand Down
3 changes: 1 addition & 2 deletions tests/test_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ def test_add_headers(self, payload):

expected_headers = {
'test': 'header',
'alg': 'HS256',
'typ': 'JWT',
'alg': 'HS256'
}

token = jws.sign(payload, 'secret', headers=additional_headers)
Expand Down