From 5a1528e3c7be5ac4c55381aa244fed877331bc4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Wed, 28 Dec 2022 20:27:53 +0100 Subject: [PATCH] Support passage as backend --- otp.bash | 60 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/otp.bash b/otp.bash index a0688d2..59c4e34 100755 --- a/otp.bash +++ b/otp.bash @@ -133,18 +133,22 @@ otp_read_secret() { otp_insert() { local path="$1" passfile="$2" contents="$3" message="$4" quiet="$5" - check_sneaky_paths "$path" - set_git "$passfile" + if [[ $PASSAGE == 1 ]]; then + echo "$contents" | $PROGRAM insert -m $path + else + check_sneaky_paths "$path" + set_git "$passfile" - mkdir -p -v "$PREFIX/$(dirname "$path")" - set_gpg_recipients "$(dirname "$path")" + mkdir -p -v "$PREFIX/$(dirname "$path")" + set_gpg_recipients "$(dirname "$path")" - echo "$contents" | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" || die "OTP secret encryption aborted." + echo "$contents" | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" || die "OTP secret encryption aborted." - if [[ "$quiet" -eq 1 ]]; then - git_add_file "$passfile" "$message" 1>/dev/null - else - 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 fi } @@ -269,15 +273,19 @@ cmd_otp_append() { local path="${1%/}" local prompt="$path" local passfile="$PREFIX/$path.gpg" - - [[ -f $passfile ]] || die "Passfile not found" + if [[ $PASSAGE == 1 ]]; then + old_contents=$($PROGRAM show $1) + else + [[ -f $passfile ]] || die "Passfile not found" + old_contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile") + fi local existing contents="" while IFS= read -r line || [ -n "$line" ]; do [[ -z "$existing" && "$line" == otpauth://* ]] && existing="$line" [[ -n "$contents" ]] && contents+=$'\n' contents+="$line" - done < <($GPG -d "${GPG_OPTS[@]}" "$passfile") + done < <(echo "$old_contents") [[ -n "$existing" ]] && yesno "An OTP secret already exists for $path. Overwrite it?" @@ -328,12 +336,16 @@ cmd_otp_code() { [[ $err -ne 0 || $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c] [--quiet,-q] pass-name" - local path="${1%/}" - local passfile="$PREFIX/$path.gpg" - check_sneaky_paths "$path" - [[ ! -f $passfile ]] && die "$path: passfile not found." + if [[ $PASSAGE == 1 ]]; then + contents=$($PROGRAM show $1) + else + local path="${1%/}" + check_sneaky_paths "$path" + local passfile="$PREFIX/$path.gpg" + [[ ! -f $passfile ]] && die "$path: passfile not found." - contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile") + contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile") + fi while read -r line; do if [[ "$line" == otpauth://* ]]; then local uri="$line" @@ -400,12 +412,16 @@ cmd_otp_uri() { [[ $err -ne 0 || $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND uri [--clip,-c | --qrcode,-q] pass-name" - local path="$1" - local passfile="$PREFIX/$path.gpg" - check_sneaky_paths "$path" - [[ ! -f $passfile ]] && die "Passfile not found" + if [[ $PASSAGE == 1 ]]; then + contents=$($PROGRAM show $1) + else + local path="$1" + local passfile="$PREFIX/$path.gpg" + check_sneaky_paths "$path" + [[ ! -f $passfile ]] && die "Passfile not found" + contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile") + fi - contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile") while read -r line; do if [[ "$line" == otpauth://* ]]; then otp_parse_uri "$line"