Skip to content

Commit cae3d63

Browse files
authored
fix: always pass body of type bytes to google.auth.transport.Request (#421)
[`google.auth.transport.Request`](https://google-auth.readthedocs.io/en/latest/reference/google.auth.transport.html#google.auth.transport.Request) says that the body should be of type bytes. Some of our code was passing in strings as reported in #318. In practice this was not causing issues, as the two http transports [requests](https://google-auth.readthedocs.io/en/latest/reference/google.auth.transport.requests.html) and [urllib3](https://google-auth.readthedocs.io/en/latest/reference/google.auth.transport.urllib3.html) are able to handle bodies passed as strings.
1 parent 56ce0a3 commit cae3d63

4 files changed

Lines changed: 7 additions & 5 deletions

File tree

packages/google-auth/google/auth/iam.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def _make_signing_request(self, message):
7070
method = "POST"
7171
url = _SIGN_BLOB_URI.format(self._service_account_email)
7272
headers = {}
73-
body = json.dumps({"bytesToSign": base64.b64encode(message).decode("utf-8")})
73+
body = json.dumps(
74+
{"bytesToSign": base64.b64encode(message).decode("utf-8")}
75+
).encode("utf-8")
7476

7577
self._credentials.before_request(self._request, method, url, headers)
7678
response = self._request(url=url, method=method, body=body, headers=headers)

packages/google-auth/google/auth/impersonated_credentials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def _make_iam_token_request(request, principal, headers, body):
8484
"""
8585
iam_endpoint = _IAM_ENDPOINT.format(principal)
8686

87-
body = json.dumps(body)
87+
body = json.dumps(body).encode("utf-8")
8888

8989
response = request(url=iam_endpoint, method="POST", headers=headers, body=body)
9090

packages/google-auth/google/oauth2/_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _token_endpoint_request(request, token_uri, body):
9595
google.auth.exceptions.RefreshError: If the token endpoint returned
9696
an error.
9797
"""
98-
body = urllib.parse.urlencode(body)
98+
body = urllib.parse.urlencode(body).encode("utf-8")
9999
headers = {"content-type": _URLENCODED_CONTENT_TYPE}
100100

101101
retry = 0

packages/google-auth/tests/oauth2/test__client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test__token_endpoint_request():
9696
method="POST",
9797
url="http://example.com",
9898
headers={"content-type": "application/x-www-form-urlencoded"},
99-
body="test=params",
99+
body="test=params".encode("utf-8"),
100100
)
101101

102102
# Check result
@@ -131,7 +131,7 @@ def test__token_endpoint_request_internal_failure_error():
131131

132132

133133
def verify_request_params(request, params):
134-
request_body = request.call_args[1]["body"]
134+
request_body = request.call_args[1]["body"].decode("utf-8")
135135
request_params = urllib.parse.parse_qs(request_body)
136136

137137
for key, value in six.iteritems(params):

0 commit comments

Comments
 (0)