-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyubioath-menu.py
executable file
·250 lines (187 loc) · 5.79 KB
/
yubioath-menu.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
#!/usr/bin/env python
from __future__ import unicode_literals, print_function
import re
import os
import io
import sys
import cgi
from subprocess import Popen, PIPE
gtk = None
FILTER = []
LAST_EVENT_TIME = 0
def gtk_error(message):
msg = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
flags=gtk.DIALOG_MODAL,
buttons=gtk.BUTTONS_OK)
msg.set_markup(message)
msg.run()
def error(message, fatal=False):
if sys.stdin.isatty()or gtk is None:
print(message, file=sys.stderr)
else:
gtk_error(message)
if fatal:
sys.exit(1)
def which(program):
"""Which witch?"""
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
try:
from yubioath.core.ccid import open_scard
from yubioath.core.controller import Controller as YubiController
except ImportError:
open_scard = YubiController = None
try:
import gtk
except ImportError:
# python3 -> gruik compat
try:
from gi import pygtkcompat
pygtkcompat.enable_gtk(version='3.0')
import gtk
except ImportError:
pass
class NoCardFound(Exception):
pass
def get_yubidata():
cmd = Popen(['yubioath'], stdout=PIPE)
for line in cmd.stdout:
label, token = line.strip().rsplit(' ', 1)
yield label.strip(), token
def get_yubidata():
controller = YubiController()
card = open_scard()
if card is None:
raise NoCardFound
for cred, token in controller.read_creds(card, None, None, None):
yield cred.name, token
def type_token(token, delay=0):
if delay > 0:
sleep(delay)
cmd = Popen(['xdotool', 'type', '--file', '-'], stdin=PIPE)
cmd.stdin.write(token.encode('utf-8'))
cmd.stdin.flush()
cmd.stdin.close()
cmd.wait()
def on_menu_select(item):
type_token(item.token)
def activate_by_gruik(menu):
# simulate events to activate menu item since
# xdotool + gtk event seems to break
# Press Down
ev = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
ev.send_event = True
ev.time = 0
ev.keyval = gtk.keysyms.Down
ev.window = menu.get_window()
ev.state = gtk.gdk.KEY_PRESS_MASK
ev.hardware_keycode = 116 # Key Down
ev.put()
# Press Return
ev = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
ev.send_event = True
ev.time = 0
ev.keyval = gtk.keysyms.Return
ev.window = menu.get_window()
ev.state = gtk.gdk.KEY_PRESS_MASK
ev.hardware_keycode = 36 # Return
ev.put()
def exit(*args):
gtk.main_quit()
def get_visible_children(menu):
return [item for item in menu.get_children() if item.get_visible()]
def on_key_press(menu, event):
global LAST_EVENT_TIME
# key-release events seems to be triggered twice
if event.get_time() == LAST_EVENT_TIME:
return
LAST_EVENT_TIME = event.get_time()
string = event.string
if event.keyval == gtk.keysyms.BackSpace:
FILTER[:] = []
elif event.keyval in (gtk.keysyms.KP_Enter, gtk.keysyms.Return):
# Enter, validate if only one match shown
visible = get_visible_children(menu)
if len(visible) == 1 and visible[0].get_sensitive():
activate_by_gruik(menu)
return
elif len(string):
FILTER.append(string)
else:
# unhandled key
return
match = False
search = ''.join(FILTER)
regsearch = re.compile('(%s)' % re.escape(cgi.escape(search)), re.I)
for item in menu.get_children():
if not item.get_sensitive():
continue
clabel = cgi.escape(item.origin)
if regsearch.search(clabel):
if search != '':
markup = regsearch.sub('<b>\\1</b>', clabel)
else:
markup = clabel
item.get_child().set_markup(markup)
item.show()
match = True
else:
item.get_child().set_markup(clabel)
item.hide()
if match:
menu.no_match.hide()
else:
menu.no_match.get_child().set_markup('<i>No match for %s</i>' %
cgi.escape(search))
menu.no_match.show()
def checkup():
"""Check global import / binary availability"""
if gtk is None:
error('Unable to import python GTK module, aborting', True)
if YubiController is None:
error('Unable to import python yubioath module. Is it installed?',
True)
if which('xdotool') is None:
error('Unable to find the xdotool binary, aborting', True)
def main():
checkup()
menu = gtk.Menu()
menu.no_match = gtk.MenuItem('')
menu.no_match.set_sensitive(False)
menu.append(menu.no_match)
no_yubikey= gtk.MenuItem('')
no_yubikey.set_sensitive(False)
no_yubikey.get_child().set_markup('<b>No Yubikey found</b>')
menu.append(no_yubikey)
try:
for label, token in get_yubidata():
item = gtk.MenuItem(label)
item.origin = label
item.token = token
item.connect('activate', on_menu_select)
item.show()
menu.append(item)
except NoCardFound:
no_yubikey.show()
menu.connect('hide', gtk.main_quit)
menu.connect('key-release-event', on_key_press)
menu.popup(parent_menu_shell=None,
parent_menu_item=None,
func=None,
data=None,
button=1,
activate_time=0)
gtk.main()
if __name__ == '__main__':
main()