Skip to content

Commit dddd38a

Browse files
committed
Support passage as backend
1 parent a364d2a commit dddd38a

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

otp.bash

+33-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ VERSION="1.1.2"
2020
OATH=$(which oathtool)
2121
OTPTOOL=$(which otptool)
2222

23+
if [[ $PASSAGE == 1 ]]; then
24+
EXT="age"
25+
else
26+
EXT="gpg"
27+
fi
28+
2329
## source: https://gist.github.com/cdown/1163649
2430
urlencode() {
2531
local l=${#1}
@@ -137,9 +143,13 @@ otp_insert() {
137143
set_git "$passfile"
138144

139145
mkdir -p -v "$PREFIX/$(dirname "$path")"
140-
set_gpg_recipients "$(dirname "$path")"
141-
142-
echo "$contents" | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" || die "OTP secret encryption aborted."
146+
if [[ $PASSAGE == 1 ]]; then
147+
set_age_recipients "$(dirname "$path")"
148+
echo "$contents" | $AGE -e "${AGE_RECIPIENT_ARGS[@]}" -o "$passfile" || die "OTP secret encryption aborted"
149+
else
150+
set_gpg_recipients "$(dirname "$path")"
151+
echo "$contents" | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" || die "OTP secret encryption aborted."
152+
fi
143153

144154
if [[ "$quiet" -eq 1 ]]; then
145155
git_add_file "$passfile" "$message" 1>/dev/null
@@ -243,7 +253,7 @@ cmd_otp_insert() {
243253
yesno "Insert into $path?"
244254
fi
245255

246-
local passfile="$PREFIX/$path.gpg"
256+
local passfile="$PREFIX/$path.$EXT"
247257
[[ $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"
248258

249259
otp_insert "$path" "$passfile" "$otp_uri" "Add OTP secret for $path to store."
@@ -268,16 +278,21 @@ cmd_otp_append() {
268278
local uri
269279
local path="${1%/}"
270280
local prompt="$path"
271-
local passfile="$PREFIX/$path.gpg"
281+
local passfile="$PREFIX/$path.$EXT"
272282

273283
[[ -f $passfile ]] || die "Passfile not found"
284+
if [[ $PASSAGE == 1 ]]; then
285+
old_contents=$($AGE -d -i "$IDENTITIES_FILE" "$passfile")
286+
else
287+
old_contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile")
288+
fi
274289

275290
local existing contents=""
276291
while IFS= read -r line || [ -n "$line" ]; do
277292
[[ -z "$existing" && "$line" == otpauth://* ]] && existing="$line"
278293
[[ -n "$contents" ]] && contents+=$'\n'
279294
contents+="$line"
280-
done < <($GPG -d "${GPG_OPTS[@]}" "$passfile")
295+
done < <(echo "$old_contents")
281296

282297
[[ -n "$existing" ]] && yesno "An OTP secret already exists for $path. Overwrite it?"
283298

@@ -329,11 +344,15 @@ cmd_otp_code() {
329344
[[ $err -ne 0 || $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c] [--quiet,-q] pass-name"
330345

331346
local path="${1%/}"
332-
local passfile="$PREFIX/$path.gpg"
347+
local passfile="$PREFIX/$path.$EXT"
333348
check_sneaky_paths "$path"
334349
[[ ! -f $passfile ]] && die "$path: passfile not found."
335350

336-
contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile")
351+
if [[ $PASSAGE == 1 ]]; then
352+
contents=$($AGE -d -i "$IDENTITIES_FILE" "$passfile")
353+
else
354+
contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile")
355+
fi
337356
while read -r line; do
338357
if [[ "$line" == otpauth://* ]]; then
339358
local uri="$line"
@@ -401,11 +420,15 @@ cmd_otp_uri() {
401420
[[ $err -ne 0 || $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND uri [--clip,-c | --qrcode,-q] pass-name"
402421

403422
local path="$1"
404-
local passfile="$PREFIX/$path.gpg"
423+
local passfile="$PREFIX/$path.$EXT"
405424
check_sneaky_paths "$path"
406425
[[ ! -f $passfile ]] && die "Passfile not found"
426+
if [[ $PASSAGE == 1 ]]; then
427+
contents=$($AGE -d -i "$IDENTITIES_FILE" "$passfile")
428+
else
429+
contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile")
430+
fi
407431

408-
contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile")
409432
while read -r line; do
410433
if [[ "$line" == otpauth://* ]]; then
411434
otp_parse_uri "$line"

0 commit comments

Comments
 (0)