diff --git a/README.md b/README.md index acd0b47..dcc6ea8 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Generate a 2FA code using this token: ``` $ pass otp totp-secret 698816 +Expires in 26 seconds ``` Display a QR code for an OTP token: diff --git a/otp.bash b/otp.bash index d4c7756..78115e2 100755 --- a/otp.bash +++ b/otp.bash @@ -334,9 +334,11 @@ cmd_otp_code() { local cmd case "$otp_type" in totp) + curr_time=$(date +%s) + totp_time_remaining=$((30 - (curr_time % 30))) cmd="$OATH -b --totp" [[ -n "$otp_algorithm" ]] && cmd+=$(echo "=${otp_algorithm}"|tr "[:upper:]" "[:lower:]") - [[ -n "$otp_period" ]] && cmd+=" --time-step-size=$otp_period"s + [[ -n "$otp_period" ]] && cmd+=" --time-step-size=$otp_period"s && totp_time_remaining=$((otp_period - (curr_time % otp_period))) [[ -n "$otp_digits" ]] && cmd+=" --digits=$otp_digits" cmd+=" $otp_secret" ;; @@ -369,8 +371,10 @@ cmd_otp_code() { if [[ $clip -ne 0 ]]; then clip "$out" "OTP code for $path" + [[ -n "$totp_time_remaining" ]] && echo "Expires in $totp_time_remaining seconds" else echo "$out" + [[ -n "$totp_time_remaining" ]] && echo "Expires in $totp_time_remaining seconds" fi } diff --git a/test/code.t b/test/code.t index 39310fb..cd828be 100755 --- a/test/code.t +++ b/test/code.t @@ -15,7 +15,7 @@ test_expect_success 'Generates TOTP code' ' test_pass_init && "$PASS" otp insert passfile <<< "$uri" && - code=$("$PASS" otp passfile) && + code=$("$PASS" otp passfile | head -n 1) && [[ ${#code} -eq 6 ]] '