From 64de3b27cd7e147088282f6cb3c69c6c8c7cbb06 Mon Sep 17 00:00:00 2001 From: Angelos Michalopoulos Date: Thu, 31 Oct 2024 14:23:46 +0200 Subject: [PATCH 1/2] Add otp command --- commands/system/otp.sh | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 commands/system/otp.sh diff --git a/commands/system/otp.sh b/commands/system/otp.sh new file mode 100755 index 000000000..0e4843091 --- /dev/null +++ b/commands/system/otp.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +# Dependency: This script requires `apw`, `jq` and `awk` to be installed and in $PATH +# +# Required parameters: +# @raycast.schemaVersion 1 +# @raycast.title One-Time Password +# @raycast.mode silent +# +# Optional parameters: +# @raycast.icon 🔑 +# @raycast.packageName System +# @raycast.argument1 { "type": "text", "placeholder": "domain" } +# @raycast.argument2 { "type": "text", "placeholder": "username index", "optional": true } +# +# @raycast.description Get OTP (One-Time Password) from Apple Password Manager +# @raycast.author Angelos Michalopoulos +# @raycase.authorURL https://github.com/miagg + +if ! command -v apw &> /dev/null || ! command -v jq &> /dev/null || ! command -v awk &> /dev/null; then + echo "This function requires apw, jq and awk to be installed" + exit 1 +fi +UINDEX=$((${2:-1} - 1)) +CODES=$(apw otp get "$1" 2>/dev/null) +STATUS=$? +# ✋ If return code 9, not authenticated, run apw auth +if [ $STATUS -eq 9 ]; then + apw auth + CODES=$(apw otp get "$1") +fi +# ✋ If return code 3, domain not found, alert user +if [ $STATUS -eq 3 ]; then + echo "Domain not found" + exit 1 +fi +# Grab available OTP codes for domain +if [ $(echo $CODES | jq '.results | length') -gt 1 ]; then + CODE=$(echo $CODES | jq -r ".results[$UINDEX].code") + USERNAME=$(echo $CODES | jq -r ".results[$UINDEX].username") + if [ "$CODE" == "null" ]; then + echo "Index out of range" + exit 1 + fi +else + CODE=$(echo $CODES | jq -r '.results[0].code') + USERNAME=$(echo $CODES | jq -r ".results[0].username") +fi +echo $CODE | pbcopy +echo "OTP code for $USERNAME copied to clipboard" \ No newline at end of file From 4c4f7c0e4a3931b174f638642a354992adf426ba Mon Sep 17 00:00:00 2001 From: Angelos Michalopoulos Date: Thu, 31 Oct 2024 15:38:14 +0200 Subject: [PATCH 2/2] apply required fixes --- commands/system/otp.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/commands/system/otp.sh b/commands/system/otp.sh index 0e4843091..830ba3ded 100755 --- a/commands/system/otp.sh +++ b/commands/system/otp.sh @@ -10,10 +10,10 @@ # Optional parameters: # @raycast.icon 🔑 # @raycast.packageName System -# @raycast.argument1 { "type": "text", "placeholder": "domain" } -# @raycast.argument2 { "type": "text", "placeholder": "username index", "optional": true } +# @raycast.argument1 { "type": "text", "placeholder": "Domain" } +# @raycast.argument2 { "type": "text", "placeholder": "Username Index", "optional": true } # -# @raycast.description Get OTP (One-Time Password) from Apple Password Manager +# @raycast.description Get One-Time Password (OTP) from Apple Password Manager # @raycast.author Angelos Michalopoulos # @raycase.authorURL https://github.com/miagg @@ -26,20 +26,21 @@ CODES=$(apw otp get "$1" 2>/dev/null) STATUS=$? # ✋ If return code 9, not authenticated, run apw auth if [ $STATUS -eq 9 ]; then - apw auth - CODES=$(apw otp get "$1") + echo "Please authenticate first by running 'apw auth'" + exit 1 fi # ✋ If return code 3, domain not found, alert user if [ $STATUS -eq 3 ]; then - echo "Domain not found" + echo "Domain $1 not found" exit 1 fi # Grab available OTP codes for domain -if [ $(echo $CODES | jq '.results | length') -gt 1 ]; then +CODES_COUNT=$(echo $CODES | jq '.results | length') +if [ $CODES_COUNT -gt 1 ]; then CODE=$(echo $CODES | jq -r ".results[$UINDEX].code") USERNAME=$(echo $CODES | jq -r ".results[$UINDEX].username") if [ "$CODE" == "null" ]; then - echo "Index out of range" + echo "Please provide an index between 1 and $CODES_COUNT" exit 1 fi else