added checks for token valid thru date

This commit is contained in:
Simon Pirkelmann 2021-04-10 16:48:56 +02:00
parent 4ac7e46ca8
commit b03f1e14e1

45
door.py
View File

@ -77,16 +77,24 @@ def read_valid_tokens():
log("Loading tokens")
valid = {}
lines =[ s.strip() for s in open(config.valid_tokens, "r").readlines() ]
for l in lines:
l = l.split('|', 1)
if len(l) > 1:
if l[0] in valid:
log("Warning: Overwriting token %s" % (l[0],))
log("Got token: %s (%s)" % (l[0], l[1]))
valid[l[0]] = l[1]
for i, line in enumerate(lines):
l = line.split('|')
if len(l) == 5:
if not l[0].strip().startswith('#'):
token, name, organization, email, valid_thru = l
try:
if len(valid_thru.strip()) > 0:
valid_thru = datetime.date.fromisoformat(valid_thru)
except Exception:
log(f"Could not parse valid thru date for token {token} in line {i}")
valid_thru = None
log(f"Got token {token} associated with {name} <{email}> of {organization}, valid thru {valid_thru}")
if token in valid:
log(f"Warning: Overwriting token {token}")
valid[token] = {'name': name, 'organization': organization, 'email': email,
'valid_thru': valid_thru}
else:
log("Got unnamed token: %s" % (l[0],))
valid[l[0]] = None
log(f"Skipping line {i} ({line}) since it does not contain exactly 5 data field")
except:
valid = {}
log("Error reading token file")
@ -166,7 +174,7 @@ def handle_door_state():
return
# Get new state
poll_door_state()
#poll_door_state()
# Idle + change = key?
if action == IDLE:
@ -252,13 +260,20 @@ def handle_nfc_token(token = None):
token = token.strip()
if token in valid:
name = valid[token]
if name:
name = "%s (%s)" % (token, name)
data = valid[token]
if data['valid_thru'] is not None:
# if a valid thru date has been set we check if the token is still valid
authorized = datetime.date.today() <= data['valid_thru']
else:
name = token
log("Valid token: %s" % name)
# otherwise we don't need to check
authorized = True
if authorized:
log(f"Valid token {token} of {data['name']}")
toggle_door_state()
else:
log(f"Token {token} of {data['name']} expired on {data['valid_thru']}")
else:
log("Invalid token:", token)
mqtt("token/last_invalid", "%s;%s" % (timestamp(), token))