From 01290a5a1c9230c48b9d5ca225f9a8386584f77a Mon Sep 17 00:00:00 2001 From: Nicholas Johnson Date: Fri, 16 Dec 2022 00:00:00 +0000 Subject: Add passage support --- otp.bash | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/otp.bash b/otp.bash index a0688d2..ca7b841 100755 --- a/otp.bash +++ b/otp.bash @@ -20,6 +20,9 @@ VERSION="1.1.2" OATH=$(which oathtool) OTPTOOL=$(which otptool) +[[ "$PROGRAM" == "passage" ]] && PASSAGE=true +[[ $PASSAGE ]] && GPG=age && ext=age || ext=gpg + ## source: https://gist.github.com/cdown/1163649 urlencode() { local l=${#1} @@ -137,9 +140,10 @@ otp_insert() { set_git "$passfile" mkdir -p -v "$PREFIX/$(dirname "$path")" - set_gpg_recipients "$(dirname "$path")" + [[ $PASSAGE ]] || set_gpg_recipients "$(dirname "$path")" - echo "$contents" | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" || die "OTP secret encryption aborted." + [[ $PASSAGE ]] && local age_opts=("-i" "$PASSAGE_IDENTITIES_FILE") + echo "$contents" | $GPG "${age_opts[@]}" -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 @@ -243,7 +247,7 @@ cmd_otp_insert() { yesno "Insert into $path?" fi - local passfile="$PREFIX/$path.gpg" + local passfile="$PREFIX/$path.$ext" [[ $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?" otp_insert "$path" "$passfile" "$otp_uri" "Add OTP secret for $path to store." @@ -268,16 +272,17 @@ cmd_otp_append() { local uri local path="${1%/}" local prompt="$path" - local passfile="$PREFIX/$path.gpg" + local passfile="$PREFIX/$path.$ext" [[ -f $passfile ]] || die "Passfile not found" + [[ $PASSAGE ]] && local age_opts=("-i" "$PASSAGE_IDENTITIES_FILE") 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 < <($GPG "${age_opts[@]}" -d "${GPG_OPTS[@]}" "$passfile") [[ -n "$existing" ]] && yesno "An OTP secret already exists for $path. Overwrite it?" @@ -329,11 +334,12 @@ 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" + local passfile="$PREFIX/$path.$ext" check_sneaky_paths "$path" [[ ! -f $passfile ]] && die "$path: passfile not found." - contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile") + [[ $PASSAGE ]] && local age_opts=("-i" "$PASSAGE_IDENTITIES_FILE") + contents=$($GPG "${age_opts[@]}" -d "${GPG_OPTS[@]}" "$passfile") while read -r line; do if [[ "$line" == otpauth://* ]]; then local uri="$line" @@ -401,11 +407,12 @@ 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" + local passfile="$PREFIX/$path.$ext" check_sneaky_paths "$path" [[ ! -f $passfile ]] && die "Passfile not found" - contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile") + [[ $PASSAGE ]] && local age_opts=("-i" "$PASSAGE_IDENTITIES_FILE") + contents=$($GPG "${age_opts[@]}" -d "${GPG_OPTS[@]}" "$passfile") while read -r line; do if [[ "$line" == otpauth://* ]]; then otp_parse_uri "$line" -- cgit v1.2.3