Skip to content

Instantly share code, notes, and snippets.

@michalrus
Created October 1, 2015 16:55
Show Gist options
  • Save michalrus/a97d780d99c2cd1669b5 to your computer and use it in GitHub Desktop.
Save michalrus/a97d780d99c2cd1669b5 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -o noclobber
set -o errexit
set -o nounset
pwstore="$HOME/.password-store/"
command -v xdotool >/dev/null || { echo >&1 'xdotool: command not found' ; exit 1 ; }
command -v pass >/dev/null || { echo >&1 'pass: command not found' ; exit 1 ; }
command -v oathtool >/dev/null || { echo >&1 'oathtool: command not found' ; exit 1 ; }
lockfile="$(readlink -f "$0").lock"
( set -o noclobber ; echo "$$" >"$lockfile" ) 2>/dev/null || { echo >&1 "$lockfile: already exists; another instance of $0 is running" ; exit 1 ; }
trap 'e=$? ; rm -f "$lockfile" ; test $e -eq 77 && exit 0 || exit $e' INT TERM EXIT
window="$(xdotool getactivewindow)"
window_title="$(xdotool getwindowname "$window")"
{ cd "$pwstore" ; find . -name '*.autotype' | cut -c 3- ; } | while IFS= read -r patterns ; do
if [ -f "$pwstore/$patterns.otp" ] ; then
if echo "$window_title" | grep -Ff "$pwstore/$patterns.otp" >/dev/null ; then
pwname="$(echo "$patterns" | sed -re 's/\.autotype$//')"
pw="$(pass show "$pwname")"
pwotpkey="$(echo "$pw" | grep -Ei '^OTP:' | head -n 1 | sed -re 's/^[^:]*:\s*(.*)$/\1/')"
pwotp="$(oathtool --totp -b "$pwotpkey")"
xdotool type --window "$window" "$pwotp"
xdotool key --window "$window" "Return"
exit 77
fi
fi
if echo "$window_title" | grep -Ff "$pwstore/$patterns" >/dev/null ; then
pwname="$(echo "$patterns" | sed -re 's/\.autotype$//')"
pw="$(pass show "$pwname")"
pwpass="$(echo "$pw" | head -n 1)"
pwuser="$(echo "$pw" | grep -Ei '^User(-?name)?:' | head -n 1 | sed -re 's/^[^:]*:\s*(.*)$/\1/')"
if [ -n "$pwuser" ] ; then
xdotool type --window "$window" "$pwuser"
xdotool key --window "$window" "Tab"
fi
xdotool type --window "$window" "$pwpass"
xdotool key --window "$window" "Return"
exit 77
fi
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment