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