-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
488 lines (414 loc) · 20.3 KB
/
gui.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QPixmap
from passhandler import *
from databaseinterface import *
import sys
from datetime import datetime, timedelta
'''
ToDO:
Enhance GUI to include delete password or Service
'''
#variables
database = r"pass-store.db"
class Controller:
def __init__(self):
global dbpassword
pass
def start_menu(self):
self.startmenu = StartMenu()
self.startmenu.switch_to_servicewindow.connect(self.service_list)
self.startmenu.switch_to_servicewindow.connect(self.close_startmenu)
self.startmenu.show()
def service_list(self):
self.servicelist = ServiceList()
self.servicelist.updatelist()
self.servicelist.switch_to_startmenu.connect(self.start_menu)
self.servicelist.switch_to_startmenu.connect(self.close_servicelist)
self.servicelist.switch_to_newpassword.connect(self.new_password)
self.servicelist.switch_to_newpassword.connect(self.close_servicelist)
self.servicelist.switch_to_newservice.connect(self.new_service)
self.servicelist.switch_to_newservice.connect(self.close_servicelist)
self.servicelist.switch_to_service_passlist.connect(self.database_passwindowload)
self.servicelist.switch_to_service_passlist.connect(self.close_servicelist)
self.servicelist.show()
def new_service(self):
self.newservice = NewService()
self.newservice.switch_to_servicewindow.connect(self.service_list)
self.newservice.switch_to_servicewindow.connect(self.close_newservice)
self.newservice.switch_to_startmenu.connect(self.start_menu)
self.newservice.switch_to_startmenu.connect(self.close_newservice)
self.newservice.new_service_confirmation.connect(self.new_serviceconfirmation)
self.newservice.show()
def new_password(self):
self.newpassword = NewPassword() # Make class
self.newpassword.switch_to_servicewindow.connect(self.service_list)
self.newpassword.switch_to_servicewindow.connect(self.close_newpassword)
self.newpassword.switch_to_databasepasswindowsave.connect(self.database_passwindowsave)
self.newpassword.switch_to_databasepasswindowsave.connect(self.close_newpassword)
self.newpassword.show()
def database_passwindowload(self):
self.databasepasswindowload = DatabasePassWindowLoad()
self.databasepasswindowload.close_databasepasswindowload.connect(self.close_database_passwindowload)
self.databasepasswindowload.servicepasslist.connect(self.service_passlist)
self.databasepasswindowload.show()
def service_passlist(self):
self.servicepasslist = ServicePassList()
self.servicepasslist.switch_to_startmenu.connect(self.start_menu)
self.servicepasslist.updatelist()
self.servicepasslist.closeservicepasslist.connect(self.close_servicepasslist)
self.servicepasslist.show()
def database_passwindowsave(self):
self.databasepasswindowsave = DatabasePassWindowSave()
self.databasepasswindowsave.close_databasepasswindowsave.connect(self.close_database_passwindowsave)
self.databasepasswindowsave.servicepasslist.connect(self.service_list)
self.databasepasswindowsave.show()
def close_startmenu(self):
self.startmenu.close()
def close_servicelist(self):
self.servicelist.close()
def close_newservice(self):
self.newservice.close()
def close_database_passwindowload(self):
#self.dbpassword = self.databasepasswindowload.dbpassword
self.service_passlist()
self.databasepasswindowload.close()
def close_database_passwindowsave(self):
self.databasepasswindowsave.close()
def new_serviceconfirmation(self):
self.newserviceconfirmation = NewServiceConfirmation()
self.newserviceconfirmation.show()
def close_newpassword(self):
self.newpassword.close()
def close_servicepasslist(self):
self.servicepasslist.close()
class StartMenu(QtWidgets.QWidget):
switch_to_servicewindow = QtCore.pyqtSignal()
switch_to_loadpasswordswindow = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle("Robbie's Password Manager")
layout = QtWidgets.QGridLayout()
layout.setAlignment(QtCore.Qt.AlignCenter)
#self.layout.setAlignment(alignment = QtCore.Qt.AlignCenter)
#define widgets items
self.image = QtWidgets.QLabel(self)
self.pixmap = QPixmap('source.png').scaledToWidth(100)
self.image.setPixmap(self.pixmap)
self.image.setAlignment(QtCore.Qt.AlignCenter)
self.label = QtWidgets.QLabel("""<br><h1>Welcome to Robbie's easy Password Manager</h1><br>This tool securely salts and hashes usernames and passwords and saves them in an SQL database. <br><br>-------------------<br> <br><b>This has not been completely security checked so I would not recommend you use this for BAU.</b> <br><br>-------------------<br><br>Database name: <b><u>"""+str(database)+"""</b></u> <i>You can change this in "gui.py" </i><br><br> Click Next to get started! <br>""")
self.label.setTextFormat(1)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.button = QtWidgets.QPushButton("Next!")
self.button.clicked.connect(self.service_list)
#add widgets to layout
layout.addWidget(self.label)
layout.addWidget(self.image)
layout.addWidget(self.button)
self.setLayout(layout)
def service_list(self):
self.switch_to_servicewindow.emit()
class ServiceList(QtWidgets.QWidget):
switch_to_startmenu = QtCore.pyqtSignal()
switch_to_newpassword= QtCore.pyqtSignal()
switch_to_newservice= QtCore.pyqtSignal()
switch_to_service_passlist = QtCore.pyqtSignal()
switch_to_databasepasswindowload = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.resize(800,600)
self.setWindowTitle("Robbie's Password Manager")
def updatelist(self):
layout = QtWidgets.QGridLayout()
self.allservices = select_service_table(database)
if self.allservices == None:
self.noservicelabel = QtWidgets.QLabel("There are no services saved in the database yet.<br><br><br> <b><u>Create a service first!</b></u><br><br> Then you can encrypt a username and password for that service")
self.noservicelabel.setTextFormat(1)
layout.addWidget(self.noservicelabel)
else:
self.headers = self.allservices[0]+("number of passwords saved",)
rows= self.allservices[1:]
for i in range(len(rows)):
row = rows[i]
service_id = row[0]
passwordcount = count_passwords(database,service_id)
row = row + tuple(str(passwordcount))
rows[i]=row
self.model = TableModel(rows, self.headers)
self.table = QtWidgets.QTableView()
self.table.clicked.connect(self.clickedserviceid)
self.table.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
self.table.setModel(self.model)
self.header = self.table.horizontalHeader()
self.header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
layout.addWidget(self.table)
#buttons
self.newservicebutton = QtWidgets.QPushButton("Create new Service")
self.newservicebutton.clicked.connect(self.new_service)
self.savepasswordbutton = QtWidgets.QPushButton("Encrypt a Username and Password attached to Selected service")
self.savepasswordbutton.clicked.connect(self.new_password)
self.servicepasslistbutton = QtWidgets.QPushButton("Load Passwords for Selected service")
self.servicepasslistbutton.clicked.connect(self.service_passlist)
self.backbutton = QtWidgets.QPushButton("Back")
self.backbutton.clicked.connect(self.start_menu)
layout.addWidget(self.newservicebutton)
layout.addWidget(self.savepasswordbutton)
layout.addWidget(self.servicepasslistbutton)
layout.addWidget(self.backbutton)
#end buttons
self.setLayout(layout)
def start_menu(self):
self.switch_to_startmenu.emit()
def new_password(self):
global service_id
if service_id != 0:
self.switch_to_newpassword.emit()
return service_id
def new_service(self):
self.switch_to_newservice.emit()
def service_passlist(self):
global service_id
if service_id != 0:
self.switch_to_service_passlist.emit()
return service_id
def clickedserviceid(self, signal):
global service_id
row = signal.row()
service_id = str(signal.sibling(signal.row(),0).data())
return service_id
class NewPassword(QtWidgets.QWidget):
switch_to_servicewindow = QtCore.pyqtSignal()
new_password_confirmation = QtCore.pyqtSignal()
switch_to_databasepasswindowsave = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
global now
global expiry_time
self.setWindowTitle("Add a new password to the selected service")
layout = QtWidgets.QGridLayout()
# columns required: id, usernamehash, passwordhash, salt, service_id, creation_date, expiry_date
self.service_name = read_service_name(database,service_id)
self.label = QtWidgets.QLabel("""\n\nEnter credentials of new password to be stored securely in the database for the service: \n" """+self.service_name+""" ": """)
self.userlabel = QtWidgets.QLabel("Username")
self.username = QtWidgets.QLineEdit()
self.passlabel = QtWidgets.QLabel("Password")
self.password = QtWidgets.QLineEdit()
now = str(datetime.now().isoformat())
expiry_time = str((datetime.now() + timedelta(days=90)).isoformat())
self.datelabel = QtWidgets.QLabel("Service will be created with the following date/time:\nCreation time: "+now+"\nExpiry date/time: "+expiry_time)
self.createpasswordbutton = QtWidgets.QPushButton("Encrpyt Username and Password in database")
self.createpasswordbutton.clicked.connect(self.create_password)
self.backbutton = QtWidgets.QPushButton("Back")
self.backbutton.clicked.connect(self.back_button)
layout.addWidget(self.label)
layout.addWidget(self.userlabel)
layout.addWidget(self.username)
layout.addWidget(self.passlabel)
layout.addWidget(self.password)
layout.addWidget(self.datelabel)
layout.addWidget(self.createpasswordbutton)
layout.addWidget(self.backbutton)
self.setLayout(layout)
def create_password(self):
###database call here.
global usernameinput
usernameinput= self.username.text()
global passwordinput
passwordinput = self.password.text()
self.switch_to_databasepasswindowsave.emit()
#passcontroller =Controller()
#passcontroller.database_passwindowsave()
def back_button(self):
self.switch_to_servicewindow.emit()
class NewService(QtWidgets.QWidget):
switch_to_servicewindow = QtCore.pyqtSignal()
switch_to_startmenu = QtCore.pyqtSignal()
new_service_confirmation = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle("Add a new service to the database")
layout = QtWidgets.QGridLayout()
self.label = QtWidgets.QLabel("\n\nEnter name of new service to add to the database:")
self.service_name = QtWidgets.QLineEdit()
self.createservicebutton = QtWidgets.QPushButton("Create Service in database")
self.createservicebutton.clicked.connect(self.create_service)
self.now = str(datetime.now().isoformat())
#no expiry included on service table
#self.expiry_time = (datetime.now() + timedelta(days=90)).isoformat()
self.datelabel = QtWidgets.QLabel("Service will be created with the following date/time:\nCreation time: "+self.now)
self.backbutton = QtWidgets.QPushButton("Back")
self.backbutton.clicked.connect(self.back_button)
layout.addWidget(self.label)
layout.addWidget(self.service_name)
layout.addWidget(self.datelabel)
layout.addWidget(self.createservicebutton)
layout.addWidget(self.backbutton)
self.setLayout(layout)
def back_button(self):
self.switch_to_servicewindow.emit()
def create_service(self):
global new_service_name
new_service_name = self.service_name.text()
service = [new_service_name,self.now]
global service_id
service_id = service_creation(database,service)
self.new_service_confirmation.emit()
self.switch_to_servicewindow.emit()
class DatabasePassWindowSave(QtWidgets.QWidget):
close_databasepasswindowsave = QtCore.pyqtSignal()
servicepasslist = QtCore.pyqtSignal() #connect this signal to the update of the database password list view.
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle("Please Enter Database Password")
layout = QtWidgets.QGridLayout()
self.label = QtWidgets.QLabel("<br><br>Enter Database Password to get access:<br><br>")
self.label.setTextFormat(1)
layout.addWidget(self.label)
self.password = QtWidgets.QLineEdit()
self.password.setEchoMode(2)
layout.addWidget(self.password)
self.warninglabel = QtWidgets.QLabel("<h3>DO NOT FORGET THIS PASSWORD or your passwords cannot be unencrypted</h3>")
self.warninglabel.setTextFormat(1)
layout.addWidget(self.warninglabel)
self.submitbutton = QtWidgets.QPushButton("Submit")
self.submitbutton.clicked.connect(self.submit_button)
self.cancelbutton = QtWidgets.QPushButton("Cancel")
self.cancelbutton.clicked.connect(self.cancel_button)
layout.addWidget(self.submitbutton)
layout.addWidget(self.cancelbutton)
self.setLayout(layout)
def submit_button(self):
global dbpassword
global now
global expiry_time
dbpassword = self.password.text().encode()
#self.servicepasslist
self.servicepasslist.emit()
self.close_databasepasswindowsave.emit()
salt = os.urandom(16)
#dbpassword=b"1234"
passhash = hash(dbpassword, passwordinput.encode(), salt)
userhash = hash(dbpassword, usernameinput.encode(), salt)
identity = userhash, passhash, salt, service_id, now, expiry_time
new_password_id = password_creation(database,identity)
#return dbpassword
def cancel_button(self):
self.close_databasepasswindowsave.emit()
class DatabasePassWindowLoad(QtWidgets.QWidget):
close_databasepasswindowload = QtCore.pyqtSignal()
servicepasslist = QtCore.pyqtSignal() #connect this signal to the update of the database password list view.
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle("Please Enter Database Password")
layout = QtWidgets.QGridLayout()
self.label = QtWidgets.QLabel("\n\nEnter Database Password to get access:\n\n")
layout.addWidget(self.label)
self.password = QtWidgets.QLineEdit()
self.password.setEchoMode(2)
layout.addWidget(self.password)
self.submitbutton = QtWidgets.QPushButton("Submit")
self.submitbutton.clicked.connect(self.submit_button)
self.cancelbutton = QtWidgets.QPushButton("Cancel")
self.cancelbutton.clicked.connect(self.cancel_button)
layout.addWidget(self.submitbutton)
layout.addWidget(self.cancelbutton)
self.setLayout(layout)
def submit_button(self):
global dbpassword
dbpassword = self.password.text()
#self.servicepasslist
self.servicepasslist.emit()
self.close_databasepasswindowload.emit()
def cancel_button(self):
self.close_databasepasswindowload.emit()
class NewServiceConfirmation(QtWidgets.QWidget):
close_databasepasswindowload = QtCore.pyqtSignal()
servicepasslist = QtCore.pyqtSignal() #connect this signal to the update of the database password list view.
def __init__(self):
global new_service_name
global service_id
QtWidgets.QWidget.__init__(self)
self.setWindowTitle("New Service Created Successfully")
layout = QtWidgets.QGridLayout()
self.label = QtWidgets.QLabel("\nNew Service created with name: "+str(new_service_name)+"\nService Id: "+str(service_id))
layout.addWidget(self.label)
self.setLayout(layout)
class ServicePassList (QtWidgets.QWidget):
switch_to_startmenu = QtCore.pyqtSignal()
closeservicepasslist = QtCore.pyqtSignal()
def __init__(self,):
global dbpassword
QtWidgets.QWidget.__init__(self)
if type(dbpassword) != bytes:
dbpassword = dbpassword.encode('utf-8')
self.resize(600,300)
self.setWindowTitle("Service Password List")
def updatelist(self):
global dbpassword
self.layout = QtWidgets.QGridLayout()
if type(dbpassword) != bytes:
dbpassword= dbpassword.encode('utf-8')
rows= select_all_service_passwords(database,service_id)
for i in range(len(rows)):
row = rows[i]
self.serv_id, self.serv_name, self.pass_id, self.usernamehash, self.passwordhash, self.salt, self.expiry_date = row
self.password = dehash(dbpassword, self.passwordhash, self.salt)
self.username = dehash(dbpassword, self.usernamehash, self.salt)
unencrypted_row = self.serv_id, self.serv_name, self.pass_id, self.username, self.password, self.salt, self.expiry_date
rows[i] = unencrypted_row
self.headers = ["service id","service name","password id","username","password","salt","password expiry date"]
self.model = TableModel(rows,self.headers)
self.table = QtWidgets.QTableView()
self.table.clicked.connect(self.viewClicked)
self.table.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
self.table.setModel(self.model)
self.table.horizontalHeader()
self.layout.addWidget(self.table)
self.backbutton = QtWidgets.QPushButton("Back to start")
self.backbutton.clicked.connect(self.back_button)
self.layout.addWidget(self.backbutton)
self.setLayout(self.layout)
def back_button(self):
self.switch_to_startmenu.emit()
self.closeservicepasslist.emit()
def viewClicked(self, clickedIndex):
row = clickedIndex.row()
model = clickedIndex.model()
'''Below Table Model class taken from example here: https://www.learnpyqt.com/courses/model-views/qtableview-modelviews-numpy-pandas/
"headerData" and "sort" are from https://stackoverflow.com/questions/52332534/column-sorting-not-working-when-using-custom-headerview
Docs require the subclassing of the PyQT5 Abstract Table model class
'''
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data,headerdata):
super(TableModel, self).__init__()
self._data = data
self.headerdata = headerdata
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
# See below for the nested-list data structure.
# .row() indexes into the outer list,
# .column() indexes into the sub-list
return self._data[index.row()][index.column()]
def rowCount(self, index):
# The length of the outer list.
return len(self._data)
def columnCount(self, index):
# The following takes the first sub-list, and returns
# the length (only works if all rows are an equal length)
return len(self._data[0])
def headerData(self, col, orientation, role):
if orientation==QtCore.Qt.Horizontal and role==QtCore.Qt.DisplayRole:
return self.headerdata[col]
return None
def sort(self,col,order):
self.layoutAboutToBeChanged.emit()
self.arraydata=sorted(self.arraydata,key=operator.itemgetter(col))
if order==QtCore.Qt.DescendingOrder:
self.arraydata.reverse()
self.layoutChanged.emit()
def main():
app = QtWidgets.QApplication(sys.argv)
controller = Controller()
controller.start_menu()
sys.exit(app.exec_())
if __name__ == '__main__':
main()