This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbmanager.py
341 lines (275 loc) · 10.7 KB
/
dbmanager.py
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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module for managing program-specific database interactions.
Contains one class, DBManager.
Usage example:
>>> db = DBManager()
>>> db.create_databases()
"""
import sqlite3
from tkinter import *
from tkinter import messagebox as mb
import ndv_cypher
"""
This file is part of Tkinter Password Manager.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
"""
INFO_BOX_TITLE = "Information"
ERROR_BOX_TITLE = "Error"
WARNING_TITLE = "Warning"
class DBManager:
"""
Class to manage all sqlite3 database operations.
Does not take any arguments.
Functions:
-create_databases(self)
-count_records(self)
-write_to_main(self, sitetext, untext, pwtext)
-write_to_keys(self, sitetext, untext, pwtext)
-write_to_log(self, date, user, success)
-read_log(self)
-clear_log(self, loginrec)
-read_all_from_db(self)
-clear_db(self, table, win)
-check_db(self)
-export_plain_db(self, record_dict, filename)
-append_to_list(box, param) [STATIC]
Usage example:
>>> manage_db = DBManager()
>>> manage_db.create_databases()
>>> number_of_records = manage_db.count_records()
>>> print (number_of_records)
62
"""
def __init__(self):
self.conn = sqlite3.connect("data/data.db")
self.cur = self.conn.cursor()
def create_databases(self):
"""
Creates password, key, and log databases if they don't already exist.
No args taken.
Usage example:
>>> manage_db = DBManager()
>>> manage_db.create_databases()
"""
self.cur.execute("CREATE TABLE IF NOT EXISTS passw_table(personID INTEGER PRIMARY KEY AUTOINCREMENT, "
"site STR, username STR, password STR)")
self.cur.execute("CREATE TABLE IF NOT EXISTS key_table(personID INTEGER PRIMARY KEY AUTOINCREMENT, "
"sitekey STR, usernamekey STR, passwordkey STR, FOREIGN KEY (personID) "
"REFERENCES passw_table(personID))") # primary keys are linked
self.cur.execute("CREATE TABLE IF NOT EXISTS log_table(logID INTEGER PRIMARY KEY AUTOINCREMENT, "
"date STR, user STR, success STR)")
def count_records(self):
"""
Counts records in password database.
No args taken.
Usage example:
>>> manage_db = DBManager()
>>> number_of_records = manage_db.count_records()
>>> print (number_of_records)
62
"""
num_of_records = 0
self.cur.execute("SELECT * FROM passw_table")
passwords = self.cur.fetchall()
for x in passwords:
num_of_records += 1
return num_of_records
def write_to_main(self, sitetext, untext, pwtext):
"""
Writes records to password database.
Args taken:
-sitetext
-untext
-pwtext
Usage example:
>>> manage_db = DBManager()
>>> sitetext = "Google"
>>> untext = "johndoe1@gmail.com"
>>> pwtext = "jhAS/123!"
>>> manage_db.write_to_main(sitetext, untext, pwtext)
"""
self.cur.execute("INSERT INTO passw_table (site, username, password) VALUES (?,?,?)", (sitetext, untext,
pwtext))
self.conn.commit()
def write_to_keys(self, sitetext, untext, pwtext):
"""
Writes keys for encrypted records to key database.
Args taken:
-sitetext
-untext
-pwtext
Usage example:
>>> manage_db = DBManager()
>>> sitetext, sitekey = ndv_cypher.VernamEncrypt.encrypt("Google")
>>> untext, unkey = ndv_cypher.VernamEncrypt.encrypt("johndoe1@gmail.com")
>>> pwtext, pwkey = ndv_cypher.VernamEncrypt.encrypt("jhAS/123!")
>>> manage_db.write_to_keys(sitekey, unkey, pwkey)
"""
self.cur.execute("INSERT INTO key_table (sitekey, usernamekey, passwordkey) VALUES (?,?,?)",
(sitetext, untext, pwtext))
self.conn.commit()
def write_to_log(self, date, user, success):
"""
Writes login date/time, user, and success to log.
Args taken:
-date (str)
-user (str)
-success (str)
Usage example:
>>> manage_db = DBManager()
>>> date = "2017-11-02 18:17:54"
>>> user = "admin"
>>> success = "Successful"
>>> manage_db.write_to_log(date, user, success)
"""
self.cur.execute("INSERT INTO log_table (date, user, success) VALUES (?,?,?)", (date, user, success))
self.conn.commit()
def read_log(self):
"""
Reads all records from the login record database.
No args taken.
Usage example:
>>> manage_db = DBManager()
>>> manage_db.read_log()
"""
box = []
all_records = self.cur.execute("SELECT * FROM log_table ORDER BY date DESC")
box = self.append_to_list(box, all_records)
return box
def clear_log(self, loginrec):
"""
Clears login records.
Args taken:
-loginrec (tkinter window 'loginrec')
Usage:
>>> manager = DBManager()
>>> loginrec = Tk()
>>> manager.clear_log(loginrec)
"""
result = mb.askquestion("Clear login records", "Are you sure?", icon='warning', parent=loginrec)
if result == 'yes':
self.clear_db('log_table')
mb.showinfo(INFO_BOX_TITLE, 'Login records cleared.')
loginrec.destroy() # no point keeping window open when there are no records to show
else:
mb.showinfo(INFO_BOX_TITLE, 'Login records not cleared.', parent=loginrec)
def read_all_from_db(self):
"""
Reads all records from both databases.
Returns records from each in two separate
lists of tuples.
No args taken.
Usage example:
>>> manage_db = DBManager()
>>> manage_db.read_all_from_db()
"""
databox = []
keybox = []
all_records = self.cur.execute("SELECT * FROM passw_table")
databox = self.append_to_list(databox, all_records)
all_keys = self.cur.execute("SELECT * FROM key_table")
self.append_to_list(keybox, all_keys)
if all_keys is None:
keybox = []
else:
keybox = self.append_to_list(keybox, all_keys)
return databox, keybox
def clear_db(self, table):
"""
Clears all records from a given databases. 'table' should be
"key_table", "passw_table", or "log_table" for this program.
Args taken:
-table (str - sqlite3 table)
Usage example:
>>> manage_db = DBManager()
>>> manage_db.clear_db("key_table")
"""
if table == "passw_table":
self.cur.execute("DELETE FROM passw_table")
self.conn.commit()
self.cur.execute("DELETE FROM SQLITE_SEQUENCE WHERE name='passw_table'")
# clears indexes so records always start from 1 (above)
self.conn.commit()
elif table == "key_table":
self.cur.execute("DELETE FROM key_table")
self.conn.commit()
self.cur.execute("DELETE FROM SQLITE_SEQUENCE WHERE name='key_table'")
self.conn.commit()
elif table == "log_table":
self.cur.execute("DELETE FROM log_table")
self.conn.commit()
self.cur.execute("DELETE FROM SQLITE_SEQUENCE WHERE name='log_table'")
self.conn.commit()
def check_db(self):
"""
Checks the database before encryption/decryption. This will
prevent either from happening if there is some kind of error.
Also calls "read_all_from_db()" and returns data and keys.
No args taken.
Usage example:
>>> manage_db = DBManager()
>>> go_ahead, databox, keybox = manage_db.check_db()
"""
databox, keybox = self.read_all_from_db()
# each go_ahead value differs slightly in meaning across functions, but usually 1 is 'Go ahead'
if databox == [] and keybox != []:
go_ahead = 0
elif databox != [] and keybox != []:
go_ahead = 1
else:
go_ahead = 2
return go_ahead, databox, keybox
def export_plain_db(self, record_dict, filename):
"""
Decrypts the password database and exports the
records to a new database.
Args taken:
-record_dict
-filename
Usage:
>>> db_manager = DBManager()
>>> dictionary = {1: ("Google", "example@gmail.com", "password")}
>>> db_manager.export_plain_db(dictionary, "output.db")
"""
conn_export = sqlite3.connect(filename) # new database is created
cur_export = conn_export.cursor()
cur_export.execute("CREATE TABLE IF NOT EXISTS passw_table(personID INTEGER PRIMARY KEY AUTOINCREMENT, "
"site STR, username STR, password STR)")
for item in record_dict:
site = record_dict[item][0]
username = record_dict[item][1]
password = record_dict[item][2]
cur_export.execute("INSERT INTO passw_table (site, username, password) VALUES (?,?,?)", (site, username,
password))
conn_export.commit()
@staticmethod
def append_to_list(box, param):
"""
Takes retrieved records from a database and appends them to a
list, in multidimentional form.
Args taken:
-box
-param
Usage example:
>>> manage_db = DBManager()
>>> searchingtext = "Letmein123!"
>>> p = manage_db.cur.execute("SELECT * FROM passw_table WHERE password Like (?)",
>>> ('%'+searchingtext+'%',))
>>> box = manage_db.append_to_list(box, p)
"""
items = param.fetchall() # param is the values retrieved from the database
for item in items:
if item not in box:
box.append(item)
return box # box can be any list given to the function