-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDataDatabase.py
More file actions
157 lines (113 loc) · 6.72 KB
/
UserDataDatabase.py
File metadata and controls
157 lines (113 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import sqlite3
from pysqlitecipher import sqlitewrapper
from dotenv import dotenv_values # Load environment variables for things usernames, passwords, and API keys
import bcrypt
import GlobalConstants as GC
STATIC_DEFAULT_NETWORK = '''
graph LR;
A[UniFi PoE Switch] --> B[ROOM: Master Bedroom];
A[UniFi PoE Switch] --> F[ZimaBoard Server];
F[CPU: ZimaBoard Server] --> E[DISPLAY: Main Central Control];
B[ROOM: Master Bedroom] --> C[LIGHT: Master Bedroom];
B[ROOM: Master Bedroom] --> D[DISPLAY: Master Bedroom];
A[UniFi PoE Switch] --> G[LIGHT-Kitchen];
style A color:#000000, fill:#03C04A, stroke:#000000;
style B color:#000000, fill:#03COFF, stroke:#000000;
style C color:#000000, fill:#FFC04A, stroke:#000000;
style D color:#FFFFFF, fill:#1F1F1F, stroke:#000000;
style E color:#FFFFFF, fill:#1F1F1F, stroke:#000000;
style F color:#000000, fill:#B8191D, stroke:#000000;
style G color:#000000, fill:#FFC04A, stroke:#000000;
'''
class UserDataDatabase:
def __init__(self):
# Connect to the database (create if it doesn't exist)
self.conn = sqlite3.connect('UserData.db')
self.cursor = self.conn.cursor()
self.cursor.execute('''CREATE TABLE IF NOT EXISTS CountableNounsTable (id INTEGER PRIMARY KEY, words TEXT)''')
self.cursor.execute('''CREATE TABLE IF NOT EXISTS UsersTable (id INTEGER PRIMARY KEY, username TEXT, password TEXT, salt TEXT)''')
self.cursor.execute('''CREATE TABLE IF NOT EXISTS NetworkStateTable (id INTEGER PRIMARY KEY, mermaidString TEXT)''')
# https://medium.com/@harshnative/encrypting-sqlite-database-in-python-using-pysqlitecipher-module-23b80129fda0
sqliteWrapperEnvironmentVariables = dotenv_values()
sqliteWrapperPassword = sqliteWrapperEnvironmentVariables['SQLITE_WRAPPER_PASSWORD']
self.obj = sqlitewrapper.SqliteCipher(dataBasePath="UserData.db", checkSameThread=False, password=sqliteWrapperPassword)
try:
self.obj.createTable("encrytedUsersTable" , ["username", "password", "salt"], makeSecure=True , commit=True)
#self.obj.createTable("unencrytedUsersTable", ["username", "password", "salt"], makeSecure=False , commit=True)
except ValueError:
pass
print("DO NOTHING encryted table already exists")
def commitChanges(self):
self.conn.commit()
def closeDatabase(self):
self.conn.close()
def queryDatabase(self, tableName):
sqlStatement = f"SELECT * FROM {tableName}"
self.cursor.execute(sqlStatement)
result = self.cursor.fetchall()
return result
def insertIntoNetworkStateTable(self, currentNetworkState):
""" Insert data into NetworkStateTable. When passing single variable into a row data must be followed by a comma, else (data1, data2, data3)
Args:
db (HouseDatabase Object): SQLite object store in a .db file
currentNetworkState (String): Network node configuration as String in NiceGUI Meraid formatted https://mermaid.js.org
"""
self.cursor.execute("INSERT INTO NetworkStateTable (mermaidString) VALUES (?)", (currentNetworkState,))
def insertIntoUserTable(self, un, pw):
""" Insert username, hashed password, and hash salt into the User Table if username is unqiue, otherwise ignore repeat user
Args:
un (String): Username to login, which can be either a 10 digit phone number or email address
pw (String): Password to login, which is NEVER stored as plain text in any database or on a SSD (RAM only)
"""
#self.cursor.execute("SELECT * FROM UsersTable WHERE username LIKE ?", ('%' + un + '%',))
#results = self.cursor.fetchall()
colList, results = db.obj.getDataFromTable("encrytedUsersTable" , raiseConversionError = True , omitID = False)
print(results)
isUserFound = False
for user in usersDatabaseList:
if user[GC] == "b.sandersnv@gmail.com":
isUserFound = True
print(user[passwordColumnNumber])
storedHashedPassword = user[passwordColumnNumber]
storedSalt = user[saltColumnNumber]
#try:
hashedPasssword = bcrypt.hashpw(password.encode('utf-8'), storedSalt)
if hashedPasssword == storedHashedPassword:
print("Password matches!")
else:
print("Invalid password.")
else:
isUserFound = isUserFound or False
print(isUserFound)
if len(results) > 0:
pass #Ignore repeat username and DO NOTHING
else:
generatedSalt = bcrypt.gensalt()
hashedPassword = bcrypt.hashpw(pw.encode('utf-8'), generatedSalt)
#self.cursor.execute("INSERT INTO UsersTable (username, password, salt) VALUES (?, ?, ?)", (un, hashedPassword, generatedSalt))
self.obj.insertIntoTable("encrytedUsersTable" , [un, hashedPassword, generatedSalt], commit = True)
def searchCountableNounsTable(self, searchTerm):
self.cursor.execute("SELECT * FROM CountableNounsTable WHERE words LIKE ?", ('%' + searchTerm + '%',))
results = self.cursor.fetchall()
return results
if __name__ == "__main__":
print("Creating new table")
db = HomeDatabase()
#db.insertIntoNetworkStateTable(STATIC_DEFAULT_NETWORK)
#db.closeDatabase()
db.insertIntoUserTable("blaze.sanders@gentex.com", "GentexPassword")
db.insertIntoUserTable("blazes.d.a.sanders@gmail.com", "TestPassword")
db.insertIntoUserTable("b.sanders.nv@gmail.com", "BadPassword")
db.commitChanges()
password = "BadPassword"
usernameColumnNumber = 1
passwordColumnNumber = 2
saltColumnNumber = 3
#databaseSearch = db.searchCountableNounsTable("Dog")
#print(databaseSearch)
#db.obj.insertIntoTable("unencrytedUsersTable" , ["blaze.d.a.sanders@gmail.com", "Password", "Salt"], commit = True)
db.obj.insertIntoTable("encrytedUsersTable" , ["blaze.d.a.sanders@gmail.com", "12345678", "wl2k45!"], commit = True)
blazeUser = db.obj.getDataFromTable("encrytedUsersTable" , raiseConversionError = True , omitID = False)
#blazeUser = db.obj.getDataFromTable("unencrytedUsersTable" , raiseConversionError = True , omitID = False)
print(blazeUser)
db.closeDatabase()