-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock-and-key.py
executable file
·1670 lines (1444 loc) · 94 KB
/
lock-and-key.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
################################################################################
# Lock&Key Password Manager #
################################################################################
# Version: 1.0 #
# Open-source, self-managed, and self-hosted #
# #
# Created by - Ludvik Kristoffersen #
# #
# Copyright 2024 Ludvik Kristoffersen #
################################################################################
################################################################################
# #
# Importing modules #
# #
################################################################################
# 1. Fernet used for encrypting and decrypting passwords before storage.
# 2. Pillow used for importing and handling images in the application.
# 3. MySQL connector used for connecting and interacting with the MySQL database.
# 4. Customtkinter used for creating the application interface.
# 5. Platform used for OS type detection.
# 6. Argon2 used for hashing master passwords and creating a secure key for encryption and decryption.
# 7. Random used for randomly selecting characters for password generating.
# 8. String used for easily getting lowercase, uppercase, and digit characters.
# 9. Socket used for testing the connection of the user supplied IP address.
# 10. Base64 used for encoding the Argon2 generated key into base64.
# 11. Time used for creating small time delays between some actions.
# 12. OS used for mainly checking for if files exist or not.
# 13. RE used for creating regex to be used to check user input.
from cryptography.fernet import Fernet
from PIL import Image
import mysql.connector
import customtkinter
import platform
import argon2
import random
import string
import socket
import base64
import time
import sys
import os
import re
################################################################################
# #
# OS detection #
# #
################################################################################
# Getting the OS type the user is currently on, used to determine various decisions.
os_name = platform.system()
# Based on the OS used, Python determines the absolute paths for the installation
# folder, this is how we are able to locate not only the executable but also the
# images, and the other files used in the script.
if os_name == "Windows":
def resource_path(relative_path):
try:
base_path = sys._MEIPASS2
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
elif os_name == "Linux":
def resource_path(relative_path):
try:
base_path = sys._MEIPASS2
except Exception:
base_path = os.path.abspath("/opt")
if relative_path.endswith((".json", ".txt")):
return os.path.join(base_path, "lock-and-key", relative_path)
elif relative_path.endswith(".png"):
return os.path.join(base_path, "lock-and-key", ".images", relative_path)
# Based on the OS type we are importing images from the ".images" folder,
# and saving these images as variables to be used later in the script.
if os_name == "Windows":
logo_image_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path(".images\\lock-and-key-darkmode.png")), size=(165,39))
logo_image_light = customtkinter.CTkImage(light_image=Image.open(resource_path(".images\\lock-and-key-lightmode.png")), size=(165,39))
information_image_light = customtkinter.CTkImage(light_image=Image.open(resource_path(".images\\info-button-lightmode.png")), size=(24,24))
information_image_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path(".images\\info-button-darkmode.png")), size=(24,24))
dark_mode_image = customtkinter.CTkImage(light_image=Image.open(resource_path(".images\\light-mode.png")), size=(24,24))
light_mode_image = customtkinter.CTkImage(dark_image=Image.open(resource_path(".images\\dark-mode.png")), size=(24,24))
exit_image_light = customtkinter.CTkImage(light_image=Image.open(resource_path(".images\\exit-button-lightmode.png")), size=(24,24))
exit_image_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path(".images\\exit-button-darkmode.png")), size=(24,24))
user_image_light = customtkinter.CTkImage(light_image=Image.open(resource_path(".images\\user-button-lightmode.png")), size=(24,24))
user_image_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path(".images\\user-button-darkmode.png")), size=(24,24))
title_bar_logo_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path(".images\\lock-and-key-titlebar-white.png")), size=(20,20))
title_bar_logo_light = customtkinter.CTkImage(dark_image=Image.open(resource_path(".images\\lock-and-key-titlebar-dark.png")), size=(20,20))
elif os_name == "Linux":
logo_image_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path("lock-and-key-darkmode.png")), size=(165,39))
logo_image_light = customtkinter.CTkImage(light_image=Image.open(resource_path("lock-and-key-lightmode.png")), size=(165,39))
information_image_light = customtkinter.CTkImage(light_image=Image.open(resource_path("info-button-lightmode.png")), size=(24,24))
information_image_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path("info-button-darkmode.png")), size=(24,24))
dark_mode_image = customtkinter.CTkImage(light_image=Image.open(resource_path("light-mode.png")), size=(24,24))
light_mode_image = customtkinter.CTkImage(dark_image=Image.open(resource_path("dark-mode.png")), size=(24,24))
exit_image_light = customtkinter.CTkImage(light_image=Image.open(resource_path("exit-button-lightmode.png")), size=(24,24))
exit_image_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path("exit-button-darkmode.png")), size=(24,24))
user_image_light = customtkinter.CTkImage(light_image=Image.open(resource_path("user-button-lightmode.png")), size=(24,24))
user_image_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path("user-button-darkmode.png")), size=(24,24))
title_bar_logo_dark = customtkinter.CTkImage(dark_image=Image.open(resource_path("lock-and-key-titlebar-white.png")), size=(20,20))
title_bar_logo_light = customtkinter.CTkImage(dark_image=Image.open(resource_path("lock-and-key-titlebar-dark.png")), size=(20,20))
################################################################################
# #
# Application color control #
# #
################################################################################
# Setting the default appearance mode of the application to the custom JSON
# theme, and setting the default appearance mode to being dark. Also setting
# the error message color, and succeed message color.
customtkinter.set_default_color_theme(resource_path(".app-theme.json"))
error_color = "#E63946"
succeed_color = "#3A3DFD"
appearance_mode = "dark"
# A function for getting the saved appearance mode on application startup,
# and then configuring the color for the title bar based on the appearance mode.
def get_color():
global appearance_mode
if os.path.isfile(resource_path(".appearance-mode.txt")):
with open(resource_path(".appearance-mode.txt"), "r") as file:
appearance_mode = file.readline().strip()
file.close()
if appearance_mode == "dark":
customtkinter.set_appearance_mode("light")
title_bar.configure(fg_color="#eaeaff", bg_color="#eaeaff")
title_bar_close_button.configure(fg_color="#b5b3de", bg_color="#b5b3de", text_color="#0B0B12", hover_color="#a3a0e2")
title_bar_logo_label.configure(image=title_bar_logo_light)
appearance_mode = "dark"
elif appearance_mode == "light":
customtkinter.set_appearance_mode("dark")
title_bar.configure(fg_color="#2c2c46", bg_color="#2c2c46")
title_bar_close_button.configure(fg_color="#0B0B12", bg_color="#0B0B12", text_color="#DAD9FC", hover_color="#19192d")
title_bar_logo_label.configure(image=title_bar_logo_dark)
appearance_mode = "light"
else:
customtkinter.set_appearance_mode("dark")
title_bar.configure(fg_color="#2c2c46", bg_color="#2c2c46")
title_bar_close_button.configure(fg_color="#0B0B12", bg_color="#0B0B12", text_color="#DAD9FC", hover_color="#19192d")
title_bar_logo_label.configure(image=title_bar_logo_dark)
appearance_mode = "light"
with open(resource_path(".appearance-mode.txt"), "w") as file:
file.write("light")
file.close()
else:
with open(resource_path(".appearance-mode.txt"), "x") as file:
file.close()
with open(resource_path(".appearance-mode.txt"), "w") as file:
file.write("light")
file.close()
with open(resource_path(".appearance-mode.txt"), "r") as file:
appearance_mode = file.readline()
file.close()
if appearance_mode == "light":
customtkinter.set_appearance_mode("dark")
title_bar.configure(fg_color="#2c2c46", bg_color="#2c2c46")
title_bar_close_button.configure(fg_color="#0B0B12", bg_color="#0B0B12", text_color="#DAD9FC", hover_color="#19192d")
appearance_mode = "light"
else:
customtkinter.set_appearance_mode("dark")
title_bar.configure(fg_color="#2c2c46", bg_color="#2c2c46")
title_bar_close_button.configure(fg_color="#0B0B12", bg_color="#0B0B12", text_color="#DAD9FC", hover_color="#19192d")
appearance_mode = "light"
# This function is for triggering the a UI change once the user changes
# the appearance mode within the application, it is also changing the
# colors of objects that might have changed during application usage.
def ui_change():
if appearance_mode == "dark":
button_change_appearance.configure(image=light_mode_image)
button_home.configure(image=information_image_light)
button_exit_application.configure(image=exit_image_light)
user_management_button.configure(image=user_image_light)
logo_label.configure(image=logo_image_light)
right_frame.configure(fg_color="#DAD9FC", bg_color="#DAD9FC")
title_bar.configure(fg_color="#eaeaff", bg_color="#eaeaff")
title_bar_close_button.configure(fg_color="#b5b3de", bg_color="#b5b3de", text_color="#0B0B12", hover_color="#a3a0e2")
title_bar_logo_label.configure(image=title_bar_logo_light)
try:
password_strength_slider.configure(progress_color="#DAD9FC", button_color="#DAD9FC", button_hover_color="#DAD9FC")
password_strength_updater(None)
except:
pass
customtkinter.set_appearance_mode("light")
elif appearance_mode == "light":
button_change_appearance.configure(image=dark_mode_image)
button_home.configure(image=information_image_dark)
button_exit_application.configure(image=exit_image_dark)
user_management_button.configure(image=user_image_dark)
logo_label.configure(image=logo_image_dark)
right_frame.configure(fg_color="#11111C", bg_color="#11111C")
title_bar.configure(fg_color="#2c2c46", bg_color="#2c2c46")
title_bar_close_button.configure(fg_color="#0B0B12", bg_color="#0B0B12", text_color="#DAD9FC", hover_color="#19192d")
title_bar_logo_label.configure(image=title_bar_logo_dark)
try:
password_strength_slider.configure(progress_color="#11111C", button_color="#11111C", button_hover_color="#11111C")
password_strength_updater(None)
except:
pass
customtkinter.set_appearance_mode("dark")
else:
button_change_appearance.configure(image=dark_mode_image)
button_home.configure(image=information_image_dark)
button_exit_application.configure(image=exit_image_dark)
user_management_button.configure(image=user_image_dark)
logo_label.configure(image=logo_image_dark)
right_frame.configure(fg_color="#11111C", bg_color="#11111C")
title_bar.configure(fg_color="#2c2c46", bg_color="#2c2c46")
title_bar_close_button.configure(fg_color="#0B0B12", bg_color="#0B0B12", text_color="#DAD9FC", hover_color="#19192d")
title_bar_logo_label.configure(image=title_bar_logo_dark)
try:
password_strength_slider.configure(progress_color="#11111C", button_color="#11111C", button_hover_color="#11111C")
password_strength_updater(None)
except:
pass
customtkinter.set_appearance_mode("dark")
# This is the function that changes the appearance mode based on user interaction
# during runtime.
def change_appearance_mode():
global appearance_mode
if appearance_mode == "dark":
appearance_mode = "light"
with open(resource_path(".appearance-mode.txt"), "w") as file:
file.write("light")
file.close()
ui_change()
else:
appearance_mode = "dark"
with open(resource_path(".appearance-mode.txt"), "w") as file:
file.write("dark")
file.close()
ui_change()
################################################################################
# #
# Minor functions #
# #
################################################################################
# Creating some regex's that determine what the user is allowed to type
# in the various input fields, checks if the user has used characters that
# is not allowed.
username_regex = r"^[A-Za-z0-9_.@\-]+$"
password_regex = r"^[A-Za-z0-9!@#$%^&*]+$"
folder_regex = r"^[A-Za-z0-9]+$"
# Function for removing the objects in the right frame, used to reset the
# right frame by removing the contents that is in place, used at the start in
# every main function.
def remove_right_objects():
for widget in right_frame.winfo_children():
widget.destroy()
# Function for removing the objects in the sidebar frame, used to reset the
# sidebar frame by removing the contents that is in place.
def remove_sidebar_objects():
for widget in login_frame.winfo_children():
widget.destroy()
# Fucntion for deleting the objects that are currently present in the title bar.
def remove_titlebar_objects():
for widget in title_bar.winfo_children():
widget.destroy()
# Functions for letting the user click and drag on the custom title bar to
# move it around on the screen.
def get_position(event):
global x_pos, y_pos
x_pos = event.x
y_pos = event.y
def move_application(event):
x = event.x_root - x_pos
y = event.y_root - y_pos
root.geometry(f"+{x}+{y}")
# This function is used to exit the application, it closes the current
# database connection and the database cursor and then safely exists
# the application.
def exit_application():
try:
if cursor and connection:
cursor.close()
connection.close()
time.sleep(1)
root.quit()
sys.exit()
else:
root.quit()
sys.exit()
except:
time.sleep(1)
root.quit()
sys.exit()
################################################################################
# #
# Password strength check #
# #
################################################################################
# This function calculates the score based on if the user meets the requirements
# listed below, these requirements follow the guidelines found on this website
# https://www.cmu.edu/iso/governance/guidelines/password-management.html
# but has been modified to make the requirements stronger.
def password_score_calculation(password):
password_score = 0
if len(password) >= 20:
password_score += 1
if re.search(r"[a-z]", password):
password_score += 1
if re.search(r"(?:[A-Z].*?){1,}", password):
password_score += 1
if re.search(r"(?:[0-9].*?){3,}", password):
password_score += 1
if re.search(r"(?:[!@#$%^&*].*?){3,}", password):
password_score += 1
return password_score
# This function updates the password strength checker based on teh score
# calculated in the previous function. This let's the user see exactly
# how weak or strong the password entered is.
def password_strength_updater(event):
entered_password = password_entry.get()
overall_score = password_score_calculation(entered_password)
if overall_score == 0:
password_strength_label.configure(text="")
if appearance_mode == "light":
password_strength_slider.configure(progress_color="#11111C", button_color="#11111C", button_hover_color="#11111C")
elif appearance_mode == "dark":
password_strength_slider.configure(progress_color="#DAD9FC", button_color="#DAD9FC", button_hover_color="#DAD9FC")
else:
password_strength_slider.configure(progress_color="#11111C", button_color="#11111C", button_hover_color="#11111C")
elif overall_score <= 2:
password_strength_label.configure(text="Weak")
password_strength_slider.configure(progress_color="#E63946", button_color="#E63946", button_hover_color="#E63946")
elif overall_score <= 4:
password_strength_label.configure(text="Good")
password_strength_slider.configure(progress_color="#ffa100", button_color="#ffa100", button_hover_color="#ffa100")
elif overall_score <= 6:
password_strength_label.configure(text="Strong")
password_strength_slider.configure(progress_color="#3A3DFD", button_color="#3A3DFD", button_hover_color="#3A3DFD")
else:
password_strength_label.configure(text="")
password_strength_slider.set(overall_score)
################################################################################
# #
# Home screen #
# #
################################################################################
# This function creates the home screen, this is the first screen the user sees
# once they have authenticated themselves. The home screen provides a short
# description of the password manager, and also lists the functionality provided
# by this password manager.
def home_screen():
remove_right_objects()
description_title_label = customtkinter.CTkLabel(right_frame, text="Description", font=customtkinter.CTkFont(size=20, weight="bold"))
description_title_label.grid(row=0, column=0, padx=20, pady=(20,5), sticky="w")
description_text = customtkinter.CTkTextbox(right_frame, width=550, height=50, font=customtkinter.CTkFont(size=13), wrap="word")
description_text.grid(row=1, column=0, padx=20, sticky="w")
description_text.insert("end", "Lock&Key is a self-hosted, self-managed, open-source password manager. It provides everything you need to store and manage your accounts securely!")
description_text.configure(state="disabled")
functionalities_label = customtkinter.CTkLabel(right_frame, text="Provided Functionalities", font=customtkinter.CTkFont(size=20, weight="bold"))
functionalities_label.grid(row=2, column=0, padx=20, pady=(5,5), sticky="w")
functionalities_text = customtkinter.CTkTextbox(right_frame, width=550, height=200, font=customtkinter.CTkFont(size=13), wrap="word")
functionalities_text.grid(row=3, column=0, padx=20, sticky="w")
functionalities_text.insert("end", """• Adding account entries: Create new or add existing account entries.\n
• Updating account entries: Modify account entries with new information.\n
• Deleting account entries: Remove unwanted account entries.\n
• Listing account entries: Display a list of all or specified account entries.\n
• Password generation: Generate random, complex passwords.\n
• Encryption: All data is securely stored with encryption.""")
functionalities_text.configure(state="disabled")
################################################################################
# #
# Adding entry #
# #
################################################################################
# This is one of the main functionalities provided by the password manager, this
# function lets the user add a new password entry into the database.
def adding_entry():
# Calls the function to remove what is currently in the right frame.
remove_right_objects()
global username_entry, folder_entry, folder_menu, password_entry, password_strength_label, password_strength_slider
add_entry_label = customtkinter.CTkLabel(right_frame, text="Add Entry", font=customtkinter.CTkFont(size=15, weight="bold"))
add_entry_label.grid(row=0, column=0, padx=20, pady=20, sticky="w")
username_label = customtkinter.CTkLabel(right_frame, text="Username:")
username_label.grid(row=1, column=0, padx=20, pady=10, sticky="w")
username_entry = customtkinter.CTkEntry(right_frame)
username_entry.grid(row=1, column=0, padx=100, pady=10, sticky="w")
# Function for showing the entered password.
def toggle_password_show():
if password_show.get():
password_entry.configure(show="")
else:
password_entry.configure(show="*")
password_label = customtkinter.CTkLabel(right_frame, text="Password:")
password_label.grid(row=2, column=0, padx=20, pady=(10,0), sticky="w")
password_entry = customtkinter.CTkEntry(right_frame, show="*")
password_entry.grid(row=2, column=0, padx=100, pady=(10,0), sticky="w")
# Bind that triggers the password strength checker on key release.
password_entry.bind("<KeyRelease>", password_strength_updater)
password_strength_slider = customtkinter.CTkSlider(right_frame, from_=0, to=6, number_of_steps=6, width=115, height=5)
password_strength_slider.grid(row=3, column=0, padx=(100,0), pady=0, sticky="w")
if appearance_mode == "light":
password_strength_slider.configure(progress_color="#11111C")
elif appearance_mode == "dark":
password_strength_slider.configure(progress_color="#DAD9FC")
else:
password_strength_slider.configure(progress_color="#11111C")
password_strength_slider.configure(state="disabled")
password_strength_slider.set(0)
password_strength_label = customtkinter.CTkLabel(right_frame, text="", font=customtkinter.CTkFont(size=11))
password_strength_label.grid(row=3, column=0, padx=(205,0), pady=0, sticky="w")
password_show = customtkinter.CTkCheckBox(right_frame, text="Show password", command=toggle_password_show)
password_show.grid(row=2, column=1, pady=(10,0), sticky="w")
# MySQL query that retrieves all the folders from the vault.
cursor.execute("SELECT entry_folder FROM vault WHERE user_id = %s", (user_id,))
rows = cursor.fetchall()
folder_list = []
for row in rows:
folder = row[0].encode()
decrypted_folder = cipher_instance.decrypt(folder)
decode_folder = decrypted_folder.decode()
folder_list.append(decode_folder)
if "None" in folder_list:
folder_list.remove("None")
else:
pass
folder_list = list(set(folder_list))
folder_list.sort(key=str.lower)
folder_label = customtkinter.CTkLabel(right_frame, text="Folder:")
folder_label.grid(row=4, column=0, padx=20, pady=(0,10), sticky="w")
folder_entry = customtkinter.CTkEntry(right_frame)
folder_entry.grid(row=4, column=0, padx=100, pady=(0,10), sticky="w")
folder_menu = customtkinter.CTkOptionMenu(right_frame, values=["None"]+folder_list)
folder_menu.grid(row=4, column=1, pady=(0,10), sticky="w")
# Function for adding the new entry into the database, it checks if the username,
# password, and folder matches their respective regex patterns. Passwords are
# encrypted, and that encrypted value is stored in the database.
def add_database_entry():
try:
username = username_entry.get().strip()
password = password_entry.get().strip()
folder = folder_entry.get().strip()
folder_select = folder_menu.get()
if len(username) > 0 and len(password) > 0:
message_label.configure(text="")
if re.match(username_regex,username):
message_label.configure(text="")
if re.match(password_regex,password):
message_label.configure(text="")
encrypt_username = cipher_instance.encrypt(username.encode())
decoded_encrypted_username = encrypt_username.decode()
encrypt_password = cipher_instance.encrypt(password.encode())
decoded_encrypted_password = encrypt_password.decode()
encrypt_folder = cipher_instance.encrypt(folder.encode())
decoded_encrypted_folder = encrypt_folder.decode()
if len(folder) > 0:
message_label.configure(text="")
if re.match(folder_regex,folder):
cursor.execute("INSERT INTO vault (user_id, entry_username, entry_password, entry_folder) VALUES (%s, %s, %s, %s)", (user_id, decoded_encrypted_username, decoded_encrypted_password, decoded_encrypted_folder))
connection.commit()
message_label.configure(text="New entry added.", text_color=succeed_color)
username_entry.delete(0, 'end')
password_entry.delete(0, 'end')
folder_entry.delete(0, 'end')
password_strength_updater(None)
else:
message_label.configure(text="Folder invalid.", text_color=error_color)
else:
encrypt_folder = cipher_instance.encrypt(folder_select.encode())
decoded_encrypted_folder = encrypt_folder.decode()
message_label.configure(text="")
cursor.execute("INSERT INTO vault (user_id, entry_username, entry_password, entry_folder) VALUES (%s, %s, %s, %s)", (user_id, decoded_encrypted_username, decoded_encrypted_password, decoded_encrypted_folder))
connection.commit()
message_label.configure(text="New entry added.", text_color=succeed_color)
username_entry.delete(0, 'end')
password_entry.delete(0, 'end')
folder_entry.delete(0, 'end')
password_strength_updater(None)
else:
message_label.configure(text="Password invalid.", text_color=error_color)
else:
message_label.configure(text="Username invalid.", text_color=error_color)
else:
message_label.configure(text="Username or password cannot be empty.", text_color=error_color)
except mysql.connector.Error:
message_label.configure(text="Failed to add new entry.", text_color=error_color)
add_button = customtkinter.CTkButton(right_frame, text="Add Entry", command=add_database_entry)
add_button.grid(row=5, column=0, padx=20, pady=10, sticky="w")
# This function provides the user with the possibility to generate a random strong password
# that must be in the 25-255 character limit. The randomly generated password must create
# a strong password each time, meaning if must match the requirements for the password
# strength checker.
def generate_random_password():
try:
password_length = int(password_char_length.get())
if password_length < 25:
message_label.configure(text="Password character length to short.", text_color=error_color)
elif password_length > 255:
message_label.configure(text="Password character length to long.", text_color=error_color)
else:
message_label.configure(text="")
password_entry.delete(0, "end")
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase
numbers = string.digits
special_char = "!@#$%^&*"
combined_char_list = lowercase+uppercase+numbers+special_char
random_password = "".join(random.choices(combined_char_list, k=password_length))
if any(char in lowercase for char in random_password) and any(char in uppercase for char in random_password) \
and sum(char in numbers for char in random_password) >= 3 and sum(char in special_char for char in random_password) >= 3:
password_entry.insert(0, random_password)
else:
generate_random_password()
password_strength_updater(None)
except:
message_label.configure(text="Password character length not valid.", text_color=error_color)
password_char_length = customtkinter.CTkEntry(right_frame, placeholder_text="25-255", width=60, justify="center")
password_char_length.grid(row=5, column=2, padx=10, pady=10, sticky="w")
password_char_length.insert(0, "50")
generate_password_button = customtkinter.CTkButton(right_frame, text="Generate Password", command=generate_random_password)
generate_password_button.grid(row=5, column=1, pady=10, sticky="w")
message_label = customtkinter.CTkLabel(right_frame, text="")
message_label.grid(row=6, column=0, padx=20, pady=10, sticky="w")
################################################################################
# #
# Updating entry #
# #
################################################################################
# This is one of the main functionalities provided by the password manager, this
# function lets the user update any entries that are already stored in the
# password manager.
def updating_entry():
# Calls the function to remove what is currently in the right frame.
remove_right_objects()
global updating_list
update_entry_label = customtkinter.CTkLabel(right_frame, text="Update Entry", font=customtkinter.CTkFont(size=15, weight="bold"))
update_entry_label.grid(row=0, column=0, padx=20, pady=20, sticky="w")
# MySQL query that retrieves all the folders from the vault which is used
# for displaying all entries or only specified entries.
cursor.execute("SELECT entry_folder FROM vault WHERE user_id = %s", (user_id,))
rows = cursor.fetchall()
folder_list = []
for row in rows:
folder = row[0].encode()
decrypted_folder = cipher_instance.decrypt(folder)
decode_folder = decrypted_folder.decode()
folder_list.append(decode_folder)
folder_list = list(set(folder_list))
folder_list.sort(key=str.lower)
scrollable_frame = customtkinter.CTkScrollableFrame(right_frame, width=550, height=235, corner_radius=6)
scrollable_frame.grid(row=3, column=0, padx=(20, 0), pady=(10, 0), sticky="nsew")
scrollable_frame.grid_columnconfigure(0, weight=1)
# Function that retrieves all entries currently stored in the password manager,
# but it does not retrieve the password.
def updating_list(*args):
for widget in scrollable_frame.winfo_children():
widget.destroy()
selected_folder = folder_menu.get()
cursor.execute("SELECT id, entry_username, entry_folder FROM vault WHERE user_id = %s", (user_id,))
entries = cursor.fetchall()
entries.sort(key=lambda entry: cipher_instance.decrypt(entry[2].encode()).decode().lower())
if len(folder_list) != 0:
title_username_label = customtkinter.CTkLabel(scrollable_frame, text="Username", font=customtkinter.CTkFont(size=13, weight="bold"))
title_username_label.grid(row=2, column=0, padx=0, pady=5, sticky="w")
title_folder_label = customtkinter.CTkLabel(scrollable_frame, text="Folder", font=customtkinter.CTkFont(size=13, weight="bold"))
title_folder_label.grid(row=2, column=1, padx=(5,40), pady=5, sticky="w")
else:
pass
entry_id = 3
for entry in entries:
decrypted_folder = cipher_instance.decrypt(entry[2].encode()).decode()
if selected_folder == "All" or decrypted_folder == selected_folder:
decrypted_username = cipher_instance.decrypt(entry[1].encode()).decode()
username_label = customtkinter.CTkLabel(scrollable_frame, text=f"{decrypted_username}")
username_label.grid(row=entry_id, column=0, padx=0, pady=5, sticky="w")
folder_label = customtkinter.CTkLabel(scrollable_frame, text=f"{decrypted_folder}")
folder_label.grid(row=entry_id, column=1, padx=(5,40), pady=5, sticky="w")
row_id = entry[0]
select_entry_button = customtkinter.CTkButton(scrollable_frame, text="Select")
select_entry_button.grid(row=entry_id, column=2, padx=(5,0), pady=5, sticky="w")
select_entry_button.configure(command=lambda r=row_id, u=decrypted_username, f=decrypted_folder: updating_entry_button(r,u,f))
entry_id += 1
folder_menu_label = customtkinter.CTkLabel(right_frame, text="Select folder:", font=customtkinter.CTkFont(size=13, weight="bold"))
folder_menu_label.grid(row=1, column=0, padx=20, pady=0, sticky="w")
folder_menu = customtkinter.CTkOptionMenu(right_frame, values=["All"]+folder_list, command=updating_list)
folder_menu.grid(row=1, column=0, padx=120, pady=0, sticky="w")
# When a user selects a entry they want to edit or update in any way, they get
# brought to the same screen used to create a new entry, but the username
# and folder is already filled in, indicating that the user is updating a
# entry. The password does not show, if the password is not changed then the
# currently stored password will not change, but if it is changed then the
# password will update.
def updating_entry_button(row_id, username, get_folder):
global password_entry, password_strength_label, password_strength_slider
remove_right_objects()
update_entry_label = customtkinter.CTkLabel(right_frame, text="Update Entry", font=customtkinter.CTkFont(size=15, weight="bold"))
update_entry_label.grid(row=0, column=0, padx=20, pady=20, sticky="w")
username_label = customtkinter.CTkLabel(right_frame, text="Username:")
username_label.grid(row=1, column=0, padx=20, pady=10, sticky="w")
username_entry = customtkinter.CTkEntry(right_frame)
username_entry.grid(row=1, column=0, padx=100, pady=10, sticky="w")
username_entry.insert(0, username)
# Function for showing the entered password.
def toggle_password_show():
if password_show.get():
password_entry.configure(show="")
else:
password_entry.configure(show="*")
password_label = customtkinter.CTkLabel(right_frame, text="Password:")
password_label.grid(row=2, column=0, padx=20, pady=(10,0), sticky="w")
password_entry = customtkinter.CTkEntry(right_frame, show="*")
password_entry.grid(row=2, column=0, padx=100, pady=(10,0), sticky="w")
# Bind that triggers the password strength checker on key release.
password_entry.bind("<KeyRelease>", password_strength_updater)
password_strength_slider = customtkinter.CTkSlider(right_frame, from_=0, to=6, number_of_steps=6, width=115, height=5)
password_strength_slider.grid(row=3, column=0, padx=(100,0), pady=0, sticky="w")
if appearance_mode == "light":
password_strength_slider.configure(progress_color="#11111C")
elif appearance_mode == "dark":
password_strength_slider.configure(progress_color="#DAD9FC")
else:
password_strength_slider.configure(progress_color="#11111C")
password_strength_slider.configure(state="disabled")
password_strength_slider.set(0)
password_strength_label = customtkinter.CTkLabel(right_frame, text="", font=customtkinter.CTkFont(size=11))
password_strength_label.grid(row=3, column=0, padx=(205,0), pady=0, sticky="w")
password_show = customtkinter.CTkCheckBox(right_frame, text="Show password", command=toggle_password_show)
password_show.grid(row=2, column=1, pady=(10,0), sticky="w")
folder_label = customtkinter.CTkLabel(right_frame, text="Folder:")
folder_label.grid(row=4, column=0, padx=20, pady=(0,10), sticky="w")
folder_entry = customtkinter.CTkEntry(right_frame)
folder_entry.grid(row=4, column=0, padx=100, pady=(0,10), sticky="w")
folder_entry.insert(0, get_folder)
# MySQL query that retrieves all the folders from the vault.
cursor.execute("SELECT entry_folder FROM vault WHERE user_id = %s", (user_id,))
rows = cursor.fetchall()
folder_list = []
for row in rows:
folder = row[0].encode()
decrypted_folder = cipher_instance.decrypt(folder)
decode_folder = decrypted_folder.decode()
folder_list.append(decode_folder)
if "None" in folder_list:
folder_list.remove("None")
else:
pass
folder_list = list(set(folder_list))
folder_list.sort(key=str.lower)
folder_menu = customtkinter.CTkOptionMenu(right_frame, values=["None"]+folder_list)
folder_menu.grid(row=4, column=1, padx=0, pady=(0,10))
# Function that updates the selected entry. It checks if anything has been changed
# if not then nothing will be updated, all inputs are also being run through the
# regex specified earlier to hinder the user from entering characters that might
# break the back end query made. The user can choose to update one or multiple
# things, new passwords are also encrypted before being stored in the database.
def confirm_update():
new_username = username_entry.get().strip()
password = password_entry.get().strip()
new_folder = folder_entry.get().strip()
row_id_int = int(row_id)
if len(new_username) > 0:
message_label.configure(text="")
if re.match(username_regex,new_username):
message_label.configure(text="")
if len(password) == 0 or re.match(password_regex,password):
message_label.configure(text="")
if len(new_folder) == 0 or re.match(folder_regex,new_folder):
message_label.configure(text="")
if len(password) != 0:
encode_password = password.encode()
encrypt_password = cipher_instance.encrypt(encode_password)
decoded_encrypted_password = encrypt_password.decode()
if len(new_folder) == 0:
folder_encode = folder_menu.get().encode()
encrypt_folder = cipher_instance.encrypt(folder_encode)
decoded_encrypted_folder = encrypt_folder.decode()
else:
folder_encode = new_folder.encode()
encrypt_folder = cipher_instance.encrypt(folder_encode)
decoded_encrypted_folder = encrypt_folder.decode()
username_encode = new_username.encode()
encrypt_username = cipher_instance.encrypt(username_encode)
decoded_encrypted_username = encrypt_username.decode()
if new_username != username and len(password) != 0 and new_folder != get_folder:
if len(new_folder) == 0:
cursor.execute("UPDATE vault SET entry_username = %s, entry_password = %s, entry_folder = %s WHERE id = %s", (decoded_encrypted_username, decoded_encrypted_password, decoded_encrypted_folder, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
else:
cursor.execute("UPDATE vault SET entry_username = %s, entry_password = %s, entry_folder = %s WHERE id = %s", (decoded_encrypted_username, decoded_encrypted_password, decoded_encrypted_folder, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
elif new_username != username and len(password) != 0:
cursor.execute("UPDATE vault SET entry_username = %s, entry_password = %s WHERE id = %s", (decoded_encrypted_username, decoded_encrypted_password, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
elif new_username != username and new_folder != get_folder:
if len(new_folder) == 0:
cursor.execute("UPDATE vault SET entry_username = %s, entry_folder = %s WHERE id = %s", (decoded_encrypted_username, decoded_encrypted_folder, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
else:
cursor.execute("UPDATE vault SET entry_username = %s, entry_folder = %s WHERE id = %s", (decoded_encrypted_username, decoded_encrypted_folder, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
elif len(password) != 0 and new_folder != get_folder:
if len(new_folder) == 0:
cursor.execute("UPDATE vault SET entry_password = %s, entry_folder = %s WHERE id = %s", (decoded_encrypted_password, decoded_encrypted_folder, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
else:
cursor.execute("UPDATE vault SET entry_password = %s, entry_folder = %s WHERE id = %s", (decoded_encrypted_password, decoded_encrypted_folder, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
elif new_username != username:
cursor.execute("UPDATE vault SET entry_username = %s WHERE id = %s", (decoded_encrypted_username, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
elif len(password) != 0:
cursor.execute("UPDATE vault SET entry_password = %s WHERE id = %s", (decoded_encrypted_password, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
elif new_folder != get_folder:
if len(new_folder) == 0:
cursor.execute("UPDATE vault SET entry_folder = %s WHERE id = %s", (decoded_encrypted_folder, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
else:
cursor.execute("UPDATE vault SET entry_folder = %s WHERE id = %s", (decoded_encrypted_folder, row_id_int))
connection.commit()
password_strength_updater(None)
updating_entry()
else:
message_label.configure(text="Nothing has been changed!", text_color=error_color)
else:
message_label.configure(text="Folder invalid.", text_color=error_color)
else:
message_label.configure(text="Password invalid.", text_color=error_color)
else:
message_label.configure(text="Username invalid.", text_color=error_color)
else:
message_label.configure(text="Username cannot be empty.", text_color=error_color)
# Function for letting the user cancel the current update, brings the user
# back to the selection screen.
def cancel_update():
password_strength_updater(None)
remove_right_objects()
updating_entry()
confirm_update_button = customtkinter.CTkButton(right_frame, text="Update", command=confirm_update, width=60)
confirm_update_button.grid(row=5, column=0, padx=20, pady=10, sticky="w")
cancel_update_button = customtkinter.CTkButton(right_frame, text="Cancel", command=cancel_update, width=60)
cancel_update_button.grid(row=5, column=0, padx=90, pady=10, sticky="w")
# This function provides the user with the possibility to generate a random strong password
# that must be in the 25-255 character limit. The randomly generated password must create
# a strong password each time, meaning if must match the requirements for the password
# strength checker.
def generate_random_password():
try:
password_length = int(password_char_length.get())
if password_length < 25:
message_label.configure(text="Password character length to short.", text_color=error_color)
elif password_length > 255:
message_label.configure(text="Password character length to long.", text_color=error_color)
else:
message_label.configure(text="")
password_entry.delete(0, "end")
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase
numbers = string.digits
special_char = "!@#$%^&*"
combined_char_list = lowercase+uppercase+numbers+special_char
random_password = "".join(random.choices(combined_char_list, k=password_length))
if any(char in lowercase for char in random_password) and any(char in uppercase for char in random_password) \
and sum(char in numbers for char in random_password) >= 3 and sum(char in special_char for char in random_password) >= 3:
password_entry.insert(0, random_password)
else:
generate_random_password()
password_strength_updater(None)
except:
message_label.configure(text="Password character length not valid.", text_color=error_color)
password_char_length = customtkinter.CTkEntry(right_frame, placeholder_text="25-255", width=60, justify="center")
password_char_length.grid(row=5, column=2, padx=10, pady=10, sticky="w")
password_char_length.insert(0, "50")
generate_password_button = customtkinter.CTkButton(right_frame, text="Generate Password", command=generate_random_password)
generate_password_button.grid(row=5, column=1, pady=10, sticky="w")
message_label = customtkinter.CTkLabel(right_frame, text="")
message_label.grid(row=6, column=0, padx=20, pady=10, sticky="w")
# Calls the updating list function immediately to update the current list of entries.
updating_list()
################################################################################
# #
# Deleting entry #
# #
################################################################################
# This is one of the main functionalities provided by the password manager, this
# function lets the user delete entries in the password manager.
def deleting_entry():
# Calls the function to remove what is currently in the right frame.
remove_right_objects()
delete_entry_label = customtkinter.CTkLabel(right_frame, text="Delete Entry", font=customtkinter.CTkFont(size=15, weight="bold"))
delete_entry_label.grid(row=0, column=0, padx=20, pady=20, sticky="w")
# MySQL query that retrieves all the folders from the vault which is used
# for displaying all entries or only specified entries.
cursor.execute("SELECT entry_folder FROM vault WHERE user_id = %s", (user_id,))
rows = cursor.fetchall()
folder_list = []
for row in rows:
folder = row[0].encode()
decrypted_folder = cipher_instance.decrypt(folder)
decode_folder = decrypted_folder.decode()
folder_list.append(decode_folder)
folder_list = list(set(folder_list))
folder_list.sort(key=str.lower)
scrollable_frame = customtkinter.CTkScrollableFrame(right_frame, width=550, height=235, corner_radius=6)
scrollable_frame.grid(row=3, column=0, padx=(20, 0), pady=(10, 0), sticky="nsew")
scrollable_frame.grid_columnconfigure(0, weight=1)
# Clicking the delete button on one of the entries in the list
# will bring up a confirmation screen where the user can confirm
# the deletion or decline the deletion.
def delete_entry_button(row_id):
remove_right_objects()
delete_entry_label = customtkinter.CTkLabel(right_frame, text="Delete Entry", font=customtkinter.CTkFont(size=15, weight="bold"))
delete_entry_label.grid(row=0, column=0, padx=20, pady=20, sticky="w")
warning_label = customtkinter.CTkLabel(right_frame, text="Deletion of entries are final, are you sure?")
warning_label.grid(row=1, column=0, padx=20, pady=5, sticky="w")
# This function is run when the user confirms the deletion, this will
# delete the entry from the database.
def confirm_deletion():
cursor.execute(f"DELETE FROM vault WHERE id={int(row_id)}")
connection.commit()
deleting_entry()
confirm_deletion_button = customtkinter.CTkButton(right_frame, text="Yes", command=confirm_deletion, width=50, fg_color=error_color, hover_color="#CC323F", text_color="#0B0B12")
confirm_deletion_button.grid(row=2, column=0, padx=20, pady=5, sticky="w")
regret_deletion_button = customtkinter.CTkButton(right_frame, text="No", command=deleting_entry, width=50)
regret_deletion_button.grid(row=2, column=0, padx=90, pady=5, sticky="w")
# Function that retrieves all entries currently stored in the password manager,
# but it does not retrieve the password.
def updating_list(*args):
for widget in scrollable_frame.winfo_children():
widget.destroy()
selected_folder = folder_menu.get()
cursor.execute("SELECT id, entry_username, entry_folder FROM vault WHERE user_id = %s", (user_id,))
entries = cursor.fetchall()
entries.sort(key=lambda entry: cipher_instance.decrypt(entry[2].encode()).decode().lower())
if len(folder_list) != 0:
title_username_label = customtkinter.CTkLabel(scrollable_frame, text="Username", font=customtkinter.CTkFont(size=13, weight="bold"))
title_username_label.grid(row=2, column=0, padx=0, pady=5, sticky="w")
title_folder_label = customtkinter.CTkLabel(scrollable_frame, text="Folder", font=customtkinter.CTkFont(size=13, weight="bold"))
title_folder_label.grid(row=2, column=1, padx=(5,40), pady=5, sticky="w")
else:
pass
entry_id = 3
for entry in entries:
decrypted_folder = cipher_instance.decrypt(entry[2].encode()).decode()
if selected_folder == "All" or decrypted_folder == selected_folder:
decrypted_username = cipher_instance.decrypt(entry[1].encode()).decode()
username_label = customtkinter.CTkLabel(scrollable_frame, text=f"{decrypted_username}")
username_label.grid(row=entry_id, column=0, padx=0, pady=5, sticky="w")
folder_label = customtkinter.CTkLabel(scrollable_frame, text=f"{decrypted_folder}")
folder_label.grid(row=entry_id, column=1, padx=(5,40), pady=5, sticky="w")
row_id = entry[0]
remove_button = customtkinter.CTkButton(scrollable_frame, text="Delete", fg_color=error_color, hover_color="#CC323F", text_color="#0B0B12")
remove_button.grid(row=entry_id, column=2, padx=(5,0), pady=5, sticky="w")
remove_button.configure(command=lambda r=row_id: delete_entry_button(r))
entry_id += 1
folder_menu_label = customtkinter.CTkLabel(right_frame, text="Select folder:", font=customtkinter.CTkFont(size=13, weight="bold"))
folder_menu_label.grid(row=1, column=0, padx=20, pady=0, sticky="w")
folder_menu = customtkinter.CTkOptionMenu(right_frame, values=["All"]+folder_list, command=updating_list)
folder_menu.grid(row=1, column=0, padx=120, pady=0, sticky="w")
# Calls the updating list function immediately to update the current list of entries.
updating_list()
################################################################################
# #
# Listing entries #
# #
################################################################################
# This is one of the main functionalities provided by the password manager, this
# function lets the user list all or specified entries and let's the user copy
# password for any stored entry in the database.
def listing_entries():
# Calls the function to remove what is currently in the right frame.
remove_right_objects()
global scrollable_frame
list_entry_label = customtkinter.CTkLabel(right_frame, text="Listing Entries", font=customtkinter.CTkFont(size=15, weight="bold"))
list_entry_label.grid(row=0, column=0, padx=20, pady=20, sticky="w")
# MySQL query that retrieves all the folders from the vault which is used
# for displaying all entries or only specified entries.
cursor.execute("SELECT entry_folder FROM vault WHERE user_id = %s", (user_id,))
rows = cursor.fetchall()
folder_list = []
for row in rows: