-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add autotyping #2
base: master
Are you sure you want to change the base?
Changes from all commits
b76543d
7ec9793
0b8c7f6
db36b19
3b247be
ed25480
0101b2a
5f8fdba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Package | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Package | ||
run: make release | ||
- name: Upload workflow artifact | ||
uses: actions/upload-artifact@v1 | ||
with: | ||
name: gopass.alfredworkflow | ||
path: gopass.alfredworkflow |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
*.iml | ||
gopass.alfredworkflow |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
release: | ||
zip -r ../gopass.alfredworkflow * | ||
rm -f ../gopass.alfredworkflow | ||
zip -r ./gopass.alfredworkflow ./* -x@alfred_package.ignore |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.idea | ||
.env | ||
.git | ||
*.iml | ||
.github |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env python | ||
#!/usr/bin/env python3 | ||
|
||
import subprocess | ||
import sys | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import json | ||
import os | ||
import subprocess | ||
import sys | ||
import time | ||
from os.path import expanduser | ||
|
||
home = expanduser("~") | ||
my_env = os.environ.copy() | ||
my_env['PATH'] = '/usr/local/bin:{}'.format(my_env['PATH']) | ||
|
||
special_autotype_handlers = { | ||
":tab": lambda: do_stroke(48), | ||
":space": lambda: do_stroke(49), | ||
":enter": lambda: do_stroke(36), | ||
":delete": lambda: do_stroke(51), | ||
":delay": lambda: time.sleep(1), | ||
":clearField": lambda: clear_field_content() | ||
} | ||
|
||
|
||
def get_additional_autotype_handlers_filename(): | ||
base_path = os.getenv("XDG_CONFIG", home) | ||
return '{base_path}/{filename}'.format(base_path=base_path, filename=".gopass_autotype_handlers.json") | ||
|
||
|
||
def load_additional_autotype_handlers_config(): | ||
handlers_path = get_additional_autotype_handlers_filename() | ||
if os.path.exists(handlers_path): | ||
with open(handlers_path) as f: | ||
return json.load(f) | ||
else: | ||
return {} | ||
|
||
|
||
def execute_additional_autotype_handler(command): | ||
command_result = os.popen(command).read() | ||
do_type(command_result) | ||
|
||
|
||
def load_additional_autotype_handlers(): | ||
config = load_additional_autotype_handlers_config() | ||
return dict(map(lambda item: (item[0], lambda: execute_additional_autotype_handler(item[1])), config.items())) | ||
|
||
|
||
def all_autotype_handlers(): | ||
additional = load_additional_autotype_handlers() | ||
return {**additional, **special_autotype_handlers} | ||
|
||
|
||
def do_type(to_type): | ||
os.system(f"echo 'tell application \"System Events\" to keystroke \"{to_type}\"' | osascript") | ||
|
||
|
||
def do_stroke(stroke): | ||
os.system(f"echo 'tell application \"System Events\" to key code \"{stroke}\"' | osascript") | ||
|
||
|
||
def clear_field_content(): | ||
os.system("""echo 'tell application "System Events" to keystroke "a" using command down' | osascript""") | ||
do_stroke(51) | ||
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced how this is actually clearing a field? Isn't it just adding 51 times a char There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well this is a bit special - how do you clear a field only from the command line? What I came up with is just issue a Ctrl+a and issue a backspace. |
||
|
||
|
||
def get_gopass_data_for(query): | ||
return subprocess.run(["gopass", "show", "-f", query], stdout=subprocess.PIPE, env=my_env).stdout.decode("utf-8") | ||
|
||
|
||
def parse_gopass_data(data): | ||
lines = data.split("\n") | ||
password = lines[0] | ||
non_empty_lines = list(filter(lambda line: len(line) > 0, lines)) | ||
lines_splitted = map(lambda line: line.split(": "), non_empty_lines[1:]) | ||
lines_with_two_items = filter(lambda elements: len(elements) == 2, lines_splitted) | ||
items_dict = {item[0]: item[1] for item in lines_with_two_items} | ||
items_dict["pass"] = password | ||
return items_dict | ||
|
||
|
||
def autotype(items, field): | ||
autotype = items.get(field) | ||
handlers = all_autotype_handlers() | ||
if autotype is None: | ||
return | ||
to_type = autotype.split(" ") | ||
for word in to_type: | ||
handler = handlers.get(word) | ||
if handler is None: | ||
mapped = items.get(word) | ||
do_type(word if mapped is None else mapped) | ||
else: | ||
handler() | ||
|
||
|
||
def autotype_list(path, parsed): | ||
return [ | ||
{ | ||
"uid": result, | ||
"title": result, | ||
"subtitle": path, | ||
"arg": f"{path} {result}", | ||
"autocomplete": result | ||
} for result in parsed.keys() | ||
] | ||
|
||
|
||
action = sys.argv[1] | ||
path = sys.argv[2].split(" ") | ||
|
||
data = get_gopass_data_for(path[0]) | ||
parsed = parse_gopass_data(data) | ||
autotype_action = "autotype" if len(path) == 1 else path[1] | ||
|
||
if action == 'list': | ||
print(json.dumps({'items': autotype_list(path[0], parsed)})) | ||
elif action == 'autotype': | ||
autotype(parsed, autotype_action) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why we need this?
What's supposed to be in the
.gopass_autotype_handlers.json
file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a bit of extra documentation. In the end it allows to include output from external commands in the autotype command. I explicitly use it to type YubiKey TOTP tokens in authentication forms, so I don't have to do it manually. It allows to fill forms completely, without any manual interaction (as long as you can code any part as bash commands)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general there is an updated documentation for the autotyping, also including the special commands and the additional handlers. Maybe that makes it more clear on how it works.