#!/usr/bin/env bash # auth.sh — pi credential checkpoint (M19): per-provider auth report and # named-account listing. Never prints credential material: provider names, # credential types, permission bits, and env var NAMES only. # # Usage: # scripts/auth.sh status per-provider report (auth.json + env-side names) # scripts/auth.sh accounts list named account files (auth..json) # # The credential file is the compose read-only mount source (PI_AUTH_FILE, # default ~/.pi/agent/auth.json). Resolution follows pi: auth.json entries # take priority over environment variables. # # Exit codes: 0 report produced · 2 credential file unparseable · # 3 file/dir missing for a read · 4 file/environment problem. set -uo pipefail cd "$(dirname "$0")/.." AUTH_FILE="${PI_AUTH_FILE:-$HOME/.pi/agent/auth.json}" AUTH_DIR="$(dirname "$AUTH_FILE")" OP="${1:-}" case "$OP" in status) if [ ! -e "$AUTH_FILE" ]; then echo "auth: credential file not found: $AUTH_FILE" >&2 exit 3 fi if [ ! -f "$AUTH_FILE" ] || [ -L "$AUTH_FILE" ]; then echo "auth: credential file must be a regular, non-symbolic-link file: $AUTH_FILE" >&2 exit 4 fi if ! node -e ' const fs = require("fs"); const file = process.argv[1]; let doc; try { doc = JSON.parse(fs.readFileSync(file, "utf8")); } catch (e) { console.error("auth: credential file is not valid JSON: " + e.message); process.exit(2); } if (typeof doc !== "object" || doc === null || Array.isArray(doc)) { console.error("auth: credential file must be a JSON object keyed by provider"); process.exit(2); } const st = fs.statSync(file); console.log("auth file: " + file + " (perms " + String(st.mode & 0o777).toString(8).padStart(3, "0") + ")"); const keys = Object.keys(doc).sort(); if (keys.length === 0) console.log("providers: (none in file)"); for (const k of keys) { const e = doc[k]; let type = "unknown"; if (typeof e === "object" && e !== null && typeof e.type === "string" && /^[a-z0-9_-]+$/i.test(e.type)) type = e.type; console.log(" " + k + " type=" + type + " source=auth.json"); } console.log("resolution: auth.json entries take priority over environment (pi order: --api-key > auth.json > env > models.json)"); ' "$AUTH_FILE"; then exit 2 fi ENV_NAMES="$(env | grep -oE '^[A-Z0-9_]+_API_KEY' | sort -u | paste -sd, -)" if [ -n "$ENV_NAMES" ]; then echo "env-side credential-like names set (informational; file entries take priority): $ENV_NAMES" else echo "env-side credential-like names set: (none)" fi ;; accounts) if [ ! -d "$AUTH_DIR" ]; then echo "auth: directory not found: $AUTH_DIR" >&2 exit 3 fi shopt -s nullglob FILES=("$AUTH_DIR"/auth.*.json) shopt -u nullglob if [ "${#FILES[@]}" -eq 0 ]; then echo "accounts: (none) — named accounts are auth..json beside the credential file" exit 0 fi echo "named accounts in $AUTH_DIR:" for f in "${FILES[@]}"; do name="$(basename "$f" .json)" name="${name#auth.}" marker="" [ "$f" = "$AUTH_FILE" ] && marker=" <- active (PI_AUTH_FILE)" echo " $name$marker" done ;; *) echo "usage: scripts/auth.sh status | accounts" >&2 exit 4 ;; esac