-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
196 lines (158 loc) · 5.47 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
#!/usr/bin/python3
import time, os, sys, pyotp, json, shutil, base64
import tkinter
from tkinter import ttk
from tkinter.font import nametofont
from tkinter import messagebox
from json_file import JsonFile
app_path = os.path.dirname(sys.argv[0])
window = tkinter.Tk()
window.title("Py2FA Google Authenticator")
window.geometry("800x700")
window.resizable(False, False)
# Style
style = ttk.Style(window)
# Style theme
window.tk.call("source", app_path + "/forest-light.tcl")
window.tk.call("source", app_path + "/forest-dark.tcl")
# Set theme
style.theme_use("forest-dark")
# Dialog box font
window.option_add("*Dialog.msg.font", "roboto 16")
# Main frame
fr = tkinter.Frame(window)
fr.pack()
# Callbacks
def load_data():
jf = JsonFile("secrets.json")
return jf.getAll()
def selectItem(event):
tree = event.widget
current_item = tree.focus()
item = tree.item(current_item)
# print("Selected Item", item)
try:
if hasattr(item, "values"):
code = format(item["values"][2], "06d")
messagebox.showinfo("Code", code)
# print("Id:", current_item, "Item:", item, "Code:", code)
except (IndexError, Exception):
# print("Item not selected")
pass
def update():
t = time.strftime("%H:%M:%S", time.localtime())
treeview.delete(*treeview.get_children())
i = 0
for item_tuple in load_data():
if i % 2 == 0:
treeview.insert("", tkinter.END, id=i, values=item_tuple, tags=("odd",))
else:
treeview.insert("", tkinter.END, id=i, values=item_tuple, tags=("even",))
i = i + 1
window.after(15000, update)
print("Update", t)
# Show user codes
fr_codes = tkinter.LabelFrame(fr, text="2FA Codes", font="roboto 16")
fr_codes.grid(row=0, column=0)
treeFrame = ttk.Frame(fr_codes, padding=(10, 10))
treeFrame.grid(row=0)
treeScroll = ttk.Scrollbar(treeFrame)
treeScroll.pack(side="right", fill="y")
cols = ("Name", "Secret", "Code")
treeview = ttk.Treeview(
treeFrame,
show="headings",
columns=cols,
height=13,
selectmode="browse",
yscrollcommand=treeScroll.set,
padding=(-4, -4),
# displaycolumns=("Name", "Code"),
)
# Headings
treeview.heading(cols[0], text=cols[0], anchor=tkinter.W)
treeview.heading(cols[1], text=cols[1], anchor=tkinter.W)
treeview.heading(cols[2], text=cols[2], anchor=tkinter.E)
# Heading style
treeview.column("Name", width=200, stretch=True, anchor=tkinter.W)
treeview.column("Secret", width=200, stretch=True, anchor=tkinter.W)
treeview.column("Code", width=250, stretch=True, anchor=tkinter.E)
# Select event
treeview.bind("<<TreeviewSelect>>", selectItem)
# treeview.bind('<ButtonRelease-1>', selectItem)
# Style rows silver
treeview.tag_configure("odd", background="#3a4a5e", foreground="#ffffff")
treeview.tag_configure("even", background="#283240", foreground="#ffffff")
# # Blue
# treeview.tag_configure("odd", background="#0550ae", foreground="#ffffff")
# treeview.tag_configure("even", background="#0a3069", foreground="#ffffff")
# # Violet
# treeview.tag_configure("odd", background="#8250df", foreground="#ffffff")
# treeview.tag_configure("even", background="#a475f9", foreground="#ffffff")
# # Red
# treeview.tag_configure("odd", background="#82071e", foreground="#ffffff")
# treeview.tag_configure("even", background="#a40e26", foreground="#ffffff")
i = 0
for item_tuple in load_data():
if i % 2 == 0:
treeview.insert("", tkinter.END, id=i, values=item_tuple, tags=("odd",))
else:
treeview.insert("", tkinter.END, id=i, values=item_tuple, tags=("even",))
i = i + 1
# Add
treeview.pack(fill="x")
# Scrollbar
treeScroll.config(command=treeview.yview)
# Style treeview
style.configure("Treeview.Heading", font=("Roboto", 13))
style.configure(
"Treeview", font=("Roboto", 14), rowheight=30, highlightthickness=0, bd=0
)
# style.layout("Treeview", [("mystyle.Treeview.treearea", {"sticky": "nswe"})])
# nametofont("TkHeadingFont").configure(family="roboto", size=16, weight="bold")
# Saving user secret callbacks
def callback_add():
name = input_name.get()
secret = input_secret.get()
input_name.delete(0, "end")
input_secret.delete(0, "end")
js = JsonFile("secrets.json")
if len(name) >= 3:
if len(secret) >= 16:
# print("Name", name, "Secret", secret)
js.addItem(name, secret)
update()
def callback_del():
name = input_name.get()
input_name.delete(0, "end")
js = JsonFile("secrets.json")
js.removeItem(name)
update()
# Saving user secret
fr_add_secret = tkinter.LabelFrame(
fr, text="Add 2FA Secret", font="roboto 16", padx=5, pady=10
)
fr_add_secret.grid(row=1, column=0)
# fr_add_secret.pack(fill="x")
label_name = tkinter.Label(
fr_add_secret, text="App name", font="roboto 14", anchor=tkinter.W
)
label_name.grid(row=0, column=0)
label_secret = tkinter.Label(
fr_add_secret, text="App secret", font="roboto 14", anchor=tkinter.W
)
label_secret.grid(row=0, column=1)
input_name = tkinter.Entry(fr_add_secret, font="roboto 15", width=26, border=1)
input_name.grid(row=1, column=0, padx=5, pady=5)
input_secret = tkinter.Entry(fr_add_secret, font="roboto 15", width=26, border=1)
input_secret.grid(row=1, column=1, padx=5, pady=5)
button_add = tkinter.Button(
fr_add_secret, text="Save", command=callback_add, font="roboto 15"
)
button_add.grid(row=2, column=1)
button_del = tkinter.Button(
fr_add_secret, text="Delete", command=callback_del, font="roboto 15"
)
button_del.grid(row=2, column=0)
update()
window.mainloop()