-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
121 lines (93 loc) · 4.37 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
from operator import itemgetter
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.client.Extension import Extension
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.utils.fuzzy_search import get_score
import subprocess
import logging
logger = logging.getLogger()
twoFactorAuthenticatorCommand = "2ami"
generateCommand = twoFactorAuthenticatorCommand + " generate %s --clip"
listCommand = twoFactorAuthenticatorCommand + " list"
def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, _ = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def checkForCommand(command):
commandFound = which(command)
if commandFound:
logger.debug("command found: %s" % commandFound)
return True
logger.error("%s command not found" % command)
return False
class TwoFactorAuthenticatorExtension(Extension):
def __init__(self):
super(TwoFactorAuthenticatorExtension, self).__init__()
if not checkForCommand(twoFactorAuthenticatorCommand):
logger.warn(
"two-factor-authenticator command not found or not executable, extension halted")
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
logger.info("Initialized extension")
class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
data = event.get_data()
logger.info("Generating token for %s" % data)
subprocess.Popen(generateCommand % data, shell=True)
return HideWindowAction()
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
if not checkForCommand(twoFactorAuthenticatorCommand):
logger.warn(
"two-factor-authenticator command not found or not executable, extension halted")
return RenderResultListAction([ExtensionResultItem(
icon=None, name='two-factor-autenticator command not found')])
proc = subprocess.Popen(listCommand, shell=True,
stdout=subprocess.PIPE)
profile_list = proc.stdout.read().decode("utf-8")
available_profiles = [
x for x in profile_list.split("\n") if x != ""]
logger.debug("Loaded 2fa profile list")
items = []
data = event.get_argument()
available_profiles = [{'name': x, 'score': 0}
for x in available_profiles]
for d in available_profiles:
d['score'] = get_score(data, d['name'])
logger.debug(available_profiles)
if data:
available_profiles = sorted(available_profiles, key=itemgetter('score'),
reverse=True)
logger.debug(available_profiles)
fuzzy_threshold = float(
extension.preferences['2fa_fuzzy_threshold'])
logger.debug(fuzzy_threshold)
available_profiles = [
x for x in available_profiles if x['score'] > fuzzy_threshold]
logger.debug(available_profiles)
if len(available_profiles) == 0:
items.append(ExtensionResultItem(
icon=None, name='No available items'))
for i in available_profiles:
logger.info(i)
items.append(ExtensionResultItem(icon='images/icon.png',
name='%s' % i['name'],
description='Generate token and copy to clipboard',
on_enter=ExtensionCustomAction(i['name'], keep_app_open=True)))
return RenderResultListAction(items)
if __name__ == '__main__':
TwoFactorAuthenticatorExtension().run()