removed counter because we cannot reliably guarantee that it is incremented

This commit is contained in:
Simon Pirkelmann 2020-06-24 20:52:52 +01:00
parent 75e7e8bf99
commit bc66a3a729
3 changed files with 19 additions and 52 deletions

Binary file not shown.

View File

@ -69,9 +69,9 @@ def select_all_users(conn):
return rows return rows
def increment_counter(conn, id): def register_access(conn, id):
""" """
increment the authentication counter in the database for the given id increment the authentication counter in the database for the given id and save time of the last access
""" """
sql = ''' UPDATE users sql = ''' UPDATE users
SET counter = counter + 1 , SET counter = counter + 1 ,

67
main.py
View File

@ -11,23 +11,10 @@ class DoorLock():
def __init__(self): def __init__(self):
# initialize card reader # initialize card reader
self.reader = SimpleMFRC522() self.reader = SimpleMFRC522()
self.key = b'Mellon!'
self.key += b' ' * (48 - len(self.key))
def increment(self, counter): def check_authorization(self, reader_id, reader_data):
# increment counter and send it to card
counter += 1
data_new = str(counter)
self.reader.write(data_new)
# check if counter was updated successfully
_, text = self.reader.read()
try:
counter_new = int(text)
except ValueError:
return False
return counter == counter_new
def check_authorization(self, reader_id, counter):
# open database # open database
conn = create_connection(database) conn = create_connection(database)
@ -39,15 +26,14 @@ class DoorLock():
db_id = user[0] db_id = user[0]
name = user[1] name = user[1]
user_card_id = user[2] user_card_id = user[2]
user_counter = user[3]
if reader_id == user_card_id: if reader_id == user_card_id:
# check if use counter on the card matches counter in the database print("card id match found")
# if counter is different -> assume the card has been cloned if reader_data.encode() == self.key:
if counter == user_counter:
print("user {} with card_id {} authorized".format(name, hex(reader_id))) print("user {} with card_id {} authorized".format(name, hex(reader_id)))
return True, db_id return True, db_id
else: else:
print("error: counter does not match! please investigate!") print("incorrect key phrase")
# if no match was found in the database: deny entry # if no match was found in the database: deny entry
print("You shall not pass!") print("You shall not pass!")
@ -70,36 +56,17 @@ class DoorLock():
print("Hold card before reader..") print("Hold card before reader..")
uid, data = self.reader.read() uid, data = self.reader.read()
print("data = ", data) print("card read: \n uid = {}\ndata = {}\n".format(hex(uid), data))
#counter = int.from_bytes(data, byteorder='big') authorized, db_id = self.check_authorization(uid, data)
try:
counter = int(data)
except ValueError:
print("error: data on the card could not be converted")
counter = None
if counter is not None: if authorized:
print("card read: \n uid = {}\ncounter = {}\n".format(hex(uid), counter)) conn = create_connection(database)
authorized, db_id = self.check_authorization(uid, counter) register_access(conn, db_id)
if authorized: self.unlock_door()
# increment use counter on the card else:
increment_status = self.increment(counter) print("authentication failed")
self.release_the_kraken()
if increment_status:
# update the counter and the time of last access in the database
# open database
conn = create_connection(database)
increment_counter(conn, db_id)
self.unlock_door()
else:
# if we cannot increment the counter on the card (e.g. because the card was removed too quickly)
# we do not let the user in even though authentication was correct -> try again
print("increment failed!")
else:
print("authentication failed")
self.release_the_kraken()
time.sleep(1.5) time.sleep(1.5)
finally: finally:
#GPIO.cleanup() #GPIO.cleanup()
@ -117,7 +84,7 @@ if __name__ == "__main__":
doors_of_durin = DoorLock() doors_of_durin = DoorLock()
data = bytearray([0]*16) data = bytearray([0]*16)
#data = '0' data = 'Mellon!'
#write_success = doors_of_durin.reader.write(data) #write_success = doors_of_durin.reader.write(data)
doors_of_durin.run_authorization() doors_of_durin.run_authorization()
pass pass