Skip to content

Commit 9ff767b

Browse files
authored
fix: Make OAUTH2.0 client resistant to string type 'expires_in' responses from non-compliant services (#1208)
This fixes googleapis/google-auth-library-python#1207.
1 parent 8d63bd9 commit 9ff767b

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ def _parse_expiry(response_data):
120120
expires_in = response_data.get("expires_in", None)
121121

122122
if expires_in is not None:
123+
# Some services do not respect the OAUTH2.0 RFC and send expires_in as a
124+
# JSON String.
125+
if isinstance(expires_in, str):
126+
expires_in = int(expires_in)
127+
123128
return _helpers.utcnow() + datetime.timedelta(seconds=expires_in)
124129
else:
125130
return None

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ def test__can_retry_no_retry_message(response_data):
9999
assert not _client._can_retry(http_client.OK, response_data)
100100

101101

102+
@pytest.mark.parametrize("mock_expires_in", [500, "500"])
102103
@mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min)
103-
def test__parse_expiry(unused_utcnow):
104-
result = _client._parse_expiry({"expires_in": 500})
104+
def test__parse_expiry(unused_utcnow, mock_expires_in):
105+
result = _client._parse_expiry({"expires_in": mock_expires_in})
105106
assert result == datetime.datetime.min + datetime.timedelta(seconds=500)
106107

107108

0 commit comments

Comments
 (0)