Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated datetime.utcnow() by datetime.now(timezone.utc) #979

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ importlib-resources = {python = "<3.9", version = "*"}
paste = {optional = true, version = "*"}
pyopenssl = "<24.3.0"
python-dateutil = "*"
pytz = "*"
"repoze.who" = {optional = true, version = "*"}
requests = "^2"
xmlschema = "^2"
Expand All @@ -68,7 +67,6 @@ ipdb = "^0.13.9"
mypy = "^1.0.0"
types-pyopenssl = "^23.0.0.3"
types-python-dateutil = "^2.8.19.6"
types-pytz = "^2022.7.1.0"
types-setuptools = "^67.2.0.1"
types-six = "^1.16.21.4"
types-requests = "^2.28.11.12"
Expand Down
3 changes: 1 addition & 2 deletions src/saml2/cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from OpenSSL import crypto
import dateutil.parser
import pytz

import saml2.cryptography.pki

Expand Down Expand Up @@ -278,7 +277,7 @@ def verify_chain(self, cert_chain_str_list, cert_str):

def certificate_not_valid_yet(self, cert):
starts_to_be_valid = dateutil.parser.parse(cert.get_notBefore())
now = pytz.UTC.localize(datetime.datetime.utcnow())
now = datetime.datetime.now(datetime.timezone.utc)
if starts_to_be_valid < now:
return False
return True
Expand Down
2 changes: 1 addition & 1 deletion src/saml2/mongo_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def store(self, value, **kwargs):
doc.update(kwargs)
# Add timestamp to all documents to allow external garbage collecting
if "created_at" not in doc:
doc["created_at"] = datetime.datetime.utcnow()
doc["created_at"] = datetime.datetime.now(datetime.timezone.utc)
_ = self.db.insert_one(doc)

def get(self, value=None, **kwargs):
Expand Down
3 changes: 1 addition & 2 deletions src/saml2/sigver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from urllib import parse

from OpenSSL import crypto
import pytz

from saml2 import ExtensionElement
from saml2 import SamlBase
Expand Down Expand Up @@ -387,7 +386,7 @@ def active_cert(key):
except AttributeError:
return False

now = pytz.UTC.localize(datetime.datetime.utcnow())
now = datetime.datetime.now(datetime.timezone.utc)
valid_from = dateutil.parser.parse(cert.get_notBefore())
valid_to = dateutil.parser.parse(cert.get_notAfter())
active = not cert.has_expired() and valid_from <= now < valid_to
Expand Down
6 changes: 3 additions & 3 deletions src/saml2/time_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

import calendar
from datetime import datetime
from datetime import datetime, timezone
from datetime import timedelta
import re
import sys
Expand Down Expand Up @@ -175,7 +175,7 @@ def time_in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0
:return: UTC time
"""
delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
return datetime.utcnow() + delta
return datetime.now(timezone.utc) + delta


def time_a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0):
Expand All @@ -185,7 +185,7 @@ def time_a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=
minutes[, hours[, weeks]]]]]]])
"""
delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
return datetime.utcnow() - delta
return datetime.now(timezone.utc) - delta


def in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0, format=TIME_FORMAT):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_41_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_issuer_none(self):
@patch("saml2.time_util.datetime")
def test_false_sign(self, mock_datetime, caplog):
caplog.set_level(logging.ERROR)
mock_datetime.utcnow = Mock(return_value=datetime.datetime(2016, 9, 4, 9, 59, 39))
mock_datetime.now = Mock(return_value=datetime.datetime(2016, 9, 4, 9, 59, 39, tzinfo=datetime.timezone.utc))
with open(FALSE_ASSERT_SIGNED) as fp:
xml_response = fp.read()

Expand Down
4 changes: 2 additions & 2 deletions tests/test_44_authnresp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
from contextlib import closing
from datetime import datetime
from datetime import datetime, timezone

from dateutil import parser
from pathutils import dotname
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_verify_w_authn(self):
assert len(authn_info) == 1
assert authn_info[0][0] == INTERNETPROTOCOLPASSWORD
assert authn_info[0][1] == ["http://www.example.com/login"]
now = datetime.utcnow()
now = datetime.now(timezone.utc)
dt = parser.parse(authn_info[0][2])
assert now.year == dt.year and now.month == dt.month and now.day == dt.day
session_info = self.ar.session_info()
Expand Down