aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTad Fisher <tad@simple.com>2017-03-18 18:20:28 -0700
committerTad Fisher <tad@simple.com>2017-03-18 18:20:28 -0700
commit72373e86cea699c5fb69cfff436e390186cc9e4e (patch)
treeef7339e48c32376b5b4d10ed176534234943c0b5
parent01a95a45932f9a54c2c2fc3423824bdbac3918d8 (diff)
downloadpass-otp-72373e86cea699c5fb69cfff436e390186cc9e4e.tar.gz
pass-otp-72373e86cea699c5fb69cfff436e390186cc9e4e.zip
Add cmd_insert_uri; refactor tests
-rwxr-xr-xotp.bash28
-rwxr-xr-xtest/insert.t40
-rw-r--r--test/setup.sh16
3 files changed, 68 insertions, 16 deletions
diff --git a/otp.bash b/otp.bash
index 93f815e..1d12c8b 100755
--- a/otp.bash
+++ b/otp.bash
@@ -22,7 +22,7 @@ OATH=$(which oathtool)
# Vars are consumed by caller
# shellcheck disable=SC2034
otp_parse_uri() {
- local uri="$*"
+ local uri="$1"
uri="${uri//\`/%60}"
uri="${uri//\"/%22}"
@@ -75,17 +75,18 @@ otp_increment_counter() {
}
otp_insert() {
+ echo "args: $*"
+
local path="${1%/}"
local passfile="$PREFIX/$path.gpg"
local force=$2
local contents="$3"
check_sneaky_paths "$path"
+ set_git "$passfile"
[[ $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"
- set_git "$passfile"
-
mkdir -p -v "$PREFIX/$(dirname "$path")"
set_gpg_recipients "$(dirname "$path")"
@@ -94,6 +95,25 @@ otp_insert() {
git_add_file "$passfile" "Add given OTP secret for $path to store."
}
+otp_insert_uri() {
+ local opts force=0
+ opts="$($GETOPT -o f -l force -n "$PROGRAM" -- "$@")"
+ local err=$?
+ eval set -- "$opts"
+ while true; do case $1 in
+ -f|--force) force=1; shift ;;
+ --) shift; break ;;
+ esac done
+
+ [[ $err -ne 0 || $# -ne 2 ]] && die "Usage: $PROGRAM $COMMAND insert [--force,-f] uri pass-name"
+
+ local uri="$1"
+
+ otp_parse_uri "$uri"
+
+ otp_insert "$2" $force "$otp_uri"
+}
+
otp_insert_totp() {
local opts contents secret="" algorithm="sha1" period=30 digits=6 force=0
opts="$($GETOPT -o s:a:p:d:f -l secret:,algorithm:,period:,digits:,force -n "$PROGRAM" -- "$@")"
@@ -203,7 +223,7 @@ cmd_otp_insert() {
case "$1" in
totp) shift; otp_insert_totp "$@" ;;
hotp) shift; otp_insert_hotp "$@" ;;
- *) die "Invalid OTP type '$1'. May be one of 'totp' or 'hotp'" ;;
+ *) otp_insert_uri "$@" ;;
esac
}
diff --git a/test/insert.t b/test/insert.t
index a78971f..e04914e 100755
--- a/test/insert.t
+++ b/test/insert.t
@@ -4,16 +4,44 @@ export test_description="Tests pass otp insert commands"
. ./setup.sh
+test_expect_success 'Inserts a key URI' '
+ uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example"
+
+ test_pass_init &&
+ "$PASS" otp insert "$uri" passfile &&
+ [[ $("$PASS" show passfile) == "$uri" ]]
+'
+
+test_expect_success 'Prompts before overwriting key URI' '
+ uri1="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Foo"
+ uri2="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Bar"
+
+ test_pass_init &&
+ "$PASS" otp insert "$uri1" passfile &&
+ test_faketty "echo n | $PASS otp insert $uri2 passfile" &&
+ [[ $("$PASS" show passfile) == "$uri1" ]]
+'
+
+test_expect_success 'Force overwrites key URI' '
+ uri1="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Foo"
+ uri2="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Bar"
+
+ test_pass_init &&
+ "$PASS" otp insert "$uri1" passfile &&
+ "$PASS" otp insert -f "$uri2" passfile &&
+ [[ $("$PASS" show passfile) == "$uri2" ]]
+'
+
test_expect_success 'Inserts a basic TOTP key' '
- "$PASS" init $KEY1 &&
- "$PASS" otp insert totp -s AAAAAAAAAAAAAAAAAAAAA totp-secret
+ test_pass_init &&
+ "$PASS" otp insert totp -s AAAAAAAAAAAAAAAAAAAAA passfile
'
test_expect_success 'Commits insert to git' '
- git init "$PASSWORD_STORE_DIR" &&
- "$PASS" init $KEY1 &&
- "$PASS" otp insert totp -s AAAAAAAAAAAAAAAAAAAAA totp-secret2 &&
- git log --no-decorate -1 | grep "Add given OTP secret for totp-secret2 to store."
+ test_pass_init &&
+ pass git init &&
+ "$PASS" otp insert totp -s AAAAAAAAAAAAAAAAAAAAA passfile &&
+ git log --no-decorate -1 | grep "Add given OTP secret for passfile to store."
'
test_done
diff --git a/test/setup.sh b/test/setup.sh
index f896382..b363e6d 100644
--- a/test/setup.sh
+++ b/test/setup.sh
@@ -33,12 +33,6 @@ export PASSWORD_STORE_ENABLE_EXTENSIONS=true
export PASSWORD_STORE_EXTENSIONS_DIR="$EXT_HOME"
export PASSWORD_STORE_DIR="$SHARNESS_TRASH_DIRECTORY/test-store"
-rm -rf "$PASSWORD_STORE_DIR"
-mkdir -p "$PASSWORD_STORE_DIR"
-if [[ ! -d $PASSWORD_STORE_DIR ]]; then
- echo "Could not create $PASSWORD_STORE_DIR"
- exit 1
-fi
export GIT_DIR="$PASSWORD_STORE_DIR/.git"
export GIT_WORK_TREE="$PASSWORD_STORE_DIR"
@@ -65,3 +59,13 @@ KEY2="D774A374" # pass test key 2
KEY3="EB7D54A8" # pass test key 3
KEY4="E4691410" # pass test key 4
KEY5="39E5020C" # pass test key 5
+
+# Test helpers
+test_pass_init() {
+ rm -rf "$PASSWORD_STORE_DIR"
+ "$PASS" init "$KEY1"
+}
+
+test_faketty() {
+ script -qfc "$(printf "%q " "$@")"
+}