auth-probe.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env bash
  2. # Probes Cloudflare API auth state for the Turnstile Spin agent.
  3. #
  4. # Reads:
  5. # $CLOUDFLARE_API_TOKEN (required)
  6. # $CLOUDFLARE_ACCOUNT_ID (optional; if set, must be one of the token's accounts)
  7. #
  8. # Outputs JSON to stdout, always exits 0. The agent reads `status`:
  9. # "ok" ; selected account passed both Turnstile and Workers scope probes
  10. # "missing_token" ; no token set, or wrangler whoami failed
  11. # "missing_scope" ; token lacks Account.Turnstile:Edit on the selected account
  12. # "missing_workers_scope"; token has Turnstile scope but lacks Workers Scripts on the selected account
  13. # "multiple_accounts" ; token covers >1 accounts and $CLOUDFLARE_ACCOUNT_ID is unset; agent must ask user to pick, set it, and re-run
  14. # "account_mismatch" ; $CLOUDFLARE_ACCOUNT_ID is set but is not in the token's accounts list
  15. #
  16. # Human-readable diagnostics go to stderr. The agent surfaces them to the user.
  17. set -uo pipefail
  18. emit() {
  19. echo "$1"
  20. exit 0
  21. }
  22. token="${CLOUDFLARE_API_TOKEN:-}"
  23. declared_account="${CLOUDFLARE_ACCOUNT_ID:-}"
  24. if [ -z "$token" ]; then
  25. echo "auth-probe: \$CLOUDFLARE_API_TOKEN is not set." >&2
  26. emit '{"status":"missing_token","reason":"no_env_var"}'
  27. fi
  28. whoami_json=$(npx wrangler whoami --json 2>/dev/null || true)
  29. if [ -z "$whoami_json" ] || [ "$(echo "$whoami_json" | head -c 1)" != "{" ]; then
  30. echo "auth-probe: wrangler whoami returned no JSON. Token may be invalid or expired." >&2
  31. emit '{"status":"missing_token","reason":"whoami_failed"}'
  32. fi
  33. # Extract the accounts array. Fall back to python3 if jq is missing.
  34. accounts_json=$(echo "$whoami_json" | (jq -c '.accounts' 2>/dev/null || python3 -c "import sys,json; print(json.dumps(json.load(sys.stdin)['accounts']))"))
  35. account_count=$(echo "$accounts_json" | (jq 'length' 2>/dev/null || python3 -c "import sys,json; print(len(json.load(sys.stdin)))"))
  36. if [ -z "$account_count" ] || [ "$account_count" = "0" ] || [ "$account_count" = "null" ]; then
  37. echo "auth-probe: wrangler whoami succeeded but no accounts found on the token." >&2
  38. emit '{"status":"missing_token","reason":"no_accounts"}'
  39. fi
  40. # Pick the account to probe:
  41. # - $CLOUDFLARE_ACCOUNT_ID set: must be in the token's accounts list, else account_mismatch
  42. # - unset, exactly 1 account: use it silently
  43. # - unset, >1 accounts: emit multiple_accounts; agent picks and re-runs
  44. if [ -n "$declared_account" ]; then
  45. in_list=$(echo "$accounts_json" | (jq --arg id "$declared_account" 'map(.id) | index($id) != null' 2>/dev/null || python3 -c "import sys,json; print('true' if any(a['id']==sys.argv[1] for a in json.load(sys.stdin)) else 'false')" "$declared_account"))
  46. if [ "$in_list" != "true" ]; then
  47. echo "auth-probe: \$CLOUDFLARE_ACCOUNT_ID ($declared_account) is not one of the token's accounts." >&2
  48. echo "auth-probe: Either unset \$CLOUDFLARE_ACCOUNT_ID, or set it to an account included in the token's Account Resources." >&2
  49. emit "{\"status\":\"account_mismatch\",\"declared\":\"$declared_account\",\"accounts\":$accounts_json}"
  50. fi
  51. account_id="$declared_account"
  52. elif [ "$account_count" = "1" ]; then
  53. account_id=$(echo "$accounts_json" | (jq -r '.[0].id' 2>/dev/null || python3 -c "import sys,json; print(json.load(sys.stdin)[0]['id'])"))
  54. else
  55. echo "auth-probe: token covers $account_count accounts; ask the user to pick one, then export \$CLOUDFLARE_ACCOUNT_ID and re-run." >&2
  56. emit "{\"status\":\"multiple_accounts\",\"accounts\":$accounts_json}"
  57. fi
  58. # Probe Turnstile scope on the selected account
  59. tmp=$(mktemp)
  60. http_code=$(curl -sS -w "%{http_code}" -o "$tmp" \
  61. "https://api.cloudflare.com/client/v4/accounts/$account_id/challenges/widgets" \
  62. -H "Authorization: Bearer $token" 2>/dev/null || echo "000")
  63. body=$(cat "$tmp"); rm -f "$tmp"
  64. success=$(echo "$body" | (jq -r '.success' 2>/dev/null || echo "false"))
  65. if [ "$success" != "true" ]; then
  66. echo "auth-probe: token cannot read /challenges/widgets on account $account_id (HTTP $http_code). Missing Account.Turnstile:Edit." >&2
  67. emit "{\"status\":\"missing_scope\",\"account_id\":\"$account_id\",\"http_code\":$http_code}"
  68. fi
  69. # Probe Workers scope on the selected account. GET /workers/scripts requires
  70. # Account.Workers Scripts:Read, which is a best-effort proxy for Edit. Tokens
  71. # granted Edit-only (without Read) will fail this probe and emit a confusing
  72. # missing_workers_scope; the agent should suggest adding Read alongside Edit.
  73. tmp=$(mktemp)
  74. workers_code=$(curl -sS -w "%{http_code}" -o "$tmp" \
  75. "https://api.cloudflare.com/client/v4/accounts/$account_id/workers/scripts" \
  76. -H "Authorization: Bearer $token" 2>/dev/null || echo "000")
  77. workers_body=$(cat "$tmp"); rm -f "$tmp"
  78. workers_success=$(echo "$workers_body" | (jq -r '.success' 2>/dev/null || echo "false"))
  79. if [ "$workers_success" != "true" ]; then
  80. echo "auth-probe: token cannot read /workers/scripts on account $account_id (HTTP $workers_code). Missing Account.Workers Scripts:Edit." >&2
  81. emit "{\"status\":\"missing_workers_scope\",\"account_id\":\"$account_id\",\"http_code\":$workers_code}"
  82. fi
  83. emit "{\"status\":\"ok\",\"account_id\":\"$account_id\",\"accounts\":$accounts_json}"