From aea95a6c4894f99c3fc85cf1e2d15a2ff048a7f3 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 9 Jan 2020 12:41:45 -0800 Subject: [PATCH 1/3] fix: encode body to bytes for `google.auth.transport.Request` --- google/oauth2/_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/oauth2/_client.py b/google/oauth2/_client.py index 4cf7a7fe9..c4b792cf8 100644 --- a/google/oauth2/_client.py +++ b/google/oauth2/_client.py @@ -95,7 +95,7 @@ def _token_endpoint_request(request, token_uri, body): google.auth.exceptions.RefreshError: If the token endpoint returned an error. """ - body = urllib.parse.urlencode(body) + body = urllib.parse.urlencode(body).encode() headers = {"content-type": _URLENCODED_CONTENT_TYPE} retry = 0 From 7d3ae8adf0a9e3c2797508476e45ba3bc67e4277 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 9 Jan 2020 12:57:59 -0800 Subject: [PATCH 2/3] fix: fix tests --- tests/oauth2/test__client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/oauth2/test__client.py b/tests/oauth2/test__client.py index 9cf59eb98..17ff2cc3a 100644 --- a/tests/oauth2/test__client.py +++ b/tests/oauth2/test__client.py @@ -96,7 +96,7 @@ def test__token_endpoint_request(): method="POST", url="http://example.com", headers={"content-type": "application/x-www-form-urlencoded"}, - body="test=params", + body="test=params".encode(), ) # Check result @@ -131,7 +131,7 @@ def test__token_endpoint_request_internal_failure_error(): def verify_request_params(request, params): - request_body = request.call_args[1]["body"] + request_body = request.call_args[1]["body"].decode() request_params = urllib.parse.parse_qs(request_body) for key, value in six.iteritems(params): From 598d6c6f9adbad2d4efaf3c8d987b8cd1b726aa7 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 9 Jan 2020 13:39:29 -0800 Subject: [PATCH 3/3] fix: explicitly encode/decode in a few more places --- google/auth/iam.py | 4 +++- google/auth/impersonated_credentials.py | 2 +- google/oauth2/_client.py | 2 +- tests/oauth2/test__client.py | 4 ++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/google/auth/iam.py b/google/auth/iam.py index a43872658..0ab5b5549 100644 --- a/google/auth/iam.py +++ b/google/auth/iam.py @@ -70,7 +70,9 @@ def _make_signing_request(self, message): method = "POST" url = _SIGN_BLOB_URI.format(self._service_account_email) headers = {} - body = json.dumps({"bytesToSign": base64.b64encode(message).decode("utf-8")}) + body = json.dumps( + {"bytesToSign": base64.b64encode(message).decode("utf-8")} + ).encode("utf-8") self._credentials.before_request(self._request, method, url, headers) response = self._request(url=url, method=method, body=body, headers=headers) diff --git a/google/auth/impersonated_credentials.py b/google/auth/impersonated_credentials.py index 70fa5dc9c..bc7031e78 100644 --- a/google/auth/impersonated_credentials.py +++ b/google/auth/impersonated_credentials.py @@ -84,7 +84,7 @@ def _make_iam_token_request(request, principal, headers, body): """ iam_endpoint = _IAM_ENDPOINT.format(principal) - body = json.dumps(body) + body = json.dumps(body).encode("utf-8") response = request(url=iam_endpoint, method="POST", headers=headers, body=body) diff --git a/google/oauth2/_client.py b/google/oauth2/_client.py index c4b792cf8..4ba31a87a 100644 --- a/google/oauth2/_client.py +++ b/google/oauth2/_client.py @@ -95,7 +95,7 @@ def _token_endpoint_request(request, token_uri, body): google.auth.exceptions.RefreshError: If the token endpoint returned an error. """ - body = urllib.parse.urlencode(body).encode() + body = urllib.parse.urlencode(body).encode("utf-8") headers = {"content-type": _URLENCODED_CONTENT_TYPE} retry = 0 diff --git a/tests/oauth2/test__client.py b/tests/oauth2/test__client.py index 17ff2cc3a..052390aa8 100644 --- a/tests/oauth2/test__client.py +++ b/tests/oauth2/test__client.py @@ -96,7 +96,7 @@ def test__token_endpoint_request(): method="POST", url="http://example.com", headers={"content-type": "application/x-www-form-urlencoded"}, - body="test=params".encode(), + body="test=params".encode("utf-8"), ) # Check result @@ -131,7 +131,7 @@ def test__token_endpoint_request_internal_failure_error(): def verify_request_params(request, params): - request_body = request.call_args[1]["body"].decode() + request_body = request.call_args[1]["body"].decode("utf-8") request_params = urllib.parse.parse_qs(request_body) for key, value in six.iteritems(params):