Skip to content

Commit

Permalink
Use Python 3.6+ ISO time functions
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jan 31, 2022
1 parent 060eba8 commit e0241ab
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions src/onelogin/saml2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import base64
import warnings
from copy import deepcopy
import calendar
from datetime import datetime
from hashlib import sha1, sha256, sha384, sha512
from isodate import parse_duration as duration_parser
Expand Down Expand Up @@ -58,10 +57,6 @@ class OneLogin_Saml2_Utils:
RESPONSE_SIGNATURE_XPATH = '/samlp:Response/ds:Signature'
ASSERTION_SIGNATURE_XPATH = '/samlp:Response/saml:Assertion/ds:Signature'

TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
TIME_FORMAT_2 = "%Y-%m-%dT%H:%M:%S.%fZ"
TIME_FORMAT_WITH_FRAGMENT = re.compile(r'^(\d{4,4}-\d{2,2}-\d{2,2}T\d{2,2}:\d{2,2}:\d{2,2})(\.\d*)?Z?$')

@staticmethod
def escape_url(url, lowercase_urlencoding=False):
"""
Expand Down Expand Up @@ -377,49 +372,41 @@ def generate_unique_id():
def parse_time_to_SAML(time):
r"""
Converts a UNIX timestamp to SAML2 timestamp on the form
yyyy-mm-ddThh:mm:ss(\.s+)?Z.
yyyy-mm-ddThh:mm:ss.
> All SAML time values have the type "xs:dateTime",
> which is built in to the W3C XML Schema Datatypes specification,
> and MUST be expressed in UTC form, with no time zone component.
:param time: The time we should convert (DateTime).
:type: string
:return: SAML2 timestamp.
:rtype: string
"""
data = datetime.utcfromtimestamp(float(time))
return data.strftime(OneLogin_Saml2_Utils.TIME_FORMAT)
return datetime.utcfromtimestamp(float(time)).isoformat(timespec="seconds")

@staticmethod
def parse_SAML_to_time(timestr):
r"""
Converts a SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(\.s+)?Z
to a UNIX timestamp. The sub-second part is ignored.
Converts a SAML2 timestamp to a UNIX timestamp.
The sub-second part is truncated.
:param timestr: The time we should convert (SAML Timestamp).
:type: string
:return: Converted to a unix timestamp.
:rtype: int
"""
try:
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT)
except ValueError:
try:
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT_2)
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)

return calendar.timegm(data.utctimetuple())
return int(datetime.fromisoformat(timestr).timestamp())

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

@staticmethod
def parse_duration(duration, timestamp=None):
Expand All @@ -444,7 +431,7 @@ def parse_duration(duration, timestamp=None):
data = datetime.utcnow() + timedelta
else:
data = datetime.utcfromtimestamp(timestamp) + timedelta
return calendar.timegm(data.utctimetuple())
return int(data.timestamp())

@staticmethod
def get_expire_time(cache_duration=None, valid_until=None):
Expand Down

0 comments on commit e0241ab

Please sign in to comment.