-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·157 lines (110 loc) · 4.44 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
#!/usr/bin/env python3
from core import *
from fire import Fire
banner = """
______ _______ ___
| _ \ (_) ___/ _ \
| | | |_ _ _| |_ / /_\ \\
| | | | | | | | _|| _ |
| |/ /| |_| | | | | | | |
|___/ \__,_|_\_| \_| |_/
"""[1:-1]
print('\x1b[1m'+ banner +'\x1b[0m')
class Duifa:
def __init__(self, add: bool = False, remove: bool = False, a: bool = False, r: bool = False, rm: bool = False):
print(info(f'Initialized [at] -> {fetchFormatedTime()}'))
self.secret_files = self.list_secret_files()
if any((add, a)):
self.add()
elif any((remove, rm, r)):
self.remove()
else:
print(good('Select an App. no -> whoose you wish to see OTP of.'))
self.display()
coolExit()
def list_secret_files(self):
secret_files = []
for _ in listdir():
if _.endswith('_secret.txt'):
secret_files.append(_)
if not secret_files:
print(bad('No secret files -> You should first add a TOTP secret using `-a` flag.'))
coolExit(1)
self.secret_files = secret_files
return secret_files
def display(self, choice=None):
if type(choice) != int:
choice = self.choice()
if type(choice) is bool and choice:
for secret_filename in self.secret_files:
otp = TOTP(read_secret(secret_filename))
print(good(f'{secret_filename[:-11]} -> {otp.now()}'))
else:
secret_filename = self.secret_files[choice]
app_name = secret_filename[:-11]
otp = TOTP(read_secret(secret_filename))
print(good(f'{app_name}\'s OTP (Copied to clipboard.) -> {otp.now()}'))
copy(str(otp.now()))
def add(self):
app_name = coolInput('App name')
secret = get_secret('App secret')
filename = f'{app_name}_secret.txt'
cipher = Cipher()
c_secret = cipher.encrypt(secret.strip().replace(' ', ''))
if not path.isfile(filename):
open(filename, 'w').write(c_secret)
sleep(0.4)
self.display(self.list_secret_files().index(filename))
print(good(f'{app_name} -> has been added to your TOTP secret storage.'))
else:
print(bad(f'{app_name} -> a TOTP secret already exists.'))
coolExit(1)
def choice(self):
for _ in range(len(self.secret_files)):
print(info(f'{_} -> {self.secret_files[_][:-11]}'))
try:
choice = int(coolInput('App no'))
except Exception as exception:
print(bad(f'Exception -> {exception}'))
coolExit(1)
if choice > _:
print(bad("Error -> Given App no doesn't exist."))
coolExit(1)
elif choice < 0:
return True
return choice
def remove(self):
print(bad('Specify an App. no -> that you want to get rid of.'))
choice = self.choice()
if type(choice) is bool and choice:
print(bad(f'Are you sure? -> you want to remove all TOTP secrets?'))
prompt = coolInput('yes/[N]o')
sleep(3.14285)
if prompt.lower() in ('y', 'yes'):
try:
for secret_filename in self.secret_files:
remove(secret_filename)
print(good(f'{secret_filename[:-11]}\'s TOTP secret -> \x1b[1mRemoved sucessfully.'))
sleep(0.7142857143)
except Exception as e:
print(bad(f'Exception -> {e}'))
coolExit(1)
else:
coolExit()
else:
secret_filename = self.secret_files[choice]
app_name = secret_filename[:-11]
print(bad(f'Are you sure? -> You want to remove {app_name}\'s TOTP secret.'))
prompt = coolInput('yes/[N]o')
if prompt.lower() in ('y', 'yes'):
try:
remove(secret_filename)
print(good(f'{app_name}\'s TOTP secret -> Removed sucessfully.'))
except Exception as exp:
print(bad(f'Exception -> {exp}'))
coolExit(1)
else:
coolExit()
if __name__ == '__main__':
chdir('.secrets')
Fire(Duifa)