Compare commits

..

No commits in common. "2576ad0dce9f76afa482bd70934ccbea28e4ded8" and "4ac7e46ca84de79556401130d45a4e4e39593537" have entirely different histories.

47
door.py
View File

@ -77,26 +77,16 @@ 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 i, line in enumerate(lines): for l in lines:
l = line.split('|') l = l.split('|', 1)
if len(l) == 5: if len(l) > 1:
if not l[0].strip().startswith('#'): if l[0] in valid:
token, name, organization, email, valid_thru = l log("Warning: Overwriting token %s" % (l[0],))
try: log("Got token: %s (%s)" % (l[0], l[1]))
if len(valid_thru.strip()) > 0: valid[l[0]] = l[1]
valid_thru = datetime.date.fromisoformat(valid_thru)
else:
valid_thru = None
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(f"Skipping line {i} ({line}) since it does not contain exactly 5 data field") log("Got unnamed token: %s" % (l[0],))
valid[l[0]] = None
except: except:
valid = {} valid = {}
log("Error reading token file") log("Error reading token file")
@ -262,20 +252,13 @@ def handle_nfc_token(token = None):
token = token.strip() token = token.strip()
if token in valid: if token in valid:
data = valid[token] name = valid[token]
if name:
if data['valid_thru'] is not None: name = "%s (%s)" % (token, name)
# 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:
# otherwise we don't need to check name = token
authorized = True log("Valid token: %s" % name)
toggle_door_state()
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: 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))