-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathotpgui.py
executable file
·191 lines (164 loc) · 7.73 KB
/
otpgui.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
#!/usr/bin/python3
"""
otpgui.py is an OTP generator compatible with TOTP.
Copyright (C) 2018 Gianluca Mascolo <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import yaml,gi,time,pyotp,subprocess,argparse,sys,os
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk,Gtk,GObject,GLib
from otpversion import program_version
class OtpSettings:
def __init__(self):
homedir = os.environ["HOME"]
xdg_config_home = os.environ.get("XDG_CONFIG_HOME",f"{homedir}/.config")
otp_settings_home = xdg_config_home + "/otpgui"
otp_settings_file = otp_settings_home + "/settings.yml"
if not os.path.isdir(otp_settings_home):
os.makedirs(otp_settings_home)
if not os.path.isfile(otp_settings_file):
self.otp_settings_data = {"config_file": f"{otp_settings_home}/otp.yml","encryption_method": "plain"}
with open(otp_settings_file, 'w') as f:
yaml.dump(self.otp_settings_data, f)
else:
try:
with open(otp_settings_file, 'r') as f:
self.otp_settings_data = yaml.safe_load(f)
except yaml.YAMLError as exc:
print(f"Cannot read settings file: {exc}")
self.otp_settings_data = None
if not os.path.isfile(self.otp_settings_data['config_file']):
default_config_file = {"otp":
{ "default":
{
"name": "default tooltip",
"genstring":"ABCDEFGHIJKLMNOP"
}
}
}
with open(self.otp_settings_data['config_file'], 'w') as f:
yaml.dump(default_config_file, f)
def settings(self):
return self.otp_settings_data
class OtpStore:
def __init__(self,config_file,encryption_method):
self.config_file = config_file
self.sops_cmd = f"sops -d --extract"
self.encryption_method=encryption_method
try:
with open(config_file, 'r') as file:
config_yaml = yaml.safe_load(file)
self.config_data = config_yaml['otp']
self.otplist = sorted(config_yaml['otp'])
except yaml.YAMLError as exc:
print(f"Error in configuration file: {exc}")
self.config_data = None
self.otplist = None
def getlabel(self,label):
self.label = label
self.tooltip = self.config_data[label]['name']
def getgenerator(self):
if self.encryption_method == "sops":
gensel = f"['otp']['{self.label}']['genstring']"
gen_decrypt = subprocess.run(f"{self.sops_cmd} \"{gensel}\" {self.config_file}",capture_output=True,shell=True,universal_newlines=True,check=True)
self.genstring = gen_decrypt.stdout
elif self.encryption_method == "plain":
self.genstring = self.config_data[self.label]['genstring']
def otpcode(self):
totp = pyotp.TOTP(self.genstring)
return totp.now()
def progress(self):
return ((30-time.time()%30)/30)
class MyWindow(Gtk.Window):
def __init__(self,otp):
self.otp = otp
Gtk.Window.__init__(self, title="OTP")
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)
self.OtpCode = Gtk.Button.new_with_label(otp.otpcode())
self.OtpCode.set_tooltip_text(otp.tooltip)
self.OtpCode.connect("clicked", self.on_otp_clicked)
vbox.pack_start(self.OtpCode, True, True, 0)
self.ProgressBar = Gtk.ProgressBar(fraction=otp.progress())
vbox.pack_start(self.ProgressBar, True, True, 0)
self.timeout_id = GLib.timeout_add(1000, self.on_timeout)
self.activity_mode = False
self.OtpLabelStore = Gtk.ListStore(str)
for key in otp.otplist:
self.OtpLabelStore.append([key])
self.OtpCombo = Gtk.ComboBox.new_with_model(self.OtpLabelStore)
self.OtpCombo.set_active(otp.otplist.index(otp.label))
self.OtpCombo.connect("changed", self.on_otp_changed)
renderer_text = Gtk.CellRendererText()
self.OtpCombo.pack_start(renderer_text, True)
self.OtpCombo.add_attribute(renderer_text, "text", 0)
vbox.pack_start(self.OtpCombo, False, False, 0)
def on_timeout(self):
new_value = self.otp.progress()
self.ProgressBar.set_fraction(new_value)
self.OtpCode.set_label(self.otp.otpcode())
self.OtpCode.set_tooltip_text(self.otp.tooltip)
return True
def on_otp_changed(self, combo):
tree_iter = combo.get_active_iter()
if tree_iter is not None:
model = combo.get_model()
SelectedLabel = model[tree_iter][0]
self.otp.getlabel(SelectedLabel)
self.otp.getgenerator()
def on_otp_clicked(self,OtpCode):
self.clipboard.set_text(self.OtpCode.get_label(), -1)
def main():
otp_settings_init = OtpSettings()
otp_settings = otp_settings_init.settings()
parser = argparse.ArgumentParser()
parser.add_argument("-c","--config-file", help="Path to otp.yml configuration file", type=str,default=otp_settings['config_file'])
parser.add_argument("-e","--encryption-method", help="Encryption method to use.",choices=["plain", "sops"],default=otp_settings['encryption_method'])
parser.add_argument("-i","--interface", help="Interface to use. Default: gtk",choices=["gtk", "script"], default="gtk")
parser.add_argument("-l","--label", help="Otp label to display on startup or script. Default to first label (sorted alphabetical) in configuration file.", type=str)
parser.add_argument("-v","--version", help="show version",action="store_true")
args = parser.parse_args()
if args.version:
print(f"{program_version}")
sys.exit(0)
config_file = args.config_file
encryption_method = args.encryption_method
interface = args.interface
otplabel = args.label
if encryption_method == "sops":
try:
subprocess.run(f"sops -v",capture_output=True,shell=True,universal_newlines=True,check=True)
except subprocess.CalledProcessError as err:
print("Cannot run sops executable. Is it in your PATH?")
print(f"{err}")
sys.exit(1)
otp = OtpStore(config_file=config_file,encryption_method=encryption_method)
if otplabel == None:
otp.getlabel(otp.otplist[0])
else:
try:
otp.getlabel(otplabel)
except KeyError as err:
print(f"Label not found\nError: {err}")
sys.exit(1)
otp.getgenerator()
if interface == "gtk":
win = MyWindow(otp)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
elif interface == "script":
print("OTP_LABEL={otplabel}\nOTP_CODE={otpcode}".format(otplabel=otp.label,otpcode=otp.otpcode()))
if __name__ == '__main__':
main()