Skip to content

Commit 687ac1a

Browse files
committed
replace deprecated utcfromtimestamp
1 parent af18060 commit 687ac1a

File tree

10 files changed

+36
-36
lines changed

10 files changed

+36
-36
lines changed

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.utcfromtimestamp(ttl)
131+
expiry = datetime.datetime.fromtimestamp(ttl, tz=datetime.timezone.utc)
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
@@ -439,7 +439,7 @@ def _call_metadata_identity_endpoint(self, request):
439439
raise new_exc from caught_exc
440440

441441
_, payload, _, _ = jwt._unverified_decode(id_token)
442-
return id_token, datetime.datetime.utcfromtimestamp(payload["exp"])
442+
return id_token, datetime.datetime.fromtimestamp(payload["exp"], tz=datetime.timezone.utc)
443443

444444
def refresh(self, request):
445445
"""Refreshes the ID token.

google/auth/impersonated_credentials.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

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

@@ -591,8 +591,8 @@ def refresh(self, request):
591591

592592
id_token = response.json()["token"]
593593
self.token = id_token
594-
self.expiry = datetime.utcfromtimestamp(
595-
jwt.decode(id_token, verify=False)["exp"]
594+
self.expiry = datetime.fromtimestamp(
595+
jwt.decode(id_token, verify=False)["exp"], tz=timezone.utc
596596
)
597597

598598

google/oauth2/_client.py

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

364364
payload = jwt.decode(id_token, verify=False)
365-
expiry = datetime.datetime.utcfromtimestamp(payload["exp"])
365+
expiry = datetime.datetime.fromtimestamp(payload["exp"], tz=datetime.timezone.utc)
366366

367367
return id_token, expiry
368368

@@ -414,7 +414,7 @@ def id_token_jwt_grant(request, token_uri, assertion, can_retry=True):
414414
raise new_exc from caught_exc
415415

416416
payload = jwt.decode(id_token, verify=False)
417-
expiry = datetime.datetime.utcfromtimestamp(payload["exp"])
417+
expiry = datetime.datetime.fromtimestamp(payload["exp"], tz=datetime.timezone.utc)
418418

419419
return id_token, expiry, response_data
420420

google/oauth2/_client_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ async def id_token_jwt_grant(request, token_uri, assertion, can_retry=True):
223223
raise new_exc from caught_exc
224224

225225
payload = jwt.decode(id_token, verify=False)
226-
expiry = datetime.datetime.utcfromtimestamp(payload["exp"])
226+
expiry = datetime.datetime.fromtimestamp(payload["exp"], tz=datetime.timezone.utc)
227227

228228
return id_token, expiry, response_data
229229

tests/compute_engine/test_credentials.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def test_default_state(self, get):
308308

309309
@mock.patch(
310310
"google.auth._helpers.utcnow",
311-
return_value=datetime.datetime.utcfromtimestamp(0),
311+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
312312
)
313313
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
314314
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -341,7 +341,7 @@ def test_make_authorization_grant_assertion(self, sign, get, utcnow):
341341

342342
@mock.patch(
343343
"google.auth._helpers.utcnow",
344-
return_value=datetime.datetime.utcfromtimestamp(0),
344+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
345345
)
346346
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
347347
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -373,7 +373,7 @@ def test_with_service_account(self, sign, get, utcnow):
373373

374374
@mock.patch(
375375
"google.auth._helpers.utcnow",
376-
return_value=datetime.datetime.utcfromtimestamp(0),
376+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
377377
)
378378
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
379379
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -429,7 +429,7 @@ def test_token_uri(self):
429429

430430
@mock.patch(
431431
"google.auth._helpers.utcnow",
432-
return_value=datetime.datetime.utcfromtimestamp(0),
432+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
433433
)
434434
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
435435
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -551,7 +551,7 @@ def test_with_target_audience_integration(self):
551551

552552
@mock.patch(
553553
"google.auth._helpers.utcnow",
554-
return_value=datetime.datetime.utcfromtimestamp(0),
554+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
555555
)
556556
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
557557
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -590,7 +590,7 @@ def test_with_quota_project(self, sign, get, utcnow):
590590

591591
@mock.patch(
592592
"google.auth._helpers.utcnow",
593-
return_value=datetime.datetime.utcfromtimestamp(0),
593+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
594594
)
595595
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
596596
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -612,7 +612,7 @@ def test_with_token_uri(self, sign, get, utcnow):
612612

613613
@mock.patch(
614614
"google.auth._helpers.utcnow",
615-
return_value=datetime.datetime.utcfromtimestamp(0),
615+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
616616
)
617617
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
618618
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -720,7 +720,7 @@ def test_with_quota_project_integration(self):
720720

721721
@mock.patch(
722722
"google.auth._helpers.utcnow",
723-
return_value=datetime.datetime.utcfromtimestamp(0),
723+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
724724
)
725725
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
726726
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -731,7 +731,7 @@ def test_refresh_success(self, id_token_jwt_grant, sign, get, utcnow):
731731
]
732732
sign.side_effect = [b"signature"]
733733
id_token_jwt_grant.side_effect = [
734-
("idtoken", datetime.datetime.utcfromtimestamp(3600), {})
734+
("idtoken", datetime.datetime.fromtimestamp(3600, tz=datetime.timezone.utc), {})
735735
]
736736

737737
request = mock.create_autospec(transport.Request, instance=True)
@@ -744,7 +744,7 @@ def test_refresh_success(self, id_token_jwt_grant, sign, get, utcnow):
744744

745745
# Check that the credentials have the token and proper expiration
746746
assert self.credentials.token == "idtoken"
747-
assert self.credentials.expiry == (datetime.datetime.utcfromtimestamp(3600))
747+
assert self.credentials.expiry == (datetime.datetime.fromtimestamp(3600, tz=datetime.timezone.utc))
748748

749749
# Check the credential info
750750
assert self.credentials.service_account_email == "[email protected]"
@@ -755,7 +755,7 @@ def test_refresh_success(self, id_token_jwt_grant, sign, get, utcnow):
755755

756756
@mock.patch(
757757
"google.auth._helpers.utcnow",
758-
return_value=datetime.datetime.utcfromtimestamp(0),
758+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
759759
)
760760
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
761761
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -782,7 +782,7 @@ def test_refresh_error(self, sign, get, utcnow):
782782

783783
@mock.patch(
784784
"google.auth._helpers.utcnow",
785-
return_value=datetime.datetime.utcfromtimestamp(0),
785+
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
786786
)
787787
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
788788
@mock.patch("google.auth.iam.Signer.sign", autospec=True)
@@ -793,7 +793,7 @@ def test_before_request_refreshes(self, id_token_jwt_grant, sign, get, utcnow):
793793
]
794794
sign.side_effect = [b"signature"]
795795
id_token_jwt_grant.side_effect = [
796-
("idtoken", datetime.datetime.utcfromtimestamp(3600), {})
796+
("idtoken", datetime.datetime.fromtimestamp(3600, tz=datetime.timezone.utc), {})
797797
]
798798

799799
request = mock.create_autospec(transport.Request, instance=True)
@@ -862,7 +862,7 @@ def test_get_id_token_from_metadata(
862862
}
863863

864864
assert cred.token == SAMPLE_ID_TOKEN
865-
assert cred.expiry == datetime.datetime.utcfromtimestamp(SAMPLE_ID_TOKEN_EXP)
865+
assert cred.expiry == datetime.datetime.fromtimestamp(SAMPLE_ID_TOKEN_EXP, tz=datetime.timezone.utc)
866866
assert cred._use_metadata_identity_endpoint
867867
assert cred._signer is None
868868
assert cred._token_uri is None

tests/oauth2/test__client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def test_jwt_grant_no_access_token():
313313

314314

315315
def test_call_iam_generate_id_token_endpoint():
316-
now = _helpers.utcnow()
316+
now = _helpers.utcnow().astimezone(datetime.timezone.utc)
317317
id_token_expiry = _helpers.datetime_to_secs(now)
318318
id_token = jwt.encode(SIGNER, {"exp": id_token_expiry}).decode("utf-8")
319319
request = make_request({"token": id_token})
@@ -343,7 +343,7 @@ def test_call_iam_generate_id_token_endpoint():
343343
# Check result
344344
assert token == id_token
345345
# JWT does not store microseconds
346-
now = now.replace(microsecond=0)
346+
now = now.replace(microsecond=0).astimezone(datetime.timezone.utc)
347347
assert expiry == now
348348

349349

@@ -368,7 +368,7 @@ def test_call_iam_generate_id_token_endpoint_no_id_token():
368368

369369

370370
def test_id_token_jwt_grant():
371-
now = _helpers.utcnow()
371+
now = _helpers.utcnow().astimezone(datetime.timezone.utc)
372372
id_token_expiry = _helpers.datetime_to_secs(now)
373373
id_token = jwt.encode(SIGNER, {"exp": id_token_expiry}).decode("utf-8")
374374
request = make_request({"id_token": id_token, "extra": "data"})
@@ -385,7 +385,7 @@ def test_id_token_jwt_grant():
385385
# Check result
386386
assert token == id_token
387387
# JWT does not store microseconds
388-
now = now.replace(microsecond=0)
388+
now = now.replace(microsecond=0).astimezone(datetime.timezone.utc)
389389
assert expiry == now
390390
assert extra_data["extra"] == "data"
391391

tests/test_app_engine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_service_account_email_explicit(self, app_identity):
159159
assert credentials.service_account_email == mock.sentinel.service_account_email
160160
assert not app_identity.get_service_account_name.called
161161

162-
@mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min)
162+
@mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min.replace(tzinfo=datetime.timezone.utc))
163163
def test_refresh(self, utcnow, app_identity):
164164
token = "token"
165165
ttl = 643942923
@@ -174,11 +174,11 @@ def test_refresh(self, utcnow, app_identity):
174174
credentials.scopes, credentials._service_account_id
175175
)
176176
assert credentials.token == token
177-
assert credentials.expiry == datetime.datetime(1990, 5, 29, 1, 2, 3)
177+
assert credentials.expiry == datetime.datetime(1990, 5, 29, 1, 2, 3, tzinfo=datetime.timezone.utc)
178178
assert credentials.valid
179179
assert not credentials.expired
180180

181-
@mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min)
181+
@mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min.replace(tzinfo=datetime.timezone.utc))
182182
def test_refresh_with_default_scopes(self, utcnow, app_identity):
183183
token = "token"
184184
ttl = 643942923
@@ -191,7 +191,7 @@ def test_refresh_with_default_scopes(self, utcnow, app_identity):
191191
credentials.default_scopes, credentials._service_account_id
192192
)
193193
assert credentials.token == token
194-
assert credentials.expiry == datetime.datetime(1990, 5, 29, 1, 2, 3)
194+
assert credentials.expiry == datetime.datetime(1990, 5, 29, 1, 2, 3, tzinfo=datetime.timezone.utc)
195195
assert credentials.valid
196196
assert not credentials.expired
197197

tests/test_impersonated_credentials.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ def test_id_token_success(
707707
id_creds.refresh(request)
708708

709709
assert id_creds.token == ID_TOKEN_DATA
710-
assert id_creds.expiry == datetime.datetime.utcfromtimestamp(ID_TOKEN_EXPIRY)
710+
assert id_creds.expiry == datetime.datetime.fromtimestamp(ID_TOKEN_EXPIRY, tz=datetime.timezone.utc)
711711

712712
def test_id_token_metrics(self, mock_donor_credentials):
713713
credentials = self.make_credentials(lifetime=None)
@@ -731,8 +731,8 @@ def test_id_token_metrics(self, mock_donor_credentials):
731731
id_creds.refresh(None)
732732

733733
assert id_creds.token == ID_TOKEN_DATA
734-
assert id_creds.expiry == datetime.datetime.utcfromtimestamp(
735-
ID_TOKEN_EXPIRY
734+
assert id_creds.expiry == datetime.datetime.fromtimestamp(
735+
ID_TOKEN_EXPIRY, tz=datetime.timezone.utc
736736
)
737737
assert (
738738
mock_post.call_args.kwargs["headers"]["x-goog-api-client"]
@@ -841,7 +841,7 @@ def test_id_token_with_target_audience(
841841
id_creds.refresh(request)
842842

843843
assert id_creds.token == ID_TOKEN_DATA
844-
assert id_creds.expiry == datetime.datetime.utcfromtimestamp(ID_TOKEN_EXPIRY)
844+
assert id_creds.expiry == datetime.datetime.fromtimestamp(ID_TOKEN_EXPIRY, tz=datetime.timezone.utc)
845845
assert id_creds._include_email is True
846846

847847
def test_id_token_invalid_cred(

tests_async/oauth2/test__client_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ async def test_jwt_grant_no_access_token():
245245

246246
@pytest.mark.asyncio
247247
async def test_id_token_jwt_grant():
248-
now = _helpers.utcnow()
248+
now = _helpers.utcnow().astimezone(datetime.timezone.utc)
249249
id_token_expiry = _helpers.datetime_to_secs(now)
250250
id_token = jwt.encode(test_client.SIGNER, {"exp": id_token_expiry}).decode("utf-8")
251251
request = make_request({"id_token": id_token, "extra": "data"})
@@ -263,7 +263,7 @@ async def test_id_token_jwt_grant():
263263
# Check result
264264
assert token == id_token
265265
# JWT does not store microseconds
266-
now = now.replace(microsecond=0)
266+
now = now.replace(microsecond=0).astimezone(datetime.timezone.utc)
267267
assert expiry == now
268268
assert extra_data["extra"] == "data"
269269

0 commit comments

Comments
 (0)