diff options
author | Aaron Ball <nullspoon@oper.io> | 2017-07-11 08:45:17 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2017-07-11 08:45:17 -0600 |
commit | d37002a3c0b8eb9e2adb996f25a3b52e9adb8723 (patch) | |
tree | 039a711ebab9c6c479a95365070eab23baa72312 /pt | |
parent | b0efff2df83158118b1a86598b02d13dd4441e67 (diff) | |
download | bin-d37002a3c0b8eb9e2adb996f25a3b52e9adb8723.tar.gz bin-d37002a3c0b8eb9e2adb996f25a3b52e9adb8723.tar.xz |
pt:Added custom delimiter and submission support
Now pt supports a custom field delimiter. Most UIs that have a username
and password field, changing to from username to password is done by
pressing the Tab key. Some UIs however, switch with other keys (like
Return). Now a pass entry can specifiy 'delim: ...', which will be used
when the username/passwords are typed.
Also added support for automatic submission. Previously we just typed
username, delimiter, password, and let the user press enter for maximum
choice. Now we allow the user, on a per-password basis, to specify
whether they want submit to be pressed or not. This adds support for the
'submit: ...' field.
Also abstracted field value retrieval out of the main function (and long
oneliner commands), into the new getfield function.
Diffstat (limited to 'pt')
-rwxr-xr-x | pt | 51 |
1 files changed, 46 insertions, 5 deletions
@@ -60,23 +60,64 @@ function check_env { } +# +# Gets a field's value from the specified [multiline] string, using the +# specified delimiter. +# +# @param content Content to extract field value from +# @param delim Delimiter seperating fields and values +# @param field Field name to get value for +# +getfield() { + local _content="${1:-}" + local _delim="${2:-}" + local _field="${3:-}" + + printf "${_content}" \ + | sed -n "s/${_field}${_delim} *\(.*\)/\1/p" \ + | head -n 1 +} + + function main { + local _passentry # Contents of the requested pass entry + local _passpassword # Password from the pass entry + local _passusername # Username field from the pass entry + local _passdelim # UI Field delimiter (Tab, Return), from the pass entry + local _passsubmit # Boolean submit (1,y,yes...), from the pass entry + + local _delim=Tab # Default delimiter if not specified in pass entry + local _submit=0 # Default submit value if not specified in pass entry + # Exit failure if no password was specified [[ -z ${1} ]] && log "Please specify a password to be typed" && exit 1 check_env || exit 0 # Check if the password exists in the store - pass ${@} &> /dev/null + # Also, copy the contents into a variable if it exists + _passentry=$(pass ${@} 2>/dev/null) [[ $? -gt 0 ]] && log "Error: '${@}' is not in the password store" && exit 1 # Parse pass output into appropriate variables - local passfile="$(pass ${@})" - local username=$(echo -e "${passfile}" | sed -n 's/username: \(.*\)/\1/p' | head -n 1) - local pass=$(echo -e "${passfile}" | head -n 1) + _passpassword=$(printf "${_passentry}" | head -n1) + _passusername=$(getfield "${_passentry}" ':' username) + _passdelim=$(getfield "${_passentry}" ':' delim) + _passsubmit=$(getfield "${_passentry}" ':' submit) + + # If any of the known 'submit' values are specified, set submit to 1 + if [ "${_passsubmit}" = '1' ] \ + || [ "${_passsubmit}" = 'y' ] \ + || [ "${_passsubmit}" = 'yes' ]; then + _submit=1 + fi # Type the password - xdotool type --delay 15 "${username} ${pass}" + xdotool type --delay 15 "${_passusername}" + xdotool key "${_delim}" + xdotool type --delay 15 "${_passpassword}" + + [ "${_submit}" = 1 ] && xdotool key Return } main ${@} |