-
-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support non-x-www-form-urlencoded bodies returned from refresh_token_…
…request compliance hook * Handle the case where a non-x-www-form-urlencoded string is returned from the refresh_token_request compliance hook, and allow it to be used as a raw string in the subsequent request * Add wix compliance fix to send token request and refresh requests with JSON bodies * Add unit tests for wix compliance fixes
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
from oauthlib.common import urldecode | ||
|
||
""" | ||
Wix requires the request body for token requests to be sent in JSON format | ||
instead of x-www-form-urlencoded. | ||
""" | ||
def wix_compliance_fix(session): | ||
|
||
def _non_compliant_access_token_request_body( | ||
url: str, headers: dict, request_kwargs: dict | ||
): | ||
""" | ||
Move the request body from the `data` kwarg to the `json` kwarg, | ||
and set the `Content-Type` header to `application/json`. | ||
""" | ||
headers["Content-Type"] = "application/json" | ||
request_kwargs["json"] = request_kwargs["data"] | ||
del request_kwargs["data"] | ||
return url, headers, request_kwargs | ||
|
||
def _non_compliant_refresh_token_request_body( | ||
token_url: str, headers: dict, body: str | ||
): | ||
""" | ||
Convert the body from a urlencoded string to a JSON string, | ||
and set the `Content-Type` header to `application/json`. | ||
""" | ||
headers["Content-Type"] = "application/json" | ||
body = json.dumps(dict(urldecode(body))) | ||
return token_url, headers, body | ||
|
||
session.register_compliance_hook("access_token_request", _non_compliant_access_token_request_body) | ||
session.register_compliance_hook("refresh_token_request", _non_compliant_refresh_token_request_body) | ||
|
||
return session |
This file contains 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
This file contains 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