Skip to content

Commit

Permalink
allow to read old legacy time from validity period
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelinux committed Jun 26, 2017
1 parent 5a82943 commit 227943b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions privacyidea/lib/tokenclass.py
Expand Up @@ -1030,13 +1030,13 @@ def check_validity_period(self):

if start:
#dt_start = datetime.datetime.strptime(start, DATE_FORMAT)
dt_start = parse_date_string(start)
dt_start = parse_legacy_time(start, return_date=True)
if dt_start > datetime.datetime.now(tzlocal()):
return False

if end:
#dt_end = datetime.datetime.strptime(end, DATE_FORMAT)
dt_end = parse_date_string(end)
dt_end = parse_legacy_time(end, return_date=True)
if dt_end < datetime.datetime.now(tzlocal()):
return False

Expand Down
10 changes: 7 additions & 3 deletions privacyidea/lib/utils.py
Expand Up @@ -628,22 +628,26 @@ def int_to_hex(serial):
return serial_hex


def parse_legacy_time(ts):
def parse_legacy_time(ts, return_date=False):
"""
The new timestrings are of the format YYYY-MM-DDThh:mm+oooo.
They contain the timezone offset!
Old legancy time strings are of format DD/MM/YY hh:mm without time zone
Old legacy time strings are of format DD/MM/YY hh:mm without time zone
offset.
This function parses string and returns the new formatted time string
including the timezone offset.
:param timestring:
:param return_date: If set to True a date is returned instead of a string
:return:
"""
from privacyidea.lib.tokenclass import DATE_FORMAT
d = parse_date_string(ts)
if not d.tzinfo:
# we need to reparse the string
d = parse_date_string(ts, tzinfos=tzlocal, dayfirst=True)
return d.strftime(DATE_FORMAT)
if return_date:
return d
else:
return d.strftime(DATE_FORMAT)
11 changes: 11 additions & 0 deletions tests/test_lib_tokenclass.py
Expand Up @@ -372,6 +372,17 @@ def test_11_old_validity_time(self):
s = token.get_validity_period_start()
self.assertTrue(s.startswith("2017-04-11T22:00"), s)

# old date format has problems with check_validity_date
start_date = datetime.datetime.now() - datetime.timedelta(days=15)
end_date = datetime.datetime.now() + datetime.timedelta(days=15)
start = start_date.strftime("%d/%m/%Y")
end = end_date.strftime("%d/%m/%Y")
# Need to write the old format to the database
token.add_tokeninfo("validity_period_start", start)
token.add_tokeninfo("validity_period_end", end)
r = token.check_validity_period()
self.assertTrue(r)

def test_11_tokeninfo_with_type(self):
db_token = Token.query.filter_by(serial=self.serial1).first()
token = TokenClass(db_token)
Expand Down

0 comments on commit 227943b

Please sign in to comment.