Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions google/auth/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ def utcnow():
return now


def utcfromtimestamp(timestamp):
"""Returns the UTC datetime from a timestamp.

Args:
timestamp (float): The timestamp to convert.

Returns:
datetime: The time in UTC.
"""
# We used datetime.utcfromtimestamp() before, since it's deprecated from
# python 3.12, we are using datetime.fromtimestamp(timestamp, timezone.utc)
# now. "utcfromtimestamp()" is offset-native (no timezone info), but
# "fromtimestamp(timestamp, timezone.utc)" is offset-aware (with timezone
# info). This will cause datetime comparison problem. For backward
# compatibility, we need to remove the timezone info.
dt = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
dt = dt.replace(tzinfo=None)
return dt


def datetime_to_secs(value):
"""Convert a datetime object to the number of seconds since the UNIX epoch.

Expand Down
2 changes: 1 addition & 1 deletion google/auth/app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def refresh(self, request):
scopes = self._scopes if self._scopes is not None else self._default_scopes
# pylint: disable=unused-argument
token, ttl = app_identity.get_access_token(scopes, self._service_account_id)
expiry = datetime.datetime.utcfromtimestamp(ttl)
expiry = _helpers.utcfromtimestamp(ttl)

self.token, self.expiry = token, expiry

Expand Down
2 changes: 1 addition & 1 deletion google/auth/compute_engine/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def _call_metadata_identity_endpoint(self, request):
raise new_exc from caught_exc

_, payload, _, _ = jwt._unverified_decode(id_token)
return id_token, datetime.datetime.utcfromtimestamp(payload["exp"])
return id_token, _helpers.utcfromtimestamp(payload["exp"])

def refresh(self, request):
"""Refreshes the ID token.
Expand Down
4 changes: 2 additions & 2 deletions google/auth/impersonated_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import base64
import copy
from datetime import datetime
from datetime import datetime, timezone
import http.client as http_client
import json

Expand Down Expand Up @@ -649,7 +649,7 @@ def refresh(self, request):
raise new_exc from caught_exc

self.token = id_token
self.expiry = datetime.utcfromtimestamp(
self.expiry = _helpers.utcfromtimestamp(
jwt.decode(id_token, verify=False)["exp"]
)

Expand Down
4 changes: 2 additions & 2 deletions google/oauth2/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def call_iam_generate_id_token_endpoint(
raise new_exc from caught_exc

payload = jwt.decode(id_token, verify=False)
expiry = datetime.datetime.utcfromtimestamp(payload["exp"])
expiry = _helpers.utcfromtimestamp(payload["exp"])

return id_token, expiry

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

payload = jwt.decode(id_token, verify=False)
expiry = datetime.datetime.utcfromtimestamp(payload["exp"])
expiry = _helpers.utcfromtimestamp(payload["exp"])

return id_token, expiry, response_data

Expand Down
3 changes: 2 additions & 1 deletion google/oauth2/_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import urllib

from google.auth import _exponential_backoff
from google.auth import _helpers
from google.auth import exceptions
from google.auth import jwt
from google.oauth2 import _client as client
Expand Down Expand Up @@ -227,7 +228,7 @@ async def id_token_jwt_grant(request, token_uri, assertion, can_retry=True):
raise new_exc from caught_exc

payload = jwt.decode(id_token, verify=False)
expiry = datetime.datetime.utcfromtimestamp(payload["exp"])
expiry = _helpers.utcfromtimestamp(payload["exp"])

return id_token, expiry, response_data

Expand Down
Loading