From d29b61248c87ab29283eb4ccbd037869f0b4df28 Mon Sep 17 00:00:00 2001 From: Tad Fisher Date: Sun, 19 Mar 2017 16:49:01 -0700 Subject: cmd_otp_uri: Show first line starting with otpauth:// in passfile --- otp.bash | 27 ++++++--------------------- test/uri.t | 24 ++++++++++-------------- test/validate.t | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 35 deletions(-) create mode 100755 test/validate.t diff --git a/otp.bash b/otp.bash index dad46b8..539fc7e 100755 --- a/otp.bash +++ b/otp.bash @@ -320,32 +320,17 @@ cmd_otp_uri() { check_sneaky_paths "$path" [[ ! -f $passfile ]] && die "Passfile not found" - local secret="" type="" algorithm="" counter="" period=30 digits=6 - contents=$($GPG -d "${GPG_OPTS[@]}" "$passfile") - while read -r -a line; do case ${line[0]} in - otp_secret:) secret=${line[1]} ;; - otp_type:) type=${line[1]} ;; - otp_algorithm:) algorithm=${line[1]} ;; - otp_period:) period=${line[1]} ;; - otp_counter:) counter=${line[1]} ;; - otp_digits:) digits=${line[1]} ;; - *) true ;; - esac done <<< "$contents" - - local uri - case $type in - totp) uri="otpauth://totp/$path?secret=$secret&algorithm=$algorithm&digits=$digits&period=$period" ;; - hotp) uri="otpauth://hotp/$path?secret=$secret&digits=$digits&counter=$counter" ;; - *) die "Invalid OTP type '$type'. Must be one of 'totp' or 'hotp'" ;; - esac + while read -r -a line; do + [[ "$line" == otpauth://* ]] && otp_parse_uri "$line" + done <<< "$contents" if [[ clip -eq 1 ]]; then - clip "$uri" "OTP key URI for $path" + clip "$otp_uri" "OTP key URI for $path" elif [[ qrcode -eq 1 ]]; then - qrcode "$uri" "OTP key URI for $path" + qrcode "$otp_uri" "OTP key URI for $path" else - echo "$uri" + echo "$otp_uri" fi } diff --git a/test/uri.t b/test/uri.t index 9fbe66c..084b010 100755 --- a/test/uri.t +++ b/test/uri.t @@ -4,24 +4,20 @@ export test_description='Tests pass otp URI parsing' . ./setup.sh -test_expect_success 'Parses a basic TOTP URI' ' - "$PASS" otp validate "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" -' - -test_expect_success 'Parses a complex TOTP URI' ' - "$PASS" otp validate otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 -' +test_expect_success 'Shows key URI in single-line passfile' ' + uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" -test_expect_success 'Fails for bogus URL' ' - test_must_fail "$PASS" otp validate https://www.google.com/ + test_pass_init && + "$PASS" otp insert "$uri" passfile && + [[ $("$PASS" otp uri passfile) == "$uri" ]] ' -test_expect_success 'Fails for missing secret' ' - test_must_fail "$PASS" otp validate otpauth://totp/ACME%20Co:john.doe@email.com?issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 -' +test_expect_success 'Shows key URI in multi-line passfile' ' + uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" -test_expect_success 'Fails for missing counter' ' - test_must_fail "$PASS" otp validate otpauth://hotp?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ + test_pass_init && + "$PASS" insert -m passfile < <(echo -e "password\nfoo\n$uri\nbar") && + [[ $("$PASS" otp uri passfile) == "$uri" ]] ' test_done diff --git a/test/validate.t b/test/validate.t new file mode 100755 index 0000000..9fbe66c --- /dev/null +++ b/test/validate.t @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +export test_description='Tests pass otp URI parsing' + +. ./setup.sh + +test_expect_success 'Parses a basic TOTP URI' ' + "$PASS" otp validate "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" +' + +test_expect_success 'Parses a complex TOTP URI' ' + "$PASS" otp validate otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 +' + +test_expect_success 'Fails for bogus URL' ' + test_must_fail "$PASS" otp validate https://www.google.com/ +' + +test_expect_success 'Fails for missing secret' ' + test_must_fail "$PASS" otp validate otpauth://totp/ACME%20Co:john.doe@email.com?issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 +' + +test_expect_success 'Fails for missing counter' ' + test_must_fail "$PASS" otp validate otpauth://hotp?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ +' + +test_done -- cgit v1.2.3