-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·348 lines (299 loc) · 14.3 KB
/
run.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
#!/usr/bin/env python3.6
import sys
import os
import pyperclip
import string
import random
from credential import Credential
from user_data import User
from pyfiglet import figlet_format
from termcolor import colored, cprint
from colorama import init
init(strip=not sys.stdout.isatty())
terminal_width = os.get_terminal_size().columns
def create_new_account(username, password):
new_account = User(username, password)
return new_account
def check_user_exists(userName):
return User.user_exists(userName)
def save_account(account):
account.save_user()
def delete_account(account):
account.user_delete_account()
def login_user(userName, passwrd):
return User.confirm_user(userName, passwrd)
def user_change_password(userName, new_pass):
return User.change_userpass(userName, new_pass)
def create_new_profile(profile_name, profile_username = None, profile_email = None, profile_password = None):
new_profile = Credential(profile_name, profile_username = None, profile_email = None, profile_password = None)
return new_profile
def save_profile(new_profile):
new_profile.save_profile()
def delete_profile(profile):
profile.delete_profile()
def generate_random_password(length):
generate_random_password = Credential.generate_random_password(length)
return generate_random_password
# chars = string.ascii_lowercase + string.ascii_uppercase + string.digits
# generated_password = ''.join(random.choice(chars) for char in range(length))
# return int(generated_password)
def check_profile_exists(profile_name, profile_username = None, profile_email = None):
return Credential.check_profile_exist(profile_name, profile_username = None, profile_email = None)
def search_profile(search):
return Credential.search_profile(search)
def copy_password(search_item):
return Credential.copy_credentials(search_item)
def display_profiles():
return Credential.profile_list
def handle_short_codes(short_code):
short_code = short_code.lower().replace(" ", "")
if short_code == "np":
cprint("You have entered a command to Create a New Profile".center(terminal_width), "blue")
print("Kindly input a Profile Name...example github")
profile_name_entered = input()
if not profile_name_entered:
cprint("Your Profile Name Cannot Be Blank", "red")
else:
print("Kindly enter Username of the Profile Account(optional)")
profile_username_entered = input()
print("Kindly enter Email of the Profile Account(optional)")
profile_email_entered = input()
print("Kindly enter Password of the Account(optional)")
profile_password_entered = input()
new_profile = Credential(profile_name_entered, profile_username_entered, profile_email_entered, profile_password_entered)
profile_exist = check_profile_exists(profile_name_entered, profile_username_entered, profile_email_entered)
if profile_exist:
print("\n")
cprint("New Profile Created".center(terminal_width),"green")
print("\n")
else:
if not save_profile(new_profile):
print("\n")
cprint("Your Profile Has Been Created.".center(terminal_width), "green")
print("\n")
elif short_code == "dp":
show_profile = display_profiles()
if not show_profile:
print("\n")
cprint("THERE IS NO PROFILE SAVED IN YOUR ACCOUNT".center(terminal_width), "red")
print("\n")
else:
cprint("Here's a list of all your profiles".center(terminal_width),"blue")
print("\n")
print(("-*-"*25).center(terminal_width))
for profile in display_profiles():
print(f"PROFILE NAME:{profile.profile_name}".center(terminal_width))
print(f"PROFILE USERNAME:{profile.profile_username}".center(terminal_width))
print(f"PROFILE EMAIL:{profile.profile_email}".center(terminal_width))
print(f"PROFILE PASSWORD:{profile.profile_password}".center(terminal_width))
print(("-*-"*25).center(terminal_width))
print("\n")
elif short_code == "gp":
print("Kindly enter the profile name you want to generate a password for")
profile_gen_passwrd = input()
profile_to_change = search_profile(profile_gen_passwrd)
if profile_to_change:
print("Input the length of password you want:")
passwrd_length = int(input())
new_passwrd = generate_random_password(passwrd_length)
profile_to_change.profile_password = new_passwrd
print("\n")
cprint("A New Password was Generated and has been Successfully Saved".center(terminal_width), "green")
print("\n")
else:
print("\n")
cprint("There is no Profile with That Name".center(terminal_width), "red")
print("\n")
elif short_code == "search":
print("Kindly enter your search")
search_string = input()
if search_string:
search_result = search_profile(search_string)
if search_result:
print("\n")
cprint("SEARCH RESULTS".center(terminal_width),"green")
print("\n")
print(("-*-"*25).center(terminal_width))
print(f"PROFILE NAME:{search_result.profile_name}".center(terminal_width))
print(f"PROFILE USERNAME:{search_result.profile_username}".center(terminal_width))
print(f"PROFILE EMAIL:{search_result.profile_email}".center(terminal_width))
print(f"PROFILE PASSWORD:{search_result.profile_password}".center(terminal_width))
print(("-*-"*25).center(terminal_width))
print("\n")
else:
print("\n")
cprint("No items were found using that criteria".center(terminal_width),"magenta")
print("\n")
else:
cprint("You MUST input a search item","red")
elif short_code == "copy":
print("Kindly enter the profile you want to copy(password)")
search_passwrd = input()
found_copy_profile = search_profile(search_passwrd)
if not search_passwrd:
cprint("You MUST input the profile you want to copy password","red")
else:
if not found_copy_profile:
cprint("\t Profile found!","red",attrs=["bold"])
else:
found_copy_profile = copy_password(search_passwrd)
paste_passwrd = pyperclip.paste()
if paste_passwrd:
cprint("\t Copied!!","green")
print("\n")
else:
cprint("\t NOT copied!!!","red")
print("\n")
elif short_code == "del":
cprint("PROCEED WITH CAUTION!".center(terminal_width),"red",attrs=["bold","blink"])
print("Enter the name of the profile you want to delete:")
del_profile = input()
found_profile = search_profile(del_profile)
if found_profile:
cprint("DO YOU WANT TO CONTINUE TO DELETE? Y/N",attrs=["bold"])
continue_prompt = input().upper()
if continue_prompt == "Y":
delete_profile(found_profile)
print("\n")
cprint("Profile DELETED!".center(terminal_width),"green")
print("\n")
elif continue_prompt == "N":
return
else:
cprint("\t You input an unrecognised command","red")
else:
print("\n")
cprint("The profile does NOT exist".center(terminal_width),"red")
print("\n")
elif short_code=="cap":
cprint("WE NEED TO CONFIRM ITS YOU!".center(terminal_width),"red",attrs=['bold','blink'])
print("Enter your username")
passwrd_change_username = input()
print("Enter your account password")
passwrd_change_password = input()
if not (passwrd_change_password or passwrd_change_username):
cprint("You submitted empty field(s)")
print("\n")
else:
data_match = login_user(passwrd_change_username, passwrd_change_password)
if data_match:
print("Enter your new password")
new_entered_passwrd = input()
print("Confirm password")
confirm_entered_passwrd = input()
if new_entered_passwrd == confirm_entered_passwrd:
user_change_password(passwrd_change_username, new_entered_passwrd)
print("\n")
cprint("Password changed","green")
print("\n")
else:
print("\n")
cprint("Password confirmation does NOT match","red")
print("\n")
else:
print("\n")
cprint("ERROR, please check your login details and try again","red")
print("\n")
elif short_code=="delete":
cprint("WE NEED TO CONFIRM ITS YOU!".center(terminal_width),"red",attrs=["bold","blink"])
print("Enter your username")
passwrd_change_username = input()
print("Enter your account password")
passwrd_change_password = input()
if not (passwrd_change_password or passwrd_change_username):
cprint("You submitted empty field(s)")
print("\n")
else:
data_match = login_user(passwrd_change_username, passwrd_change_password)
user_acc = user_change_password(passwrd_change_username, passwrd_change_password)
if data_match:
delete_account(user_acc)
print("\n")
cprint("User DELETED","green")
print("\n")
return
else:
print("\n")
cprint("ERROR, please check your Account Details and try again","red")
print("\n")
elif short_code=="logout":
return
elif short_code == "ex":
cprint("\t BYE. . ...","cyan")
sys.exit()
else:
print("\n")
cprint("You input an unrecognised command".center(terminal_width),"red")
print("\n")
def main():
cprint(figlet_format('LOCKER', font='speed'),'green', attrs=['bold'])
cprint("\033[1m" + "Hello and Welcome to the Password Locker".center(terminal_width),"white", attrs=['bold','blink'])
while True:
print("Do you have an account? Y/N")
account_prompt = input().upper().strip()
if account_prompt == "Y":
print("Enter your username:")
existing_username = input()
if not existing_username:
cprint("You have not entered a username !","red")
print("Enter your username:")
existing_username = input()
print("Enter your password:")
existing_password = input()
if not existing_password:
cprint("You have not entered a password!","red")
print("Enter your password:")
existing_password = input()
login_success = login_user(existing_username, existing_password)
if not login_success:
print("\n")
cprint("Incorrect username / password combination","red")
print("\n")
else:
while True:
print("\033[1m PROFILE CONTROLS:- "+'\033[0m'+"Use these short codes : np - Add a new profile, dp-Display all profiles, gp - generate new password for a profile, search - find a profile, copy - copy password to clipboard, del - delete a profile, logout- logout of session, ex - exit the application")
print("\033[1m ACCOUNT CONTROLS:- "+'\033[0m'+"Use these short codes : acp - Change your account password, delete - Delete your account")
short_code = input()
handle_short_codes(short_code)
if short_code=="logout" or short_code=="delete":
break
elif account_prompt == "N":
print("Enter your details to create a new account".center(terminal_width))
print("Please enter your preffered username")
new_user_username = input()
if not new_user_username:
cprint("You have not entered any username!","red")
new_user_username = input()
print("Please enter your password")
new_user_password = input()
if not new_user_password:
cprint("You have not entered any password!","red")
new_user_password = input()
user_already_exist = check_user_exists(new_user_username)
if not user_already_exist:
user_new = User(new_user_username, new_user_password)
if not save_account(user_new):
print("\n")
cprint("\033[1m Account created successfully \033[0m".center(terminal_width),"green")
print("\n")
while True:
print("\033[1m PROFILE CONTROLS:- "+'\033[0m'+"Use these short codes : np - Add a new profile, dp-Display all profiles, gp - generate new password for a profile, search - find a profile, copy - copy password to clipboard, del - delete a profile, logout- logout ex - exit the application")
print("\033[1m ACCOUNT CONTROLS:- "+'\033[0m'+"Use these short codes : cap - Change your account password, delete - Delete your account")
short_code = input()
handle_short_codes(short_code)
if short_code=="logout" or short_code=="delete":
break
else:
print("\n")
cprint("The username is already in use","magenta")
cprint("Please try another username","magenta")
print("\n")
else:
print("\n")
cprint("You Input an unrecognised command...Please enter Y/N!","red")
print("\n")
print("\n")
cprint("An interrupt detected...Exiting..", "red", attrs=["bold"])
sys.exit()
if __name__ == "__main__":
main()