112 lines
2.8 KiB
Bash
Executable File
112 lines
2.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
_trim() {
|
|
local var="$*"
|
|
# remove leading whitespace characters
|
|
var="${var#"${var%%[![:space:]]*}"}"
|
|
# remove trailing whitespace characters
|
|
var="${var%"${var##*[![:space:]]}"}"
|
|
printf '%s' "$var"
|
|
}
|
|
|
|
_parse_fields() {
|
|
has_username=0
|
|
IFS=$'\n'
|
|
fields="$(pass show $password | tail -n +2 | cut -d: -f1 -s)"
|
|
field_list+="password\n"
|
|
for line in $fields; do
|
|
if [[ $line == "username" ]]; then
|
|
has_username=1
|
|
field_list+="$line\n"
|
|
elif [[ $line == "otpauth" ]]; then
|
|
field_list+="OTP\n"
|
|
elif [[ $line == autotype_always ]]; then
|
|
autotype=1
|
|
else
|
|
field_list+="$line\n"
|
|
fi
|
|
done
|
|
if [[ $typeit -eq 1 ]] && [[ $has_username -eq 1 ]]; then
|
|
printf "autotype\n"
|
|
fi
|
|
printf "$field_list"
|
|
unset IFS
|
|
}
|
|
|
|
_pass_field() {
|
|
IFS=$'\n'
|
|
plaintext="$(pass show $password | tail -n +2)"
|
|
for line in $plaintext; do
|
|
if [[ $line == $1:* ]]; then
|
|
printf "$(_trim "$(printf "$line" | cut -d: -f1 -s --complement)")"
|
|
fi
|
|
done
|
|
unset IFS
|
|
}
|
|
|
|
_pass_get() {
|
|
if [[ $1 == "password" ]]; then
|
|
pass show "$password" | { IFS= read -r pass; printf %s "$pass"; }
|
|
elif [[ $1 == "OTP" ]]; then
|
|
pass otp "$password" | tail -n1 | { IFS= read -r pass; printf %s "$pass"; }
|
|
else
|
|
printf "$(_pass_field $1)"
|
|
fi
|
|
}
|
|
|
|
_usage() {
|
|
printf "Usage: wofi-pass [options]\n"
|
|
printf " -a, --autotype autotype whatever entry is chosen\n"
|
|
printf " -h, --help show this help message\n"
|
|
printf " -s, --squash don't show field choice if password file only contains password\n"
|
|
printf " -t, --type type the selection instead of copying to clipboard\n"
|
|
}
|
|
|
|
shopt -s nullglob globstar
|
|
|
|
OPTS="$(getopt --options ahst --longoptions autotype,help,squash,type -n 'wofi-pass' -- "$@")"
|
|
eval set -- "$OPTS"
|
|
while true; do
|
|
case "$1" in
|
|
-a | --autotype ) autotype=1; shift ;;
|
|
-h | --help ) help=1; shift ;;
|
|
-s | --squash ) squash=1; shift ;;
|
|
-t | --type ) typeit=1; shift;;
|
|
-- ) shift; break ;;
|
|
* ) break ;;
|
|
esac
|
|
done
|
|
|
|
if [[ $help -eq 1 ]]; then
|
|
echo "$(_usage)" >&2
|
|
exit 0
|
|
fi
|
|
|
|
prefix=${PASSWORD_STORE_DIR-~/.password-store}
|
|
password_files=( "$prefix"/**/*.gpg )
|
|
password_files=( "${password_files[@]#"$prefix"/}" )
|
|
password_files=( "${password_files[@]%.gpg}" )
|
|
|
|
password=$(printf '%s\n' "${password_files[@]}" | wofi --dmenu "$@")
|
|
[[ -n $password ]] || exit
|
|
field_list="$(_parse_fields)"
|
|
field_count="$(echo "$field_list" | wc -l)"
|
|
if [[ $squash -eq 1 ]] && [[ $field_count -eq 1 ]]; then
|
|
field="password"
|
|
elif [[ $autotype -ne 1 ]]; then
|
|
field=$(printf "$field_list" | wofi --dmenu)
|
|
fi
|
|
|
|
if [[ $typeit -eq 0 ]]; then
|
|
wl-copy "$(_pass_get $field)"
|
|
else
|
|
if [[ $field == "autotype" ]] || [[ $autotype -eq 1 ]]; then
|
|
_pass_get "username" | ydotool type --file -
|
|
printf "\t" | ydotool type --file -
|
|
_pass_get "password" | ydotool type --file -
|
|
printf "\n" | ydotool type --file -
|
|
else
|
|
_pass_get $field | ydotool type --file -
|
|
fi
|
|
fi
|