-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
261 lines (192 loc) · 5.7 KB
/
main.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
from src import *
import getpass
import os
from sys import platform
import pyperclip
from prettytable import PrettyTable
cmd = ''
KEYPATH = os.getcwd()
if platform == "linux" or platform == "linux2":
cmd = 'clear' # Linux
elif platform == "win32":
cmd = 'cls' # Windows
def main():
"""
All Starts Here
"""
# Get DB access
masteruser = input("Enter Master username: ").strip()
masterpass = getpass.getpass("Enter Master Password: ")
try:
# Try accessing to database
access = DB(masteruser, 'manager', masterpass)
except Exception as err:
# Wrong credentials
print(err)
else:
# App Starts Here
while True:
# Nice Logo
printLogo()
print('Choose one of the following:\n1. Search Password\n2. Add New Password\n3. Delete Password\n4. View Password List\n5. Exit')
i = input()
if i == '1':
# Search for password
printLogo()
searchPass(access)
elif i == '2':
# Store New Password
printLogo()
storePassword(access)
elif i == '3':
# Delete Password
printLogo()
deletePass(access)
elif i == '4':
# Show all passwords stored
printLogo()
showList(access)
elif i == '5':
# Quit
clear()
break
else:
# Wrong input
printLogo()
print("Wrong Input")
def deletePass(access):
"""Delete a password
Args:
access (DB): DB class object
"""
print("Enter url and email of the password to be deleted")
url = input("Enter url: ")
email = input("Enter email: ")
access.deleteEntry({'url' : url, 'email' : email})
print("Deleted! Press enter to continue")
input()
def showList(access):
"""Show list of passwords in the database
Args:
access (DB): DB class object
"""
passList = access.getall()
# Create a table in cmd line
if passList:
x = PrettyTable()
x.field_names = ["URL", "email", "password", "Notes"]
for i in passList:
x.add_row(
[i[2], i[1], '****',i[4]]
)
print(x.get_string())
else:
# No passwords available
print('\nNo Passwords Available')
print('Press enter to exit')
input()
def searchPass(access):
"""Search for a specific password.
Args:
access (DB): DB class object
"""
print("Search by url and email")
email = input("Enter email: ")
url = input("Enter url: ")
data = access.getdata({'url' : url, 'email' : email})
if data:
# Copy to clipboard
pyperclip.copy(decrypt(data[3]).decode())
print("\nCopied to clipboard press ENTER to exit")
else:
print("\nNo Matches found. Press enter to go back")
input()
def storePassword(access):
"""Store new password
Args:
access (DB): DB class object
"""
# Ask if needed to generate new password or just save
i = input('1. Generate and store password\n2. Just Store the password\n')
if i == '1':
# Generate and save
printLogo()
genetateNStore(access)
elif i == '2':
# Just Save
printLogo()
savepass(access)
def genetateNStore(access):
"""Generate and store in Database
Args:
access (DB): DB class object
"""
while True:
print("Enter -1 to exit\n")
# Ask for a len of password > 6
l = input("Enter Length of password ( > 6): ")
try:
# Check if the input is an integer
t = int(l)
except:
# If not integer
printLogo()
print("Please enter integer")
else:
# If Integer
if t == -1:
# Exit
return
elif int(l) > 6:
# If len > 6
break
else:
# if len < 6
printLogo()
print("Please give a length > 6\n")
# Generate password
password = generator(int(l))
printLogo()
# Save the password
savepass(access, password=password)
def savepass(access, password = ''):
"""encrypt and save the record in the database
Args:
access (DB): DB class object
password (str): Passed if a password is generated. Defaults to ''.
"""
# take details
print('(Not all details are compulsory exept for password, email and url)\nEnter Details:')
url = input('Url: ')
username = input('Username: ')
email = input('Email: ')
if password == '':
# If just password
password = input('Password: ')
else:
# If generated password, copied to clipboard
print(f"Password: {'*'* len(password)}\nPassword Copied to clipboard")
pyperclip.copy(password)
notes = input('Notes: ')
# If relevant data
if password and url and email:
access.savedata({
'url' : url,
'username' : username,
'password' : password,
'email' : email,
'notes' : notes
})
print("Data Saved!\n Press enter to continue")
else:
# If not
print("Insufficient data submitted. Please try again")
input()
if __name__ == "__main__":
# Setup.py takes care of this
if KEYPATH == '':
print('Please Run setup.py first')
elif os.path.exists(KEYPATH):
main()
else:
print('ERROR! Fix KEYPATH in main.py')