Skip to content

Commit 16616cc

Browse files
committed
fix: updates with helper function for utcfromtimestamp
1 parent f0cd184 commit 16616cc

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

google/auth/_helpers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@ def utcnow():
124124
return now
125125

126126

127+
def utcfromtimestamp(timestamp):
128+
"""Returns the UTC datetime from a timestamp.
129+
130+
Args:
131+
timestamp (float): The timestamp to convert.
132+
133+
Returns:
134+
datetime: The time in UTC.
135+
"""
136+
# We used datetime.utcfromtimestamp() before, since it's deprecated from
137+
# python 3.12, we are using datetime.fromtimestamp(timestamp, timezone.utc)
138+
# now. "utcfromtimestamp()" is offset-native (no timezone info), but
139+
# "fromtimestamp(timestamp, timezone.utc)" is offset-aware (with timezone
140+
# info). This will cause datetime comparison problem. For backward
141+
# compatibility, we need to remove the timezone info.
142+
dt = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
143+
dt = dt.replace(tzinfo=None)
144+
return dt
145+
146+
127147
def datetime_to_secs(value):
128148
"""Convert a datetime object to the number of seconds since the UNIX epoch.
129149

google/auth/app_engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def refresh(self, request):
128128
scopes = self._scopes if self._scopes is not None else self._default_scopes
129129
# pylint: disable=unused-argument
130130
token, ttl = app_identity.get_access_token(scopes, self._service_account_id)
131-
expiry = datetime.datetime.fromtimestamp(ttl, tz=datetime.timezone.utc)
131+
expiry = _helpers.utcfromtimestamp(ttl)
132132

133133
self.token, self.expiry = token, expiry
134134

google/auth/compute_engine/credentials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def _call_metadata_identity_endpoint(self, request):
498498
raise new_exc from caught_exc
499499

500500
_, payload, _, _ = jwt._unverified_decode(id_token)
501-
return id_token, datetime.datetime.fromtimestamp(payload["exp"], tz=datetime.timezone.utc)
501+
return id_token, _helpers.utcfromtimestamp(payload["exp"])
502502

503503
def refresh(self, request):
504504
"""Refreshes the ID token.

google/auth/impersonated_credentials.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"""
2727

2828
import base64
29-
import copy
30-
from datetime import datetime, timezone
29+
import datetime
30+
from datetime import datetime
3131
import http.client as http_client
3232
import json
3333

@@ -649,8 +649,8 @@ def refresh(self, request):
649649
raise new_exc from caught_exc
650650

651651
self.token = id_token
652-
self.expiry = datetime.fromtimestamp(
653-
jwt.decode(id_token, verify=False)["exp"], tz=timezone.utc
652+
self.expiry = _helpers.utcfromtimestamp(
653+
jwt.decode(id_token, verify=False)["exp"]
654654
)
655655

656656

google/oauth2/_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def call_iam_generate_id_token_endpoint(
368368
raise new_exc from caught_exc
369369

370370
payload = jwt.decode(id_token, verify=False)
371-
expiry = datetime.datetime.fromtimestamp(payload["exp"], tz=datetime.timezone.utc)
371+
expiry = _helpers.utcfromtimestamp(payload["exp"])
372372

373373
return id_token, expiry
374374

@@ -420,7 +420,7 @@ def id_token_jwt_grant(request, token_uri, assertion, can_retry=True):
420420
raise new_exc from caught_exc
421421

422422
payload = jwt.decode(id_token, verify=False)
423-
expiry = datetime.datetime.fromtimestamp(payload["exp"], tz=datetime.timezone.utc)
423+
expiry = _helpers.utcfromtimestamp(payload["exp"])
424424

425425
return id_token, expiry, response_data
426426

google/oauth2/_client_async.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import urllib
3030

3131
from google.auth import _exponential_backoff
32+
from google.auth import _helpers
3233
from google.auth import exceptions
3334
from google.auth import jwt
3435
from google.oauth2 import _client as client
@@ -227,7 +228,7 @@ async def id_token_jwt_grant(request, token_uri, assertion, can_retry=True):
227228
raise new_exc from caught_exc
228229

229230
payload = jwt.decode(id_token, verify=False)
230-
expiry = datetime.datetime.fromtimestamp(payload["exp"], tz=datetime.timezone.utc)
231+
expiry = _helpers.utcfromtimestamp(payload["exp"])
231232

232233
return id_token, expiry, response_data
233234

0 commit comments

Comments
 (0)