-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
118 lines (107 loc) · 3.54 KB
/
app.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
"""
Usage:
Crypto --c <secret>
Crypto --r <secret>
Crypto --u <secret>
Crypto --d <secret>
Arguments:
--c <secret> Create Secret
--r <secret> Retrieve Secret
--u <secret> Update Secret
--d <secret> Delete Secret
Options:
-h, --help Show this screen
-version Show Version Information
"""
import os
import sys
from getpass import getpass
from docopt import docopt
from Crypto import DB, PySecrets
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
else:
application_path = os.path.dirname(__file__)
def strtobool(val: str) -> bool:
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
else:
return False
# noinspection PyTypeChecker
def main(**kwargs):
db = DB(root_dir=application_path)
secret = PySecrets()
if kwargs['--c']:
sec = kwargs['--c']
while True:
_user = input(f"Enter username for {sec}: ")
if not _user:
print('Invalid entry. Try again.')
continue
else:
break
while True:
_pass = getpass(f"Enter password for username: {_user}: ")
if not _pass:
print('Invalid entry. Try again')
continue
else:
break
user = secret.make_secret(_user)
passw = secret.make_secret(_pass)
db.create_secrets(secret=sec, username=user, password=passw)
elif kwargs['--r']:
try:
if not db.secret_exists(kwargs['--r']):
raise ValueError("Secret does not exist in the database")
_user = None
_password = None
results = db.retrieve_secrets(kwargs['--r'])
for result in results:
_user = result.username
_password = result.password
user = secret.make_public(_user)
password = secret.make_public(_password)
print(f"user={user}, password={password}")
except ValueError as v:
print(v)
elif kwargs['--u']:
try:
user = None
_user = None
sec = kwargs['--u']
if not db.secret_exists(secret=sec):
raise ValueError("Secret does not exist in the database. Use --c")
user_up = strtobool(input(f"Update username? (default=N): "))
if user_up:
while True:
_user = input(f"Enter username for {sec}: ")
if not _user:
print('Invalid entry. Try again.')
continue
else:
break
while True:
_pass = getpass(f"Enter updated password: ")
if not _pass:
print('Invalid entry. Try again.')
continue
else:
break
if user_up:
user = secret.make_secret(secret=_user)
passw = secret.make_secret(secret=_pass)
db.update_secret(secret=sec, username=user, password=passw)
except ValueError as t:
print(t)
elif kwargs['--d']:
try:
if not db.secret_exists(kwargs['--d']):
raise ValueError("Secret does not exist in the database")
db.delete_secret(kwargs['--d'])
except ValueError as v:
print(v)
if __name__ == "__main__":
cmd_args = docopt(__doc__, version="Crypto 1.0")
main(**cmd_args)