-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrofi_pass.py
executable file
·252 lines (196 loc) · 6.46 KB
/
rofi_pass.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
251
252
#!/usr/bin/env python3
"""Display a rofi menu for pass accounts."""
import glob
import os
import re
import shlex
import sys
from enum import IntEnum
from os.path import basename, expanduser, join, splitext
from subprocess import CalledProcessError, run
class Command(IntEnum):
CopyPassword = 0
CopyLogin = 10
CopyOTP = 11
CopyURL = 12
OpenURL = 13
ViewData = 14
EditAccount = 15
CLIPBOARD_TIMEOUT = 45
TERMINAL = os.environ.get("TERMINAL", "rofi-sensible-terminal")
ENCODING = "UTF-8"
FIELD_RX = re.compile(r"^(?P<name>\S.*?)\s*[:=]\s*(?P<value>.*)$")
KEYBINDINGS = {
Command.CopyPassword: ("Copy password", "Return"),
Command.CopyLogin: ("Copy login", "Ctrl-Return"),
Command.CopyOTP: ("Copy OTP", "Ctrl-o"),
Command.CopyURL: ("Copy URL", "Ctrl-u"),
Command.OpenURL: ("Open URL", "Ctrl-U"),
Command.ViewData: ("View data", "Shift-Return"),
Command.EditAccount: ("Edit account", "Control-Shift-Return"),
}
LOGIN_NAME_FIELDS = ("login", "username", "user", "id", "email", "emailaddress", "account")
ROFI_SELECT_CMD = [
"rofi",
"-dmenu",
"-no-custom",
"-kb-accept-alt",
"",
"-kb-accept-custom",
"",
"-kb-accept-custom-alt",
"",
"-kb-remove-to-sol",
"",
"-i",
"-p",
"Account",
"-normal-window",
]
ROFI_VIEW_CMD = [
"rofi",
"-dmenu",
"-no-custom",
"-kb-accept-custom",
"",
"-kb-accept-alt",
"",
"-kb-custom-1",
"Shift-Return",
"-kb-custom-2",
"Control-Return",
"-i",
"-mesg",
"ESC: Back, Return: Copy field value, Shift-Return: Copy & back; Control-Return: Copy & close",
"-p",
"Field",
"-normal-window",
]
RUN_IN_TERMINAL_CMD = ["{terminal}", "-e", "{command_string}", "-T", "{title}"]
URL_FIELDS = ("url", "homepage", "website")
XCLIP_CMD = ["xclip", "-i", "-selection", "clipboard"]
def copy_password(store, account, mode="show"):
env = os.environ.copy()
env["PASSWORD_STORE_DIR"] = store
env["PASSWORD_STORE_CLIP_TIME"] = str(CLIPBOARD_TIMEOUT)
proc = run(["pass", mode, "-c", account], env=env)
if proc.returncode == 0:
show_notification(
"Secret copied",
f"Secret for '{account}' copied to clipboard.\n "
f"Will be cleared in {CLIPBOARD_TIMEOUT} seconds.",
)
def copy_fieldvalue(store, account, *fieldnames):
value = get_fieldvalue(store, account, *fieldnames)
if value:
run(XCLIP_CMD, input=value, encoding=ENCODING)
else:
show_notification(
"Data not available",
f"No value set for {', '.join(fieldnames)} in '{account}' data.",
icon="warning",
)
def edit_account(store, account):
env = os.environ.copy()
env["PASSWORD_STORE_DIR"] = store
run_in_terminal(["pass", "edit", account], title=f"Edit account: {account}", env=env)
def get_pass_accounts(store):
return sorted(
splitext(fn)[0] for fn in glob.glob(join("**", "*.gpg"), root_dir=store, recursive=True)
)
def get_account_data(store, account):
env = os.environ.copy()
env["PASSWORD_STORE_DIR"] = store
try:
proc = run(
["pass", "show", account], capture_output=True, encoding=ENCODING, check=True, env=env
)
except CalledProcessError:
return {}
data = {}
for line in proc.stdout.splitlines()[1:]:
match = FIELD_RX.match(line)
if match:
name, value = match.group("name", "value")
data[name.lower()] = value
return data
def get_fieldvalue(store, account, *fieldnames):
data = get_account_data(store, account)
for name in fieldnames:
value = data.get(re.sub(r"[-_]", "", name.lower()))
if value:
return value
return None
def open_url(store, account, *fieldnames):
url = get_fieldvalue(store, account, *fieldnames)
if url:
run(["xdg-open", url])
else:
show_notification("No URL found", f"No URL set for '{account}'.", icon="error")
def run_in_terminal(cmd, title="", env=None):
subst = {
"command": cmd,
"command_string": shlex.join(cmd),
"title": title,
"terminal": TERMINAL,
}
term_cmd = [item.format(**subst) for item in RUN_IN_TERMINAL_CMD]
run(term_cmd, env=env)
def select_account(store, account=None):
accounts = get_pass_accounts(store)
rofi_cmd = ROFI_SELECT_CMD[:]
messages = []
if account:
rofi_cmd.append("-select")
rofi_cmd.append(account)
for cmd in Command:
try:
desc, shortcut = KEYBINDINGS[cmd]
except (ValueError, KeyError):
continue
if cmd != Command.CopyPassword:
rofi_cmd.append("-kb-custom-{}".format(cmd.value - 9))
rofi_cmd.append(shortcut)
messages.append(f"{shortcut}: {desc}")
if messages:
rofi_cmd.append("-mesg")
rofi_cmd.append(", ".join(messages))
proc = run(rofi_cmd, input="\n".join(accounts), capture_output=True, encoding=ENCODING)
account = proc.stdout.strip()
actions = {
Command.CopyPassword: (copy_password,),
Command.CopyLogin: (copy_fieldvalue, *LOGIN_NAME_FIELDS),
Command.CopyOTP: (copy_password, "otp"),
Command.CopyURL: (copy_fieldvalue, *URL_FIELDS),
Command.OpenURL: (open_url, *URL_FIELDS),
Command.ViewData: (view_data,),
Command.EditAccount: (edit_account,),
}
action = actions.get(proc.returncode)
if action:
return action[0](store, account, *action[1:])
def show_notification(summary, body="", icon="info", timeout=10_000):
run(["notify-send", "-a", "pass", "-t", str(timeout), "-i", icon, summary, body])
def view_data(store, account):
data = get_account_data(store, account)
lines = "\n".join((f"{key}: {value}" for key, value in data.items()))
while True:
proc = run(ROFI_VIEW_CMD, input=lines, capture_output=True, encoding=ENCODING)
if proc.returncode in (0, 10, 11):
match = FIELD_RX.match(proc.stdout.strip())
if match:
value = match.group("value")
run(XCLIP_CMD, input=value, encoding=ENCODING)
if proc.returncode != 0:
break
if proc.returncode == 10:
return account
def main():
store = os.getenv("PASSWORD_STORE_DIR", expanduser("~/.password-store"))
cont = None
while True:
cont = select_account(store, cont)
if not cont:
break
if __name__ == "__main__":
sys.exit(main() or 0)