@zachjxyz/moxie 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env bash
2
+ # moxie/lib/gateway-keys.sh — Secure API key storage for AI Gateway
3
+ # Compatible with Bash 3.2. Requires platform.sh (MOXIE_PLATFORM).
4
+
5
+ MOXIE_KEY_DIR="$HOME/.moxie"
6
+
7
+ # Detect WSL (subset of linux)
8
+ _is_wsl() {
9
+ [ "$MOXIE_PLATFORM" = "linux" ] && grep -qi microsoft /proc/version 2>/dev/null
10
+ }
11
+
12
+ # ---- Store an API key ----
13
+ # Usage: gateway_store_key <service_label>
14
+ # Reads key interactively from /dev/tty (never from args or stdin pipe).
15
+
16
+ gateway_store_key() {
17
+ local label="${1:-vercel-ai-gateway}"
18
+
19
+ printf "Enter API key for %s: " "$label" >&2
20
+ local key
21
+ # Read without echo so the key isn't visible
22
+ stty -echo < /dev/tty 2>/dev/null
23
+ IFS= read -r key < /dev/tty
24
+ stty echo < /dev/tty 2>/dev/null
25
+ printf "\n" >&2
26
+
27
+ if [ -z "$key" ]; then
28
+ echo "ERROR: No key provided." >&2
29
+ return 1
30
+ fi
31
+
32
+ if _is_wsl; then
33
+ powershell.exe -Command "[System.Environment]::SetEnvironmentVariable('MOXIE_GATEWAY_KEY_${label}', '${key}', 'User')" 2>/dev/null
34
+ if [ $? -eq 0 ]; then
35
+ echo "Key stored in Windows User environment." >&2
36
+ else
37
+ echo "ERROR: Failed to store key via powershell.exe." >&2
38
+ return 1
39
+ fi
40
+ elif [ "$MOXIE_PLATFORM" = "darwin" ]; then
41
+ security add-generic-password -a moxie -s "$label" -w "$key" -U 2>/dev/null
42
+ if [ $? -eq 0 ]; then
43
+ echo "Key stored in macOS Keychain." >&2
44
+ else
45
+ echo "ERROR: Failed to store key in Keychain." >&2
46
+ return 1
47
+ fi
48
+ elif [ "$MOXIE_PLATFORM" = "linux" ]; then
49
+ mkdir -p "$MOXIE_KEY_DIR"
50
+ chmod 700 "$MOXIE_KEY_DIR"
51
+ local enc_file="$MOXIE_KEY_DIR/${label}.key.enc"
52
+ printf "Set a passphrase to encrypt your key (you'll need this when moxie starts):\n" >&2
53
+ echo "$key" | openssl enc -aes-256-cbc -pbkdf2 -salt -out "$enc_file" -pass file:/dev/tty 2>/dev/null
54
+ if [ $? -eq 0 ]; then
55
+ chmod 600 "$enc_file"
56
+ echo "Key encrypted and stored at $enc_file" >&2
57
+ else
58
+ echo "ERROR: Failed to encrypt key." >&2
59
+ return 1
60
+ fi
61
+ else
62
+ echo "ERROR: Unsupported platform for key storage." >&2
63
+ return 1
64
+ fi
65
+ }
66
+
67
+ # ---- Retrieve an API key ----
68
+ # Usage: key=$(gateway_get_key <service_label>)
69
+ # Outputs key to stdout. Returns 1 if not found.
70
+
71
+ gateway_get_key() {
72
+ local label="${1:-vercel-ai-gateway}"
73
+
74
+ if _is_wsl; then
75
+ local key
76
+ key=$(powershell.exe -Command "[System.Environment]::GetEnvironmentVariable('MOXIE_GATEWAY_KEY_${label}', 'User')" 2>/dev/null | tr -d '\r\n')
77
+ if [ -n "$key" ]; then
78
+ echo "$key"
79
+ return 0
80
+ fi
81
+ return 1
82
+ elif [ "$MOXIE_PLATFORM" = "darwin" ]; then
83
+ security find-generic-password -a moxie -s "$label" -w 2>/dev/null
84
+ return $?
85
+ elif [ "$MOXIE_PLATFORM" = "linux" ]; then
86
+ local enc_file="$MOXIE_KEY_DIR/${label}.key.enc"
87
+ if [ ! -f "$enc_file" ]; then
88
+ return 1
89
+ fi
90
+ # Use MOXIE_KEY_PASSPHRASE env var for non-interactive, otherwise prompt
91
+ if [ -n "${MOXIE_KEY_PASSPHRASE:-}" ]; then
92
+ openssl enc -aes-256-cbc -pbkdf2 -d -in "$enc_file" -pass "pass:$MOXIE_KEY_PASSPHRASE" 2>/dev/null
93
+ else
94
+ openssl enc -aes-256-cbc -pbkdf2 -d -in "$enc_file" -pass file:/dev/tty 2>/dev/null
95
+ fi
96
+ return $?
97
+ fi
98
+ return 1
99
+ }
100
+
101
+ # ---- Check if a key exists (no output) ----
102
+ # Usage: gateway_has_key <service_label> && echo "yes"
103
+
104
+ gateway_has_key() {
105
+ local label="${1:-vercel-ai-gateway}"
106
+
107
+ if _is_wsl; then
108
+ local key
109
+ key=$(powershell.exe -Command "[System.Environment]::GetEnvironmentVariable('MOXIE_GATEWAY_KEY_${label}', 'User')" 2>/dev/null | tr -d '\r\n')
110
+ [ -n "$key" ]
111
+ elif [ "$MOXIE_PLATFORM" = "darwin" ]; then
112
+ security find-generic-password -a moxie -s "$label" -w &>/dev/null
113
+ elif [ "$MOXIE_PLATFORM" = "linux" ]; then
114
+ [ -f "$MOXIE_KEY_DIR/${label}.key.enc" ]
115
+ else
116
+ return 1
117
+ fi
118
+ }