Skip to content

Commit 315385d

Browse files
authored
Add OPT command (One-Time Password) in system folder (#966)
* Add otp command * apply required fixes
1 parent 51a84ea commit 315385d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

commands/system/otp.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 One-Time Password (OTP) 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>/dev/null)
26+
STATUS=$?
27+
# ✋ If return code 9, not authenticated, run apw auth
28+
if [ $STATUS -eq 9 ]; then
29+
echo "Please authenticate first by running 'apw auth'"
30+
exit 1
31+
fi
32+
# ✋ If return code 3, domain not found, alert user
33+
if [ $STATUS -eq 3 ]; then
34+
echo "Domain $1 not found"
35+
exit 1
36+
fi
37+
# Grab available OTP codes for domain
38+
CODES_COUNT=$(echo $CODES | jq '.results | length')
39+
if [ $CODES_COUNT -gt 1 ]; then
40+
CODE=$(echo $CODES | jq -r ".results[$UINDEX].code")
41+
USERNAME=$(echo $CODES | jq -r ".results[$UINDEX].username")
42+
if [ "$CODE" == "null" ]; then
43+
echo "Please provide an index between 1 and $CODES_COUNT"
44+
exit 1
45+
fi
46+
else
47+
CODE=$(echo $CODES | jq -r '.results[0].code')
48+
USERNAME=$(echo $CODES | jq -r ".results[0].username")
49+
fi
50+
echo $CODE | pbcopy
51+
echo "OTP code for $USERNAME copied to clipboard"

0 commit comments

Comments
 (0)