Skip to content

Commit c45eb9d

Browse files
bdrungc00kiemon5ter
authored andcommitted
Replace deprecated datetime.utcnow() by datetime.now(timezone.utc)
Fixes: #934 Since Python 3.12, the utcnow function is deprecated. see, https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow This commit turns usages of `utcnow()` into `now(timezone.UTC)`. The solution is based on the suggestion from official Python docs: https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow Replace `datetime.datetime.utcnow()` with `datetime.datetime.now(datetime.timezone.utc)`. At the same time, the pytz dependency is removed in favour of the standard Python functions. Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent 178f6d1 commit c45eb9d

File tree

8 files changed

+22
-44
lines changed

8 files changed

+22
-44
lines changed

poetry.lock

Lines changed: 1 addition & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ dependencies = [
2727
"defusedxml",
2828
"pyopenssl <24.3.0",
2929
"python-dateutil",
30-
"pytz",
3130
"requests >=2.0.0,<3.0.0", # ^2 means compatible with 2.x
3231
"xmlschema >=2.0.0,<3.0.0"
3332
]
@@ -69,7 +68,6 @@ ipdb = "^0.13.9"
6968
mypy = "^1.0.0"
7069
types-pyopenssl = "^23.0.0.3"
7170
types-python-dateutil = "^2.8.19.6"
72-
types-pytz = "^2022.7.1.0"
7371
types-setuptools = "^67.2.0.1"
7472
types-six = "^1.16.21.4"
7573
types-requests = "^2.28.11.12"

src/saml2/cert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
__author__ = "haho0032"
22

33
import base64
4-
import datetime
54
from os import remove
65
from os.path import join
6+
from datetime import datetime
7+
from datetime import timezone
78

89
from OpenSSL import crypto
910
import dateutil.parser
10-
import pytz
1111

1212
import saml2.cryptography.pki
1313

@@ -278,7 +278,7 @@ def verify_chain(self, cert_chain_str_list, cert_str):
278278

279279
def certificate_not_valid_yet(self, cert):
280280
starts_to_be_valid = dateutil.parser.parse(cert.get_notBefore())
281-
now = pytz.UTC.localize(datetime.datetime.utcnow())
281+
now = datetime.now(timezone.utc)
282282
if starts_to_be_valid < now:
283283
return False
284284
return True

src/saml2/mongo_store.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import datetime
2-
from hashlib import sha1
31
import logging
2+
from hashlib import sha1
3+
from datetime import datetime
4+
from datetime import timezone
45

56
from pymongo import MongoClient
67
import pymongo.errors
@@ -205,7 +206,7 @@ def store(self, value, **kwargs):
205206
doc.update(kwargs)
206207
# Add timestamp to all documents to allow external garbage collecting
207208
if "created_at" not in doc:
208-
doc["created_at"] = datetime.datetime.utcnow()
209+
doc["created_at"] = datetime.now(timezone.utc)
209210
_ = self.db.insert_one(doc)
210211

211212
def get(self, value=None, **kwargs):

src/saml2/sigver.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
"""
44

55
import base64
6-
import datetime
7-
from importlib.resources import files as _resource_files
86
import hashlib
97
import itertools
108
import logging
119
import os
1210
import re
11+
from datetime import datetime
12+
from datetime import timezone
13+
from importlib.resources import files as _resource_files
1314
from subprocess import PIPE
1415
from subprocess import Popen
1516
from tempfile import NamedTemporaryFile
@@ -19,7 +20,6 @@
1920

2021
from OpenSSL import crypto
2122
import dateutil
22-
import pytz
2323

2424
from saml2 import ExtensionElement
2525
from saml2 import SamlBase
@@ -377,7 +377,7 @@ def active_cert(key):
377377
except AttributeError:
378378
return False
379379

380-
now = pytz.UTC.localize(datetime.datetime.utcnow())
380+
now = datetime.now(timezone.utc)
381381
valid_from = dateutil.parser.parse(cert.get_notBefore())
382382
valid_to = dateutil.parser.parse(cert.get_notAfter())
383383
active = not cert.has_expired() and valid_from <= now < valid_to

src/saml2/time_util.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"""
77

88
import calendar
9-
from datetime import datetime
10-
from datetime import timedelta
119
import re
1210
import sys
1311
import time
12+
from datetime import datetime
13+
from datetime import timezone
14+
from datetime import timedelta
1415

1516

1617
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
@@ -175,7 +176,7 @@ def time_in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0
175176
:return: UTC time
176177
"""
177178
delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
178-
return datetime.utcnow() + delta
179+
return datetime.now(timezone.utc) + delta
179180

180181

181182
def time_a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0):
@@ -185,7 +186,7 @@ def time_a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=
185186
minutes[, hours[, weeks]]]]]]])
186187
"""
187188
delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
188-
return datetime.utcnow() - delta
189+
return datetime.now(timezone.utc) - delta
189190

190191

191192
def in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0, format=TIME_FORMAT):

tests/test_41_response.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python
22
import logging
33
from contextlib import closing
4-
import datetime
4+
from datetime import datetime
5+
from datetime import timezone
56
from unittest.mock import Mock
67
from unittest.mock import patch
78

@@ -127,7 +128,7 @@ def test_issuer_none(self):
127128
@patch("saml2.time_util.datetime")
128129
def test_false_sign(self, mock_datetime, caplog):
129130
caplog.set_level(logging.ERROR)
130-
mock_datetime.utcnow = Mock(return_value=datetime.datetime(2016, 9, 4, 9, 59, 39))
131+
mock_datetime.now = Mock(return_value=datetime(2016, 9, 4, 9, 59, 39, tzinfo=timezone.utc))
131132
with open(FALSE_ASSERT_SIGNED) as fp:
132133
xml_response = fp.read()
133134

tests/test_44_authnresp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
from contextlib import closing
33
from datetime import datetime
4+
from datetime import timezone
45

56
from dateutil import parser
67
from pathutils import dotname
@@ -131,7 +132,7 @@ def test_verify_w_authn(self):
131132
assert len(authn_info) == 1
132133
assert authn_info[0][0] == INTERNETPROTOCOLPASSWORD
133134
assert authn_info[0][1] == ["http://www.example.com/login"]
134-
now = datetime.utcnow()
135+
now = datetime.now(timezone.utc)
135136
dt = parser.parse(authn_info[0][2])
136137
assert now.year == dt.year and now.month == dt.month and now.day == dt.day
137138
session_info = self.ar.session_info()

0 commit comments

Comments
 (0)