Compare commits
3 Commits
4ac7e46ca8
...
2576ad0dce
Author | SHA1 | Date | |
---|---|---|---|
2576ad0dce | |||
1616d56288 | |||
b03f1e14e1 |
47
door.py
47
door.py
|
@ -77,16 +77,26 @@ 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)
|
||||||
|
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("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")
|
||||||
|
@ -252,13 +262,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
|
||||||
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))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user