Skip to content

Commit d0435fd

Browse files
committed
Add otp command
1 parent 6167c88 commit d0435fd

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

commands/system/otp.sh

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
# Dependency: This script requires `apw`, `jq` and `awk` to be installed and in $PATH
4+
#
5+
# Required parameters:
6+
# @raycast.schemaVersion 1
7+
# @raycast.title One-Time Password
8+
# @raycast.mode silent
9+
#
10+
# Optional parameters:
11+
# @raycast.icon 🔑
12+
# @raycast.packageName System
13+
# @raycast.argument1 { "type": "text", "placeholder": "domain" }
14+
# @raycast.argument2 { "type": "text", "placeholder": "username index", "optional": true }
15+
#
16+
# @raycast.description Get OTP (One-Time Password) from Apple Password Manager
17+
# @raycast.author Angelos Michalopoulos
18+
# @raycase.authorURL https://github.com/miagg
19+
20+
if ! command -v apw &> /dev/null || ! command -v jq &> /dev/null || ! command -v awk &> /dev/null; then
21+
echo "This function requires apw, jq and awk to be installed"
22+
exit 1
23+
fi
24+
UINDEX=$((${2:-1} - 1))
25+
CODES=$(apw otp get "$1" 2>nul)
26+
STATUS=$?
27+
# ✋ If return code 9, not authenticated, run apw auth
28+
if [ $STATUS -eq 9 ]; then
29+
apw auth
30+
CODES=$(apw otp get "$1")
31+
fi
32+
# ✋ If return code 3, domain not found, alert user
33+
if [ $STATUS -eq 3 ]; then
34+
echo "Domain not found"
35+
exit 1
36+
fi
37+
# Grab available OTP codes for domain
38+
if [ $(echo $CODES | jq '.results | length') -gt 1 ]; then
39+
CODE=$(echo $CODES | jq -r ".results[$UINDEX].code")
40+
USERNAME=$(echo $CODES | jq -r ".results[$UINDEX].username")
41+
if [ "$CODE" == "null" ]; then
42+
echo "Index out of range"
43+
exit 1
44+
fi
45+
else
46+
CODE=$(echo $CODES | jq -r '.results[0].code')
47+
USERNAME=$(echo $CODES | jq -r ".results[0].username")
48+
fi
49+
echo $CODE | pbcopy
50+
echo "OTP code for $USERNAME copied to clipboard"

0 commit comments

Comments
 (0)