This repository has been archived on 2023-07-10. You can view files and clone it, but cannot push or open issues or pull requests.
wofi-pass/wofi-pass

114 lines
2.8 KiB
Plaintext
Raw Normal View History

2020-03-27 22:55:38 -04:00
#!/usr/bin/env bash
2021-07-13 21:13:36 -04:00
TYPE_CMD="wtype -"
2020-03-27 22:55:38 -04:00
_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
2020-03-27 22:55:38 -04:00
IFS=$'\n'
fields="$(pass show $password | tail -n +2 | cut -d: -f1 -s)"
field_list+="password\n"
2020-03-27 22:55:38 -04:00
for line in $fields; do
if [[ $line == "username" ]]; then
has_username=1
field_list+="$line\n"
elif [[ $line == "otpauth" ]]; then
field_list+="OTP\n"
2020-04-08 12:25:53 -04:00
elif [[ $line == autotype_always ]]; then
autotype=1
2020-03-27 22:55:38 -04:00
else
field_list+="$line\n"
2020-03-27 22:55:38 -04:00
fi
done
2020-04-08 12:28:16 -04:00
if [[ $typeit -eq 1 ]] && [[ $has_username -eq 1 ]]; then
printf "autotype\n"
fi
printf "$field_list"
2020-03-27 22:55:38 -04:00
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)")"
2020-03-27 22:55:38 -04:00
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"
2020-04-08 12:25:53 -04:00
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
2020-04-08 12:25:53 -04:00
OPTS="$(getopt --options ahst --longoptions autotype,help,squash,type -n 'wofi-pass' -- "$@")"
eval set -- "$OPTS"
while true; do
case "$1" in
2020-04-08 12:25:53 -04:00
-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
2020-03-27 22:55:38 -04:00
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 "$@")
2020-03-27 22:55:38 -04:00
[[ -n $password ]] || exit
field_list="$(_parse_fields)"
field_count="$(echo "$field_list" | wc -l)"
2020-04-08 12:25:53 -04:00
if [[ $squash -eq 1 ]] && [[ $field_count -eq 1 ]]; then
field="password"
2020-04-08 12:25:53 -04:00
elif [[ $autotype -ne 1 ]]; then
field=$(printf "$field_list" | wofi --dmenu)
fi
2020-03-27 22:55:38 -04:00
if [[ $typeit -eq 0 ]]; then
wl-copy "$(_pass_get $field)"
else
2020-04-08 12:25:53 -04:00
if [[ $field == "autotype" ]] || [[ $autotype -eq 1 ]]; then
2021-07-13 21:13:36 -04:00
_pass_get "username" | ${TYPE_CMD}
printf "\t" | ${TYPE_CMD}
_pass_get "password" | ${TYPE_CMD}
printf "\n" | ${TYPE_CMD}
2020-03-27 22:55:38 -04:00
else
2021-07-13 21:13:36 -04:00
_pass_get $field | ${TYPE_CMD}
2020-03-27 22:55:38 -04:00
fi
fi