-
Notifications
You must be signed in to change notification settings - Fork 256
Implement RFC-7797 / JWS (Detached Payload) #166 #272
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
Open
finvu
wants to merge
1
commit into
mpdavis:master
Choose a base branch
from
finvu:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -45,17 +45,68 @@ def sign(payload, key, headers=None, algorithm=ALGORITHMS.HS256): | |||||
| return signed_output | ||||||
|
|
||||||
|
|
||||||
| def verify(token, key, algorithms, verify=True): | ||||||
| def sign_detached(payload, key, headers=None, algorithm=ALGORITHMS.HS256): | ||||||
| """Signs a claims set and returns a JWS as a detached payload string, as per RFC7797 | ||||||
|
|
||||||
| Args: | ||||||
| payload (str or dict): A string to sign | ||||||
| key (str or dict): The key to use for signing the claim set. Can be | ||||||
| individual JWK or JWK set. | ||||||
| headers (dict, optional): A set of headers that will be added to | ||||||
| the default headers. Any headers that are added as additional | ||||||
| headers will override the default headers. | ||||||
| if the signature needs to be generated on encoded payload, then | ||||||
| header has to contain {"b64":True} | ||||||
| algorithm (str, optional): The algorithm to use for signing the | ||||||
| the claims. Defaults to HS256. | ||||||
|
|
||||||
| Returns: | ||||||
| str: The string representation of the header, and signature in detached jws format | ||||||
| payload: the payload as received in the request or encoed if {"b4":True} header is passed in the call | ||||||
|
|
||||||
| Raises: | ||||||
| JWSError: If there is an error signing the token. | ||||||
|
|
||||||
| Examples: | ||||||
|
|
||||||
| >>> jws.sign_detached({'a': 'b'}, 'secret', algorithm='HS256') | ||||||
| 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8', {'a': 'b'} | ||||||
|
|
||||||
|
|
||||||
| >>> jws.sign_detached({'a': 'b'}, 'secret', {"b64": True}, algorithm='HS256') | ||||||
| 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8', eyJhIjoiYiJ9 | ||||||
|
|
||||||
| """ | ||||||
|
|
||||||
| if algorithm not in ALGORITHMS.SUPPORTED: | ||||||
| raise JWSError("Algorithm %s not supported." % algorithm) | ||||||
|
|
||||||
| if headers: | ||||||
| if "b64" in headers and headers["b64"] is True: | ||||||
| payload = _encode_payload(payload) | ||||||
| headers.update({"crit": ["b64"]}) | ||||||
| else: | ||||||
| headers = {"b64": "false"} | ||||||
|
|
||||||
| encoded_header = _encode_header(algorithm, additional_headers=headers) | ||||||
| signed_output = _sign_header_and_claims(encoded_header, payload, algorithm, key, True) | ||||||
|
|
||||||
| return signed_output, payload | ||||||
|
|
||||||
|
|
||||||
| def verify(token, key, algorithms=None, verify=True, payload=None): | ||||||
| """Verifies a JWS string's signature. | ||||||
|
|
||||||
| Args: | ||||||
| token (str): A signed JWS to be verified. | ||||||
| key (str or dict): A key to attempt to verify the payload with. Can be | ||||||
| individual JWK or JWK set. | ||||||
| algorithms (str or list): Valid algorithms that should be used to verify the JWS. | ||||||
| payload (str or dict): Unencoded payload if the token is a detached jws | ||||||
|
|
||||||
| Returns: | ||||||
| str: The str representation of the payload, assuming the signature is valid. | ||||||
| If the token is a detached jws with "b64" true in the header, the return value will be encoded payload | ||||||
|
|
||||||
| Raises: | ||||||
| JWSError: If there is an exception verifying a token. | ||||||
|
|
@@ -65,9 +116,12 @@ def verify(token, key, algorithms, verify=True): | |||||
| >>> token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8' | ||||||
| >>> jws.verify(token, 'secret', algorithms='HS256') | ||||||
|
|
||||||
| >>> token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8' | ||||||
| >>> jws.verify(token, 'secret', algorithms='HS256', payload={"a":"b"}) | ||||||
|
|
||||||
| """ | ||||||
|
|
||||||
| header, payload, signing_input, signature = _load(token) | ||||||
| header, payload, signing_input, signature = _load(token, payload) | ||||||
|
|
||||||
| if verify: | ||||||
| _verify_signature(signing_input, header, signature, key, algorithms) | ||||||
|
|
@@ -126,7 +180,7 @@ def get_unverified_claims(token): | |||||
|
|
||||||
|
|
||||||
| def _encode_header(algorithm, additional_headers=None): | ||||||
| header = {"typ": "JWT", "alg": algorithm} | ||||||
| header = {"typ": "JOSE", "alg": algorithm} | ||||||
|
|
||||||
| if additional_headers: | ||||||
| header.update(additional_headers) | ||||||
|
|
@@ -153,7 +207,7 @@ def _encode_payload(payload): | |||||
| return base64url_encode(payload) | ||||||
|
|
||||||
|
|
||||||
| def _sign_header_and_claims(encoded_header, encoded_claims, algorithm, key): | ||||||
| def _sign_header_and_claims(encoded_header, encoded_claims, algorithm, key, is_detached=False): | ||||||
| signing_input = b".".join([encoded_header, encoded_claims]) | ||||||
| try: | ||||||
| if not isinstance(key, Key): | ||||||
|
|
@@ -164,12 +218,15 @@ def _sign_header_and_claims(encoded_header, encoded_claims, algorithm, key): | |||||
|
|
||||||
| encoded_signature = base64url_encode(signature) | ||||||
|
|
||||||
| encoded_string = b".".join([encoded_header, encoded_claims, encoded_signature]) | ||||||
| if is_detached: | ||||||
| encoded_string = b"..".join([encoded_header, encoded_signature]) | ||||||
| else: | ||||||
| encoded_string = b".".join([encoded_header, encoded_claims, encoded_signature]) | ||||||
|
|
||||||
| return encoded_string.decode("utf-8") | ||||||
|
|
||||||
|
|
||||||
| def _load(jwt): | ||||||
| def _load(jwt, payload=None): | ||||||
| if isinstance(jwt, str): | ||||||
| jwt = jwt.encode("utf-8") | ||||||
| try: | ||||||
|
|
@@ -189,10 +246,15 @@ def _load(jwt): | |||||
| if not isinstance(header, Mapping): | ||||||
| raise JWSError("Invalid header string: must be a json object") | ||||||
|
|
||||||
| try: | ||||||
| payload = base64url_decode(claims_segment) | ||||||
| except (TypeError, binascii.Error): | ||||||
| raise JWSError("Invalid payload padding") | ||||||
| if not payload: | ||||||
| try: | ||||||
| payload = base64url_decode(claims_segment) | ||||||
| except (TypeError, binascii.Error): | ||||||
| raise JWSError("Invalid payload padding") | ||||||
| else: | ||||||
| if "b64" in header and header["b64"] is True: | ||||||
| payload = _encode_payload(payload) | ||||||
| signing_input = b"".join([signing_input, payload]) | ||||||
|
||||||
| signing_input = b"".join([signing_input, payload]) | |
| signing_input = b".".join([signing_input, payload]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are typographical errors in the sign_detached() docstring; 'encoed' should be 'encoded' and 'b4' should be 'b64'.