diff --git a/authorized_cards.db b/authorized_cards.db index 95d7145..e2b6073 100644 Binary files a/authorized_cards.db and b/authorized_cards.db differ diff --git a/database.py b/database.py index a3ebc34..adbc12f 100644 --- a/database.py +++ b/database.py @@ -69,9 +69,9 @@ def select_all_users(conn): 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 SET counter = counter + 1 , diff --git a/main.py b/main.py index 8f2b02e..411d3b3 100644 --- a/main.py +++ b/main.py @@ -11,23 +11,10 @@ class DoorLock(): def __init__(self): # initialize card reader self.reader = SimpleMFRC522() + self.key = b'Mellon!' + self.key += b' ' * (48 - len(self.key)) - def increment(self, counter): - # 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): + def check_authorization(self, reader_id, reader_data): # open database conn = create_connection(database) @@ -39,15 +26,14 @@ class DoorLock(): db_id = user[0] name = user[1] user_card_id = user[2] - user_counter = user[3] + if reader_id == user_card_id: - # check if use counter on the card matches counter in the database - # if counter is different -> assume the card has been cloned - if counter == user_counter: + print("card id match found") + if reader_data.encode() == self.key: print("user {} with card_id {} authorized".format(name, hex(reader_id))) return True, db_id else: - print("error: counter does not match! please investigate!") + print("incorrect key phrase") # if no match was found in the database: deny entry print("You shall not pass!") @@ -70,36 +56,17 @@ class DoorLock(): print("Hold card before reader..") uid, data = self.reader.read() - print("data = ", data) - #counter = int.from_bytes(data, byteorder='big') - try: - counter = int(data) - except ValueError: - print("error: data on the card could not be converted") - counter = None + print("card read: \n uid = {}\ndata = {}\n".format(hex(uid), data)) + authorized, db_id = self.check_authorization(uid, data) - if counter is not None: - print("card read: \n uid = {}\ncounter = {}\n".format(hex(uid), counter)) - authorized, db_id = self.check_authorization(uid, counter) + if authorized: + conn = create_connection(database) + register_access(conn, db_id) - if authorized: - # increment use counter on the card - increment_status = self.increment(counter) - - 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() + self.unlock_door() + else: + print("authentication failed") + self.release_the_kraken() time.sleep(1.5) finally: #GPIO.cleanup() @@ -117,7 +84,7 @@ if __name__ == "__main__": doors_of_durin = DoorLock() data = bytearray([0]*16) - #data = '0' + data = 'Mellon!' #write_success = doors_of_durin.reader.write(data) doors_of_durin.run_authorization() pass \ No newline at end of file