aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kletzander <mkletzan@redhat.com>2019-03-22 12:33:29 +0100
committerTad Fisher <129148+tadfisher@users.noreply.github.com>2020-09-12 12:49:24 -0700
commit99419824e12ccfa082967248df356e15f456e8cd (patch)
tree495bac8387cb43ecdc1828aff502ea2f46432663
parent3b11688682435847b49ef6310eb11eab32acd8b9 (diff)
Add support for quiet output of otp code command
When using this option the output of `pass otp -q ...` is predictable and can be further used without parsing even when the password store is a repository as well. This is useful, for example, when using rofi-pass. The patch is written in a way that other commands can optionally use the quiet option for otp_insert as well.
-rwxr-xr-xotp.bash17
-rw-r--r--pass-otp.16
-rw-r--r--pass-otp.bash.completion2
-rwxr-xr-xtest/code.t11
-rw-r--r--test/setup.sh5
5 files changed, 32 insertions, 9 deletions
diff --git a/otp.bash b/otp.bash
index c62501a..5539a5b 100755
--- a/otp.bash
+++ b/otp.bash
@@ -130,7 +130,7 @@ otp_read_secret() {
}
otp_insert() {
- local path="$1" passfile="$2" contents="$3" message="$4"
+ local path="$1" passfile="$2" contents="$3" message="$4" quiet="$5"
check_sneaky_paths "$path"
set_git "$passfile"
@@ -140,7 +140,11 @@ otp_insert() {
echo "$contents" | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" || die "OTP secret encryption aborted."
- git_add_file "$passfile" "$message"
+ if [[ "$quiet" -eq 1 ]]; then
+ git_add_file "$passfile" "$message" 1>/dev/null
+ else
+ git_add_file "$passfile" "$message"
+ fi
}
cmd_otp_usage() {
@@ -311,16 +315,17 @@ cmd_otp_append() {
cmd_otp_code() {
[[ -z "$OATH" ]] && die "Failed to generate OTP code: oathtool is not installed."
- local opts clip=0
- opts="$($GETOPT -o c -l clip -n "$PROGRAM" -- "$@")"
+ local opts clip=0 quiet=0
+ opts="$($GETOPT -o cq -l clip,quiet -n "$PROGRAM" -- "$@")"
local err=$?
eval set -- "$opts"
while true; do case $1 in
-c|--clip) clip=1; shift ;;
+ -q|--quiet) quiet=1; shift ;;
--) shift; break ;;
esac done
- [[ $err -ne 0 || $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c] pass-name"
+ [[ $err -ne 0 || $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c] [--quiet,-q] pass-name"
local path="${1%/}"
local passfile="$PREFIX/$path.gpg"
@@ -368,7 +373,7 @@ cmd_otp_code() {
replaced+="$line"
done < <(echo "$contents")
- otp_insert "$path" "$passfile" "$replaced" "Increment HOTP counter for $path."
+ otp_insert "$path" "$passfile" "$replaced" "Increment HOTP counter for $path." "$quiet"
fi
if [[ $clip -ne 0 ]]; then
diff --git a/pass-otp.1 b/pass-otp.1
index bef89f7..9bded0c 100644
--- a/pass-otp.1
+++ b/pass-otp.1
@@ -28,13 +28,15 @@ If no COMMAND is specified, COMMAND defaults to \fBcode\fP.
.SH COMMANDS
.TP
-\fBotp code\fP [ \fI--clip\fP, \fI-c\fP ] \fIpass-name\fP
+\fBotp code\fP [ \fI--clip\fP, \fI-c\fP ] [ \fI--quiet\fP, \fI-q\fP ] \fIpass-name\fP
Generate and print an OTP code from the secret key stored in \fIpass-name\fP
using \fBoathtool\fP(1). If \fI--clip\fP or \fI-c\fP is specified, do not print
the code but instead copy it to the clipboard using \fBxclip\fP(1)
and then restore the clipboard after 45 (or \fIPASSWORD_STORE_CLIP_TIME\fP)
-seconds. This command is alternatively named \fBshow\fP.
+seconds. If \fI--quiet\fP or \fI-q\fP is specified, do not print
+the git update message to standard out. This command is alternatively named
+\fBshow\fP.
.TP
\fBotp insert\fP [ \fI--force\fP, \fI-f\fP ] [ \fI--echo\fP, \fI-e\fP ] \
diff --git a/pass-otp.bash.completion b/pass-otp.bash.completion
index 5b13dcc..419edbd 100644
--- a/pass-otp.bash.completion
+++ b/pass-otp.bash.completion
@@ -20,7 +20,7 @@ __password_store_extension_complete_otp() {
;;
esac
else
- COMPREPLY+=($(compgen -W "insert append uri validate -h --help -c --clip" -- ${cur}))
+ COMPREPLY+=($(compgen -W "insert append uri validate -h --help -c --clip -q --quiet" -- ${cur}))
_pass_complete_entries 1
fi
}
diff --git a/test/code.t b/test/code.t
index 39310fb..19b4754 100755
--- a/test/code.t
+++ b/test/code.t
@@ -30,6 +30,17 @@ test_expect_success 'Generates HOTP code and increments counter' '
[[ $("$PASS" otp uri passfile) == "$inc" ]]
'
+test_expect_success 'Generates HOTP code quietly' '
+ uri="otpauth://hotp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=10&issuer=Example"
+ inc="otpauth://hotp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=11&issuer=Example"
+
+ test_pass_git_init &&
+ "$PASS" otp insert passfile <<< "$uri" &&
+ code=$("$PASS" otp -q passfile) &&
+ [[ ${#code} -eq 6 ]] &&
+ [[ $("$PASS" otp uri passfile) == "$inc" ]]
+'
+
test_expect_success 'HOTP counter increments and preserves multiline contents' '
uri="otpauth://hotp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=10&issuer=Example"
inc="otpauth://hotp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=11&issuer=Example"
diff --git a/test/setup.sh b/test/setup.sh
index 5fd7173..4029530 100644
--- a/test/setup.sh
+++ b/test/setup.sh
@@ -72,3 +72,8 @@ test_pass_init() {
rm -rf "$PASSWORD_STORE_DIR"
"$PASS" init "${KEY[@]}"
}
+
+test_pass_git_init() {
+ rm -rf "$PASSWORD_STORE_DIR"
+ "$PASS" init "${KEY[@]}" && "$PASS" git init
+}