#!/bin/sh
# Every auth action as one idempotent command with JSON out, for an agent with no terminal: store
# an API key (RPC add_credential, pic auth login --api-key --json), run the free checks
# (check_credentials, pic auth status --check --json), list keys with what is left
# (get_credentials, pic auth status --json), make a key the provider's (set_credential with
# persist, pic auth use --json), switch model and key (set_model with credential, pic models use
# --json), bind a model family to a key (--for, set_model_credential), remove a key (logout with
# label, pic auth logout --json). Running one twice changes
# nothing and says so; no output carries a key. Against a fake server, with a private HOME.
# --no-faults (the release build): skipped, since the passphrase comes from PIC_TEST_PASSPHRASE,
# which only the dev build reads.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
SQLITE3=${SQLITE3:-sqlite3}
if [ "${1:-}" = --no-faults ]; then
printf 'SKIP: authcmd needs the dev build (PIC_TEST_PASSPHRASE)\n'
exit 0
fi
S=$(cd "$(dirname "$0")/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-authcmd.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
mkdir "$T/home" "$T/ws" "$T/log" "$T/out"
export HOME="$T/home" PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline PIC_MODELS_FILE="$T/models.json"
export PIC_TEST_PASSPHRASE="pic-ac-passphrase-$$"
K1="pic-ac-one-$$-a1"
K2="pic-ac-two-$$-b2"
printf '{"data":[{"id":"m"}]}' > "$T/models.body"
"$SERVER" "$T/port" "$T/log" 120 "at:/v1/models=status:200:$T/models.body" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
port=$(cat "$T/port")
cat > "$T/models.json" <<EOM
{"providers":{"fake":{"baseUrl":"http://127.0.0.1:$port","api":"anthropic-messages","apiKey":"\$FAKE_API_KEY","models":[
{"id":"m","contextWindow":200000},{"id":"n","contextWindow":200000,"reasoning":true}]}}}
EOM
pic() { # NAME ARGS...: stdout and stderr in $T/out/NAME.{out,err}
name=$1
shift
"$PIC" --db "$T/j" --workspace "$T/ws" "$@" > "$T/out/$name.out" 2> "$T/out/$name.err" < /dev/null
}
rpc() { # NAME LINE...: the replies in $T/out/NAME.out, no terminal anywhere
name=$1
shift
printf '%s\n' "$@" | "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m --mode rpc > "$T/out/$name.out" 2> "$T/out/$name.err" ||
fail "rpc $name: $(cat "$T/out/$name.err")"
}
reply() { grep "\"id\":\"$2\"" "$T/out/$1.out"; }
setting() { "$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT value FROM picu_setting WHERE key = '$1'"; }
# Store an API key over RPC, twice: the second changes nothing and says so.
rpc add "{\"id\":\"a\",\"type\":\"add_credential\",\"provider\":\"fake\",\"key\":\"$K1\"}" \
"{\"id\":\"b\",\"type\":\"add_credential\",\"provider\":\"fake\",\"key\":\"$K1\"}" \
"{\"id\":\"c\",\"type\":\"add_credential\",\"provider\":\"fake\",\"label\":\"second\",\"key\":\"$K2\"}" \
'{"id":"d","type":"add_credential","provider":"fake","label":"Bad Label","key":"x"}' \
'{"id":"e","type":"add_credential","provider":"nosuch","key":"x"}' \
'{"id":"f","type":"add_credential","provider":"fake"}'
reply add a | grep -qF '"data":{"provider":"fake","label":"default","kind":"api_key","changed":true,"active":true}' ||
fail "add: $(cat "$T/out/add.out")"
reply add b | grep -qF '"changed":false' || fail "add again: $(cat "$T/out/add.out")"
reply add c | grep -qF '"label":"second","kind":"api_key","changed":true,"active":false' || fail "add second: $(cat "$T/out/add.out")"
for id in d e f; do reply add $id | grep -qF '"success":false' || fail "add $id: $(cat "$T/out/add.out")"; done
printf 'PASS: add_credential stores a key, and again changes nothing\n'
# The shell's: a key on stdin, JSON out.
printf '%s\n' "$K2" | "$PIC" --db "$T/j" --workspace "$T/ws" auth login fake --api-key --as second --json > "$T/out/sh-add.out" 2> "$T/out/sh-add.err" ||
fail "shell add: $(cat "$T/out/sh-add.err")"
grep -qxF '{"provider":"fake","label":"second","kind":"api_key","changed":false,"active":false}' "$T/out/sh-add.out" ||
fail "shell add: $(cat "$T/out/sh-add.out")"
printf 'PASS: pic auth login --api-key --json says whether it changed anything\n'
# List with what is left, and run the free checks.
rpc list '{"id":"l","type":"get_credentials","provider":"fake"}' '{"id":"k","type":"check_credentials","provider":"fake"}'
reply list l | grep -qF '"label":"second","kind":"api_key","active":false,"state":"unknown"' || fail "list: $(cat "$T/out/list.out")"
reply list k | grep -qF '"checked":2' || fail "check: $(cat "$T/out/list.out")"
reply list k | grep -qF '"label":"second","kind":"api_key","active":false,"state":"ok"' || fail "check: $(cat "$T/out/list.out")"
grep -q '^GET /v1/models' "$T/log"/*.req || fail "check: no free check was sent"
pic st auth status --json || fail "status --json: $(cat "$T/out/st.err")"
grep -qF '{"credentials":[' "$T/out/st.out" || fail "status --json: $(cat "$T/out/st.out")"
grep -qF '"provider":"fake","label":"second"' "$T/out/st.out" || fail "status --json: $(cat "$T/out/st.out")"
pic stc auth status --check --json fake || fail "status --check --json: $(cat "$T/out/stc.err")"
grep -qF '"checked":2' "$T/out/stc.out" || fail "status --check --json: $(cat "$T/out/stc.out")"
printf 'PASS: get_credentials and check_credentials, and pic auth status --json and --check --json\n'
# Make a key the provider's, saved: set_credential with persist, and pic auth use --json.
rpc use '{"id":"u","type":"set_credential","provider":"fake","label":"second","persist":true}' \
'{"id":"v","type":"set_credential","provider":"fake","label":"second","persist":true}'
reply use u | grep -qF '"data":{"provider":"fake","label":"second","persist":true,"changed":true}' || fail "use: $(cat "$T/out/use.out")"
reply use v | grep -qF '"changed":false' || fail "use again: $(cat "$T/out/use.out")"
[ "$(setting activeKey:fake)" = second ] || fail "use: not saved: $(setting activeKey:fake)"
pic shuse auth use fake default --json || fail "shell use: $(cat "$T/out/shuse.err")"
grep -qxF '{"provider":"fake","label":"default","changed":true}' "$T/out/shuse.out" || fail "shell use: $(cat "$T/out/shuse.out")"
pic shuse2 auth use fake default --json || fail "shell use again: $(cat "$T/out/shuse2.err")"
grep -qxF '{"provider":"fake","label":"default","changed":false}' "$T/out/shuse2.out" || fail "shell use again: $(cat "$T/out/shuse2.out")"
printf 'PASS: set_credential persist and pic auth use --json save the choice, idempotently\n'
# Switch model and key together: set_model with credential, and pic models use --json.
rpc sw '{"id":"s","type":"set_model","provider":"fake","modelId":"n","credential":"second"}' '{"id":"g","type":"get_state"}' \
'{"id":"p","type":"prompt","message":"hi"}' '{"id":"x","type":"set_model","provider":"fake","modelId":"n","credential":"nosuch"}'
reply sw s | grep -qF '"success":true' || fail "set_model credential: $(cat "$T/out/sw.out")"
reply sw x | grep -qF '"success":false' || fail "set_model bad credential: $(cat "$T/out/sw.out")"
grep -l '"model":"n"' "$T/log"/*.req | xargs grep -l "^x-api-key: $K2" > /dev/null || fail "set_model credential: the key was not used"
[ "$(setting activeKey:fake)" = default ] || fail "set_model credential saved the choice"
pic mu models use fake/n --key second --json || fail "models use: $(cat "$T/out/mu.err")"
grep -qxF '{"model":"fake/n","credential":"fake:second","changed":true}' "$T/out/mu.out" || fail "models use: $(cat "$T/out/mu.out")"
pic mu2 models use fake/n --key second --json || fail "models use again: $(cat "$T/out/mu2.err")"
grep -qxF '{"model":"fake/n","credential":"fake:second","changed":false}' "$T/out/mu2.out" || fail "models use again: $(cat "$T/out/mu2.out")"
[ "$(setting defaultModel)" = fake/n ] && [ "$(setting activeKey:fake)" = second ] || fail "models use: not saved"
rc=0
pic mu3 models use fake/n --key nosuch --json || rc=$?
[ "$rc" = 1 ] || fail "models use with a missing key: exit $rc"
printf 'PASS: set_model with credential and pic models use --json switch model and key\n'
# Rules: a model family to a key. The provider's own key stays for the rest; --key still wins.
pic r0 auth use fake default || fail "use default: $(cat "$T/out/r0.err")"
pic r1 auth use fake second --for 'fake/n*' --json || fail "rule: $(cat "$T/out/r1.err")"
grep -qxF '{"pattern":"fake/n*","provider":"fake","label":"second","changed":true}' "$T/out/r1.out" || fail "rule: $(cat "$T/out/r1.out")"
pic r2 auth use fake second --for 'fake/n*' --json || fail "rule again: $(cat "$T/out/r2.err")"
grep -qF '"changed":false' "$T/out/r2.out" || fail "rule again: $(cat "$T/out/r2.out")"
rc=0
pic r3 auth use fake nosuch --for 'fake/m' || rc=$?
[ "$rc" = 1 ] && grep -qF 'no key named "nosuch"' "$T/out/r3.err" || fail "rule with a missing key: exit $rc $(cat "$T/out/r3.err")"
rc=0
pic r4 auth use fake second --for 'other/x' || rc=$?
[ "$rc" = 1 ] || fail "a rule for another provider's models was taken"
rc=0
pic r5 auth use fake default --for 'fake/*n' || rc=$?
[ "$rc" = 1 ] && grep -qF 'fake/n*' "$T/out/r5.err" || fail "a rule as long as one it overlaps: exit $rc $(cat "$T/out/r5.err")"
pic r6 --model fake/n -p 'by rule' || fail "by rule: $(cat "$T/out/r6.err")"
last=$(ls "$T/log" | sort -n | tail -1)
grep -q "^x-api-key: $K2" "$T/log/$last" || fail "by rule: the rule's key was not sent"
pic r7 --model fake/m -p 'no rule' || fail "no rule: $(cat "$T/out/r7.err")"
last=$(ls "$T/log" | sort -n | tail -1)
grep -q "^x-api-key: $K1" "$T/log/$last" || fail "no rule: the provider's key was not sent"
pic r8 --model fake/n --key default -p 'flag' || fail "--key over a rule: $(cat "$T/out/r8.err")"
last=$(ls "$T/log" | sort -n | tail -1)
grep -q "^x-api-key: $K1" "$T/log/$last" || fail "--key did not win over the rule"
pic r9 models --json || fail "models --json: $(cat "$T/out/r9.err")"
grep '"model":"fake/n"' "$T/out/r9.out" | grep -qF '"credential":"fake:second","bound_by":"rule","rule":"fake/n*"' ||
fail "models --json fake/n: $(grep '"model":"fake/n"' "$T/out/r9.out")"
grep '"model":"fake/m"' "$T/out/r9.out" | grep -qF '"credential":"fake:default","bound_by":"provider"' ||
fail "models --json fake/m: $(grep '"model":"fake/m"' "$T/out/r9.out")"
pic r10 auth status --json || fail "status: $(cat "$T/out/r10.err")"
grep -qF '"bindings":[{"pattern":"fake/n*","provider":"fake","label":"second"}]' "$T/out/r10.out" || fail "bindings: $(cat "$T/out/r10.out")"
rpc rb '{"id":"b1","type":"set_model_credential","pattern":"fake/m","label":"second"}' \
'{"id":"b2","type":"set_model_credential","pattern":"fake/m","label":null}' \
'{"id":"b3","type":"set_model_credential","pattern":"fake/m","label":null}' '{"id":"b4","type":"get_credentials"}'
reply rb b1 | grep -qF '"data":{"pattern":"fake/m","provider":"fake","label":"second","changed":true}' || fail "rpc rule: $(cat "$T/out/rb.out")"
reply rb b2 | grep -qF '"label":null,"changed":true' || fail "rpc rule removed: $(cat "$T/out/rb.out")"
reply rb b3 | grep -qF '"changed":false' || fail "rpc rule removed again: $(cat "$T/out/rb.out")"
reply rb b4 | grep -qF '"bindings":[{"pattern":"fake/n*"' || fail "rpc bindings: $(cat "$T/out/rb.out")"
pic r11 auth use fake --for 'fake/n*' --clear --json || fail "clear: $(cat "$T/out/r11.err")"
grep -qxF '{"pattern":"fake/n*","provider":"fake","label":null,"changed":true}' "$T/out/r11.out" || fail "clear: $(cat "$T/out/r11.out")"
printf 'PASS: rules bind a model family to a key: shell, RPC, requests, pic models --json; --key still wins\n'
# Profiles: a name for a model, a key and a thinking level, accepted wherever a model is.
pic p1 profiles set work --model fake/n --key second --thinking high --json || fail "profile: $(cat "$T/out/p1.err")"
grep -qxF '{"profile":"work","model":"fake/n","credential":"fake:second","thinking":"high","changed":true}' "$T/out/p1.out" ||
fail "profile: $(cat "$T/out/p1.out")"
pic p2 profiles set work --model fake/n --key second --thinking high --json || fail "profile again: $(cat "$T/out/p2.err")"
grep -qF '"changed":false' "$T/out/p2.out" || fail "profile again: $(cat "$T/out/p2.out")"
rc=0
pic p3 profiles set fake --model fake/m || rc=$?
[ "$rc" = 2 ] || fail "a profile named as a provider was taken"
rc=0
pic p4 profiles set bad --model fake/m --key nosuch || rc=$?
[ "$rc" = 1 ] || fail "a profile with a missing key was taken"
pic p5 profiles --json || fail "profiles: $(cat "$T/out/p5.err")"
grep -qxF '{"profile":"work","model":"fake/n","key":"second","thinking":"high"}' "$T/out/p5.out" || fail "profiles: $(cat "$T/out/p5.out")"
pic r0p auth use fake default || fail "use default: $(cat "$T/out/r0p.err")"
pic p6 --model work -p 'by profile' || fail "--model work: $(cat "$T/out/p6.err")"
last=$(ls "$T/log" | sort -n | tail -1)
grep -q '"model":"n"' "$T/log/$last" && grep -q "^x-api-key: $K2" "$T/log/$last" || fail "--model work: the profile's model and key were not used"
rpc pr '{"id":"g","type":"get_profiles"}' '{"id":"s","type":"set_model","profile":"work"}' '{"id":"t","type":"get_state"}' \
'{"id":"x","type":"set_model","profile":"nosuch"}'
reply pr g | grep -qF '"profiles":[{"name":"work","model":"fake/n","credential":"fake:second","thinking":"high"}]' || fail "get_profiles: $(cat "$T/out/pr.out")"
reply pr s | grep -qF '"success":true' || fail "set_model profile: $(cat "$T/out/pr.out")"
reply pr t | grep -qF '"id":"n"' && reply pr t | grep -qF '"thinkingLevel":"high"' || fail "set_model profile state: $(cat "$T/out/pr.out")"
reply pr x | grep -qF '"success":false' || fail "set_model unknown profile: $(cat "$T/out/pr.out")"
pic p7 profiles remove work --json || fail "remove: $(cat "$T/out/p7.err")"
grep -qxF '{"profile":"work","removed":true}' "$T/out/p7.out" || fail "remove: $(cat "$T/out/p7.out")"
printf 'PASS: profiles name a model, key and thinking level for --model, RPC set_model and get_profiles\n'
# Remove a key: logout with label, and pic auth logout --json; twice says removed false.
rpc out '{"id":"o","type":"logout","provider":"fake","label":"second"}' '{"id":"q","type":"logout","provider":"fake","label":"second"}'
reply out o | grep -qF '"data":{"removed":true}' || fail "logout: $(cat "$T/out/out.out")"
reply out q | grep -qF '"data":{"removed":false}' || fail "logout again: $(cat "$T/out/out.out")"
pic shout auth logout fake --json || fail "shell logout: $(cat "$T/out/shout.err")"
grep -qxF '{"provider":"fake","label":"default","removed":true}' "$T/out/shout.out" || fail "shell logout: $(cat "$T/out/shout.out")"
printf 'PASS: logout with label and pic auth logout --json remove a key\n'
# No output and no file but the requests carries a key.
if grep -rlF -e "$K1" -e "$K2" "$T/out" "$T/j" "$HOME/.pic" 2>/dev/null | grep -q .; then
fail "a key was written: $(grep -rlF -e "$K1" -e "$K2" "$T/out" "$T/j" "$HOME/.pic")"
fi
printf 'PASS: no output carries a key\n'