-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
348 lines (273 loc) · 10.3 KB
/
database.py
File metadata and controls
348 lines (273 loc) · 10.3 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import sqlite3
import os
import base64
from config import DB_FILE, CURRENT_USER
from crypto import md5, encrypt_data, decrypt_data, generate_aes_key
def initialize_database():
"""
Creates the database and necessary tables if they don't exist.
"""
# Only create directory if DB_FILE has a directory component
db_dir = os.path.dirname(DB_FILE)
if db_dir: # Only try to create directory if there is one specified
os.makedirs(db_dir, exist_ok=True)
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
# Create users table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
username TEXT PRIMARY KEY,
password_hash TEXT NOT NULL,
encryption_key TEXT NOT NULL
)
''')
# Check if the old credentials table exists
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='credentials'")
old_table_exists = cursor.fetchone() is not None
if old_table_exists:
# Check if we need to migrate (check if encrypted_website column exists)
cursor.execute("PRAGMA table_info(credentials)")
columns = [column[1] for column in cursor.fetchall()]
if "encrypted_website" not in columns:
# We need to migrate data from old schema to new schema
migrate_credentials_data(conn, cursor)
else:
# Table already has the new schema, no migration needed
pass
else:
# Create new credentials table with all fields encrypted
cursor.execute('''
CREATE TABLE IF NOT EXISTS credentials (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
encrypted_website TEXT NOT NULL,
encrypted_site_username TEXT NOT NULL,
encrypted_password TEXT NOT NULL,
FOREIGN KEY (username) REFERENCES users(username)
)
''')
conn.commit()
conn.close()
def migrate_credentials_data(conn, cursor):
"""
Migrates data from the old credentials schema to the new schema with all fields encrypted.
"""
# Rename the old table
cursor.execute("ALTER TABLE credentials RENAME TO credentials_old")
# Create the new table
cursor.execute('''
CREATE TABLE credentials (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
encrypted_website TEXT NOT NULL,
encrypted_site_username TEXT NOT NULL,
encrypted_password TEXT NOT NULL,
FOREIGN KEY (username) REFERENCES users(username)
)
''')
# Get all users to migrate their data
cursor.execute("SELECT username, encryption_key FROM users")
users = cursor.fetchall()
for user, key_b64 in users:
# Get the encryption key
key = base64.b64decode(key_b64)
# Get all credentials for this user
cursor.execute(
"SELECT id, website, site_username, encrypted_password FROM credentials_old WHERE username = ?",
(user,)
)
credentials = cursor.fetchall()
# Migrate each credential
for cred_id, website, site_username, encrypted_password in credentials:
# Encrypt the website and site_username
encrypted_website = encrypt_data(website, key)
encrypted_site_username = encrypt_data(site_username, key)
# Insert into the new table
cursor.execute(
"INSERT INTO credentials (id, username, encrypted_website, encrypted_site_username, encrypted_password) VALUES (?, ?, ?, ?, ?)",
(cred_id, user, encrypted_website, encrypted_site_username, encrypted_password)
)
# Drop the old table
cursor.execute("DROP TABLE credentials_old")
conn.commit()
def user_exists(username):
"""
Checks if a user exists in the database.
Args:
username (str): The username to check.
Returns:
bool: True if the user exists, False otherwise.
"""
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM users WHERE username = ?", (username,))
count = cursor.fetchone()[0]
conn.close()
return count > 0
def any_users_exist():
"""
Checks if any users exist in the database.
Returns:
bool: True if at least one user exists, False otherwise.
"""
# Make sure the database exists
if not os.path.exists(DB_FILE):
return False
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
try:
cursor.execute("SELECT COUNT(*) FROM users")
count = cursor.fetchone()[0]
conn.close()
return count > 0
except sqlite3.OperationalError:
# Table doesn't exist yet
conn.close()
return False
def add_user(username, password):
"""
Adds a new user to the database.
Args:
username (str): The username for the new user.
password (str): The password for the new user.
Returns:
bool: True if the user was added successfully, False otherwise.
"""
# Check if the user already exists
if user_exists(username):
raise ValueError("Username already exists. Choose a different one.")
# Hash the password
password_hash = md5(password)
# Generate an encryption key for this user
key = generate_aes_key()
key_b64 = base64.b64encode(key).decode('utf-8')
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute(
"INSERT INTO users (username, password_hash, encryption_key) VALUES (?, ?, ?)",
(username, password_hash, key_b64)
)
conn.commit()
conn.close()
return True
def validate_user(username, password):
"""
Validates a user's credentials.
Args:
username (str): The username to validate.
password (str): The password to validate.
Returns:
bool: True if the credentials are valid, False otherwise.
"""
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute(
"SELECT password_hash, encryption_key FROM users WHERE username = ?",
(username,)
)
result = cursor.fetchone()
conn.close()
if not result:
return False
stored_hash, key_b64 = result
# Hash the provided password and compare
if md5(password) == stored_hash:
# Store the encryption key in the CurrentUser class
key = base64.b64decode(key_b64)
CURRENT_USER.set_key(key)
return True
return False
def add_credential(website, site_username, password):
"""
Adds a new credential to the database with all fields encrypted.
Args:
website (str): The website for the credential.
site_username (str): The username for the website.
password (str): The password for the website.
Returns:
bool: True if the credential was added successfully, False otherwise.
"""
current_user = CURRENT_USER.get()
if not current_user:
raise ValueError("No user is currently logged in")
# Get the encryption key
key = CURRENT_USER.get_key()
# Encrypt all fields
encrypted_website = encrypt_data(website, key)
encrypted_site_username = encrypt_data(site_username, key)
encrypted_password = encrypt_data(password, key)
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute(
"INSERT INTO credentials (username, encrypted_website, encrypted_site_username, encrypted_password) VALUES (?, ?, ?, ?)",
(current_user, encrypted_website, encrypted_site_username, encrypted_password)
)
conn.commit()
conn.close()
return True
def get_credentials(search_term=None):
"""
Gets all credentials for the current user, optionally filtered by a search term.
Args:
search_term (str, optional): A term to search for in website or username.
Returns:
list: A list of dictionaries containing credential information.
"""
current_user = CURRENT_USER.get()
if not current_user:
raise ValueError("No user is currently logged in")
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
# Get all credentials for the current user
cursor.execute(
"SELECT id, encrypted_website, encrypted_site_username, encrypted_password FROM credentials WHERE username = ?",
(current_user,)
)
rows = cursor.fetchall()
conn.close()
# Get the encryption key
key = CURRENT_USER.get_key()
# Decrypt all fields and format results
credentials = []
for row in rows:
id, encrypted_website, encrypted_site_username, encrypted_password = row
# Decrypt all fields
website = decrypt_data(encrypted_website, key)
site_username = decrypt_data(encrypted_site_username, key)
password = decrypt_data(encrypted_password, key)
# If search term is provided, filter results
if search_term and search_term.lower() not in website.lower() and search_term.lower() not in site_username.lower():
continue
credentials.append({
"id": id,
"website": website,
"username": site_username,
"password": password
})
return credentials
def delete_credential(credential_id):
"""
Deletes a credential from the database.
Args:
credential_id (int): The ID of the credential to delete.
Returns:
bool: True if the credential was deleted successfully, False otherwise.
"""
current_user = CURRENT_USER.get()
if not current_user:
raise ValueError("No user is currently logged in")
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
# Verify the credential belongs to the current user
cursor.execute(
"SELECT COUNT(*) FROM credentials WHERE id = ? AND username = ?",
(credential_id, current_user)
)
if cursor.fetchone()[0] == 0:
conn.close()
raise ValueError("Credential not found or does not belong to the current user")
# Delete the credential
cursor.execute("DELETE FROM credentials WHERE id = ?", (credential_id,))
conn.commit()
conn.close()
return True