[pass] Patch: ability to type out password automatically using xdotool

Raphaël raphael.droz at gmail.com
Fri Feb 19 14:04:30 CET 2016


Hi,

I did something similar using xdotool in my custom version of passmenu:

Workflow is:
- In your browser copy the domain name part (ctrl-l ctrl-c)
- run passmenu --type (keybinding of your choice, eg: ctrl-shift-p)
- 1) if a pass file named after the domain is found,
     it's offered for selection
     and "login<TAB>password" is expected inside
- or 2) if another file is selected, then a line matching "domain<TAB>login<TAB>password"
  is expected (grep for "domain")
- credentials information is stored (in a variable)
- a Xmessage popup asks to focus the login field in the navigator window
- one second after pressing enter, xdotool is used to run "ctrl-V TAB ctrl-V"
- clipboard isn't apart from domain name

I use it daily and it works well but the only annoying issue is
related to a xdotools bug.
I can't get it to automatically store the current URL into the clipboard,
ie: "ctrl-L ctrl-C", thus the extra need to manually copying this into the
clipboard.

It may not please all who think domain and login should be kept "clear"
and only use pass/passmenu to paste password (and doing the domain/login
typing manually or by other means)

But in my case having the possibility to store multiple domain/login
couple per files and having generally only one login per domain makes
this version useful.


-- 
GPG id: 0xF41572CEBD4218F4
-------------- next part --------------
#!/bin/bash

# Copyright (C) 2016 Rapha?l . Droz + floss @ gmail DOT com
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# passmenu: search a domain name stored inside the clipboard in the password store
# and paste the corresponding credentials
# Expanded from the dmenu-based `passmenu` script provided with zx2c4's pass utility

shopt -s nullglob globstar

let typeit=0
bygrep=yes
passfile=


if [[ $1 == "--type" ]]; then
	typeit=1
	shift
fi

# preselection: find a file matching the domain
sites=$(xclip -o -selection clipboard|sed -e 's;^.*://;;' -e 's;/.*$;;'|sed 's/^www.//')
if [[ $sites =~ .+\..+ ]]; then
    added=( $(tree -niCfl --noreport -P "$sites*" --prune --matchdirs --ignore-case ~/.password-store | grep -F "$sites" | sed -n '/gpg/{s/\.gpg$//;s/ -> .*//;p}'|sed "s;$HOME/.password-store/;;") )
    if (( ${#added[*]} > 0 )); then
        passfile=$(printf '%s\n' "${added[@]}"|dmenu)
	bygrep=no
    fi
fi

# if not found above, or refused
. /usr/share/bash-completion/completions/pass
export cur=
while [[ -z "$passfile" ]]; do
    COMPREPLY=();
    _pass_complete_entries

    if [[ $cur == _auto ]] || ( (( ${#COMPREPLY[*]} == 1 )) && [[ $cur == ${COMPREPLY[0]} ]] ); then
	passfile="$cur"
	export -n cur
	break;
    elif (( ${#COMPREPLY[*]} == 1 )); then
	cur=${COMPREPLY[0]}
	continue;
    fi

    [[ -z "$cur" ]] && COMPREPLY+=(_auto)

    cur=$(printf '%s\n' "${COMPREPLY[@]}"|dmenu)
    [[ -z "$cur" ]] && exit 1
done

[[ -z $passfile ]] && exit

if [[ $typeit -eq 0 ]]; then
    pass show -c "$passfile" 2>/dev/null

elif [[ $passfile == _auto ]]; then
    : # later
elif [[ $(lsb_release -si) == Debian ]]; then
    ## copy website URL
    # wmctrl -xa Navigator
    ## **BUG HERE**: adding ctrl+c currently does not work
    # xdotool search --classname Navigator windowactivate --sync key --delay 250 --clearmodifiers ctrl+F6

    ## trim domain
    site=$(xclip -o -selection clipboard|sed -e 's;^.*://;;' -e 's;/.*$;;'|sed 's/^www.//')
    gxmessage "inserting pass for ${site^^}?" -default "Ok" -buttons "Ok:1,Cancel:2"
    (( $? == 2 )) && exit 0

    if [[ $bygrep == yes ]]; then
	read s l p rest < <(pass show "$passfile"|grep -iFm1 "$site")
    else
	read l p rest < <(pass show "$passfile")
	if [[ -z "$p" ]]; then
	    mapfile x < <(pass show "$passfile"|egrep -im1 'username|password'|cut -d' ' -f2-|tr "\n" " ");
	    read l p rest <<<"${a[*]}"
	fi
    fi

    rest= x=
    [[ -z "$p" ]] && notify-send "pass for $site not found" && s= l= p= && exit 1
    gxmessage "focus now" -default "Ok" -buttons "Ok:1"
    sleep 1
    # xdotool search --classname Navigator windowactivate --sync type "$l"
    xdotool type "$l"
    s= l=
    xdotool key Tab type "$p"
    p=
    exit

elif [[ $(lsb_release -si) == Gentoo ]]; then
    :
    # xdotool search --classname Navigator windowactivate --sync type --clearmodifiers -- $(pass show "$passfile" | head -n 1)"
fi


More information about the Password-Store mailing list