commit 8863078b866b78fb964c0f04629bc404e26c46a4
parent 8486079c4060d259eefd23b01342b6b0b42bf149
Author: Wim Dupont <wim@wimdupont.com>
Date: Sat, 13 Aug 2022 21:10:28 +0200
pass for autotype and no console pinentry
Diffstat:
M | bin/otp | | | 35 | +++++++++++++++++++---------------- |
M | bin/pwu | | | 169 | +++++++++++++++++++++++++++++++++++++++++++++++++------------------------------ |
2 files changed, 125 insertions(+), 79 deletions(-)
diff --git a/bin/otp b/bin/otp
@@ -3,14 +3,17 @@
shopt -s nullglob globstar
font=$DMENU_FONT
-value=$1
typeit=0
-if [[ $1 == "--type" ]]; then
- typeit=1
- value=
- shift
-fi
+while getopts ":t" arg; do
+ case $arg in
+ t) typeit=1
+ esac
+done
+
+shift $((OPTIND-1))
+
+value=$1
if [[ -n $WAYLAND_DISPLAY ]]; then
dmenu=dmenu-wl
@@ -29,7 +32,12 @@ password_files=( "${password_files[@]#"$prefix"/}" )
password_files=( "${password_files[@]%.gpg}" )
function use_arg_file () {
- pass otp -c "$1" 2>/dev/null
+ password=$1
+ if [[ $typeit -eq 0 ]]; then
+ pass otp -c "$password" 2>/dev/null
+ else
+ pass otp "$password" | { IFS= read -r pass; printf %s "$pass"; } | $xdotool
+ fi
exit;
}
@@ -54,14 +62,9 @@ if [ -n "${value}" ]; then
echo "Found more than one possible otp file for \"${value}\"."
fi
fi
-fi
-
-password=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$font" -i "$@")
-
-[[ -n $password ]] || exit
-
-if [[ $typeit -eq 0 ]]; then
- pass otp -c "$password" 2>/dev/null
else
- pass otp "$password" | { IFS= read -r pass; printf %s "$pass"; } | $xdotool
+ password=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$font" -i )
+ [[ -n $password ]] || exit
+
+ use_arg_file "${password}"
fi
diff --git a/bin/pwu b/bin/pwu
@@ -4,14 +4,19 @@
shopt -s nullglob globstar
font=$DMENU_FONT
-value=$1
typeit=0
+no_console=0
-if [[ $1 == "--type" ]]; then
- typeit=1
- value=
- shift
-fi
+while getopts ":tc" arg; do
+ case $arg in
+ t) typeit=1;;
+ c) no_console=1;;
+ esac
+done
+
+shift $((OPTIND-1))
+
+value=$1
if [[ -n $WAYLAND_DISPLAY ]]; then
dmenu=dmenu-wl
@@ -29,50 +34,117 @@ password_files=( "$prefix"/**/*.gpg )
password_files=( "${password_files[@]#"$prefix"/}" )
password_files=( "${password_files[@]%.gpg}" )
-function use_arg_file () {
+function use_file () {
+ local OPTIND
+ quiet=0
+
+ while getopts ":q" arg; do
+ case $arg in
+ q) quiet=1;;
+ esac
+ done
+ shift $((OPTIND-1))
password=$1
- pass -c "${password}"
+
+ if [[ "$password" == otp/* ]]; then
+ use_otp -f "$password"
+ fi
+
creds=$(pass show "${password}")
- echo "${creds}" | sed -n 2p | xclip
+ if [[ -z "$creds" ]]; then
+ exit 1
+ fi
+
+ clip_or_type_pass "${password}" "${creds}"
+
url=$(echo "${creds}" | sed -n 3p)
- if [[ -n "$url" ]]; then
- xdg-open "${url}" >/dev/null 2>&1 & disown
+ if [[ "$url" ]]; then
+ if [[ $quiet == 0 ]]; then
+ if [[ $typeit -eq 0 ]]; then
+ openUrl=$(printf "yes\nno" | "$dmenu" -fn "$font" -i -p "Open $url in browser?")
+ if [[ "$openUrl" == yes ]]; then
+ xdg-open "${url}" >/dev/null 2>&1 & disown
+ fi
+ fi
+ use_otp $password
+ else
+ if [[ $typeit -eq 0 ]]; then
+ xdg-open "${url}" >/dev/null 2>&1 & disown
+ fi
+ use_otp -q $password
+ fi
fi
- use_otp -q $password
- exit;
+ exit 1;
}
function use_otp () {
local OPTIND
quiet=0
+ fast=0
- while getopts "q" arg; do
+ while getopts ":qf" arg; do
case $arg in
q) quiet=1;;
+ f) fast=1;;
esac
done
shift $((OPTIND-1))
file=$1
- otp_files=( $( printf '%s\n' "${password_files[@]}" | grep 'otp/*' ) )
- for otp_file in "${otp_files[@]}"
- do
- if [[ "$otp_file" == "otp/${file}" ]] ; then
- if [[ $quiet == 0 ]]; then
- read -p "Press enter to continue"
- copyOtp=$(printf "yes\nno" | "$dmenu" -fn "$font" -i -p "Copy OTP to clipboard?")
- if [[ "$copyOtp" == yes ]]; then
- pass otp -c "$otp_file" 2>/dev/null
- fi
- else
- read -p "Press enter to copy OTP to clipboard."
- pass otp -c "$otp_file" 2>/dev/null
+ if [[ "$file" != otp/* ]]; then
+ otp_files=( $( printf '%s\n' "${password_files[@]}" | grep 'otp/*' ) )
+ for otp_file in "${otp_files[@]}"
+ do
+ if [[ "$otp_file" == "otp/${file}" ]] ; then
+ file="${otp_file}"
+ break;
+ fi
+ done
+ fi
+
+ if [[ $fast == 1 ]]; then
+ clip_or_type_otp "$file"
+ else
+ [[ $no_console == 0 ]] || exit
+ if [[ $quiet == 0 ]]; then
+ read -p "Press enter to continue"
+ copyOtp=$(printf "yes\nno" | "$dmenu" -fn "$font" -i -p "Copy OTP to clipboard?")
+ if [[ "$copyOtp" == yes ]]; then
+ clip_or_type_otp "$file"
fi
+ else
+ read -p "Press enter to copy OTP to clipboard."
+ clip_or_type_otp "$file"
fi
- done
+ fi
+
+ exit 1
}
+function clip_or_type_pass () {
+ password=$1
+ creds=$2
+ if [[ $typeit -eq 1 ]]; then
+ echo "${creds}" | sed -n 2p | { IFS= read -r pass; printf %s "$pass"; } | $xdotool
+ xdotool key Tab
+ echo "${creds}" | sed -n 1p | { IFS= read -r pass; printf %s "$pass"; } | $xdotool
+ else
+ pass -c "${password}" 2>/dev/null
+ echo "${creds}" | sed -n 2p | xclip
+ fi
+}
+
+function clip_or_type_otp () {
+ password=$1
+ if [[ $typeit -eq 0 ]]; then
+ pass otp -c "$password" 2>/dev/null
+ else
+ pass otp "$password" | { IFS= read -r pass; printf %s "$pass"; } | $xdotool
+ fi
+}
+
+
if [ -n "${value}" ]; then
shift
let i=0
@@ -81,11 +153,12 @@ if [ -n "${value}" ]; then
if [[ "$file" =~ "${value}" ]] ; then
argFile="${file}"
let i++
+ break
fi
done
if [ "$i" == 1 ]; then
- use_arg_file "${argFile}"
+ use_file -q "${argFile}"
else
if [ "$i" == 0 ]; then
echo "Found no password files for \"${value}\"."
@@ -94,39 +167,9 @@ if [ -n "${value}" ]; then
echo "Found more than one possible password file for \"${value}\"."
fi
fi
-fi
-
-password=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$font" -i "$@")
-
-[[ -n $password ]] || exit
-
-if [[ $typeit -eq 0 ]]; then
- if [[ "$password" == otp/* ]]; then
- pass otp -c "$password"
- else
- selection=$(pass show "$password")
- if [[ -z "$selection" ]]; then
- exit
- fi
- username=$(echo "$selection" | sed -n 2p)
- echo "$username"
- echo "$username" | xclip
- pass -c "$password" 2>/dev/null
- url=$(echo "$selection" | sed -n 3p)
- if [[ "$url" ]]; then
- openUrl=$(printf "yes\nno" | "$dmenu" -fn "$font" -i "$@" -p "Open $url in browser?")
- if [[ "$openUrl" == yes ]]; then
- xdg-open "$url" >/dev/null 2>&1 & disown
- fi
- fi
- use_otp "$password"
- fi
else
- selection=$(pass show "$password")
- if [[ -z "$selection" ]]; then
- exit
- fi
- echo "$selection" | sed -n 2p | { IFS= read -r pass; printf %s "$pass"; } | $xdotool
- xdotool key Tab
- echo "$selection" | sed -n 1p | { IFS= read -r pass; printf %s "$pass"; } | $xdotool
+ value=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$font" -i )
+ [[ -n $value ]] || exit
+
+ use_file "${value}"
fi