From 169e8461d253042beeb4fecb2e1d26ce49f356c4 Mon Sep 17 00:00:00 2001 From: Ondrej Mikle Date: Sat, 19 Jul 2014 22:41:46 +0200 Subject: [PATCH] UID Authenticator --- brmdoor_authenticator.py | 76 +++++++++++++++++++++++++++++++++++++ create_authenticator_db.py | 22 +++++++++++ test_uids_db.sqlite | Bin 0 -> 2048 bytes 3 files changed, 98 insertions(+) create mode 100644 brmdoor_authenticator.py create mode 100755 create_authenticator_db.py create mode 100644 test_uids_db.sqlite diff --git a/brmdoor_authenticator.py b/brmdoor_authenticator.py new file mode 100644 index 0000000..ed76ea3 --- /dev/null +++ b/brmdoor_authenticator.py @@ -0,0 +1,76 @@ +import sqlite3 + +class UidRecord(object): + """Represents UID<->nick pair""" + + def __init__(self, uid_hex, nick): + """ + Create instance binding UID to nick. UIDs should be either 4, 7 + or 10 bytes long, but that's ISO14443 thing - this object has + no such limitation. + + UID will be stored in uppercase hex, converted if necessary. + + @param uid_hex: uid in hex + @param nick: nickname this UID belongs to + """ + self.uid_hex = uid_hex.upper() + self.nick = nick + + def __str__(self): + return "(uid: %s, nick: %s)" % (self.uid_hex, self.nick) + + def __repr__(self): + return "" % (self.uid_hex, self.nick) + +class UidAuthenticator(object): + """Checks UIDs of ISO14443 RFID cards against database.""" + + def __init__(self, filename): + """ + Connects to database by given filename and later checks UIDs + against that database. + """ + #open in autocommit mode - we are not changing anything + self.conn = sqlite3.connect(filename, isolation_level=None) + + def fetchUidRecord(self, uid_hex): + """ + Returns first record that matches given UID or None if nothing + is found. + + @param uid_hex: uid to match in hex + @returns UidRecord instance if found, None otherwise + """ + cursor = self.conn.cursor() + sql = "SELECT nick FROM authorized_uids WHERE UPPER(uid_hex)=?" + sql_data =(uid_hex.upper(),) + + cursor.execute(sql, sql_data) + record = cursor.fetchone() + + if record is None: + return None + + nick = record[0] + return UidRecord(uid_hex, nick) + + def shutdown(self): + """Closes connection to database""" + self.conn.close() + + +#test routine +if __name__ == "__main__": + authenticator = UidAuthenticator("test_uids_db.sqlite") + + record = authenticator.fetchUidRecord("043a1482cc2280") + print "For UID 043a1482cc2280 we found:", str(record) + + record = authenticator.fetchUidRecord("34795fad") + print "For UID 34795fad we found:", str(record) + + record = authenticator.fetchUidRecord("01020304") + print "For UID 01020304 we found:", str(record) + + authenticator.shutdown() diff --git a/create_authenticator_db.py b/create_authenticator_db.py new file mode 100755 index 0000000..ba22819 --- /dev/null +++ b/create_authenticator_db.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +""" +Creates and empty sqlite database file for brmdoor_authenticator.UidAuthenticator. + +Give filename as first argument. +""" + +import sys +import sqlite3 + +if __name__ == "__main__": + if len(sys.argv) < 2: + print >> sys.stderr, "You must specify filename as arg1 where the DB is to be created" + + filename = sys.argv[1] + conn = sqlite3.connect(filename) + cursor = conn.cursor() + + cursor.execute("CREATE TABLE authorized_uids(uid_hex, nick)") + conn.commit() + conn.close() diff --git a/test_uids_db.sqlite b/test_uids_db.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..1580cde1098ba53da349fba7289be5b0633b863d GIT binary patch literal 2048 zcmWFz^vNtqRY=P(%1ta$FlJz3U}R))P*7lCU|$n*~GQA8IwyAlX6lMOG`5Hi!!THQ{qcAQ;N~JOwK{B zjv=lJA&yQyt_o<98bA&48L1UI3VE5y*_xod$;5nuf%z$r7zLvtFd70wG6bZVm}Pa1 zP0TG#(-KocJzW%n^urZ=Gt&}_QWcCO8JRU@4NQy^4NWYJl9P>$EDT&yi_