mirror of
https://github.com/brmlab/brmdoor_libnfc.git
synced 2025-06-08 08:34:00 +02:00
UID Authenticator
This commit is contained in:
parent
cfabc90911
commit
169e8461d2
3 changed files with 98 additions and 0 deletions
76
brmdoor_authenticator.py
Normal file
76
brmdoor_authenticator.py
Normal file
|
@ -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 "<UidRecord: uid: %s, nick: %s>" % (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()
|
22
create_authenticator_db.py
Executable file
22
create_authenticator_db.py
Executable file
|
@ -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()
|
BIN
test_uids_db.sqlite
Normal file
BIN
test_uids_db.sqlite
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue