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.utcfromtimestamp, datetime.utcnow #420

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
21 changes: 10 additions & 11 deletions src/onelogin/saml2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import base64
import warnings
from copy import deepcopy
import calendar
from datetime import datetime
from datetime import datetime, timezone
from hashlib import sha1, sha256, sha384, sha512
from isodate import parse_duration as duration_parser
import re
Expand Down Expand Up @@ -389,7 +388,7 @@ def parse_time_to_SAML(time):
:return: SAML2 timestamp.
:rtype: string
"""
data = datetime.utcfromtimestamp(float(time))
data = datetime.fromtimestamp(float(time), timezone.utc)
return data.strftime(OneLogin_Saml2_Utils.TIME_FORMAT)

@staticmethod
Expand All @@ -405,25 +404,25 @@ def parse_SAML_to_time(timestr):
:rtype: int
"""
try:
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT)
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT).replace(tzinfo=timezone.utc)
except ValueError:
try:
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT_2)
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT_2).replace(tzinfo=timezone.utc)
except ValueError:
elem = OneLogin_Saml2_Utils.TIME_FORMAT_WITH_FRAGMENT.match(timestr)
if not elem:
raise Exception("time data %s does not match format %s" % (timestr, r"yyyy-mm-ddThh:mm:ss(\.s+)?Z"))
data = datetime.strptime(elem.groups()[0] + "Z", OneLogin_Saml2_Utils.TIME_FORMAT)
data = datetime.strptime(elem.groups()[0] + "Z", OneLogin_Saml2_Utils.TIME_FORMAT).replace(tzinfo=timezone.utc)

return calendar.timegm(data.utctimetuple())
return int(data.timestamp())

@staticmethod
def now():
"""
:return: unix timestamp of actual time.
:rtype: int
"""
return calendar.timegm(datetime.utcnow().utctimetuple())
return int(datetime.now(timezone.utc).timestamp())

@staticmethod
def parse_duration(duration, timestamp=None):
Expand All @@ -445,10 +444,10 @@ def parse_duration(duration, timestamp=None):

timedelta = duration_parser(duration)
if timestamp is None:
data = datetime.utcnow() + timedelta
data = datetime.now(timezone.utc) + timedelta
else:
data = datetime.utcfromtimestamp(timestamp) + timedelta
return calendar.timegm(data.utctimetuple())
data = datetime.fromtimestamp(timestamp, timezone.utc) + timedelta
return int(data.timestamp())

@staticmethod
def get_expire_time(cache_duration=None, valid_until=None):
Expand Down
4 changes: 2 additions & 2 deletions tests/src/OneLogin/saml2_tests/metadata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
from os.path import dirname, join, exists
from time import strftime
from datetime import datetime
from datetime import datetime, timezone
import unittest

from onelogin.saml2 import compat
Expand Down Expand Up @@ -97,7 +97,7 @@ def testBuilder(self):
self.assertNotIn("cacheDuration", metadata5)
self.assertIn('validUntil="2014-10-01T11:04:29Z"', metadata5)

datetime_value = datetime.now()
datetime_value = datetime.now(timezone.utc)
metadata6 = OneLogin_Saml2_Metadata.builder(sp_data, security["authnRequestsSigned"], security["wantAssertionsSigned"], datetime_value, "P1Y", contacts, organization)
self.assertIsNotNone(metadata5)
self.assertIn("<md:SPSSODescriptor", metadata6)
Expand Down
Loading