b3_credentials_check.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. usage() {
  4. cat <<'USAGE'
  5. Usage:
  6. Add B3_CLIENT_ID and B3_CLIENT_SECRET to the project .env file, then run:
  7. ./test/b3_credentials_check.sh
  8. The script:
  9. 1. Loads credentials from ../.env
  10. 2. Requests an OAuth token via Client Credentials (prod URL)
  11. 3. Calls the prod healthcheck endpoint with the token
  12. Both endpoints are hardcoded to the production environment.
  13. USAGE
  14. }
  15. SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
  16. PROJECT_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
  17. ENV_FILE="$PROJECT_ROOT/.env"
  18. parse_env_value() {
  19. local key="$1"
  20. if [[ ! -f "$ENV_FILE" ]]; then
  21. return
  22. fi
  23. sed -n "s/^${key}=//p" "$ENV_FILE" | tail -n1 | tr -d '\r'
  24. }
  25. B3_CLIENT_ID=$(parse_env_value "B3_CLIENT_ID")
  26. B3_CLIENT_SECRET=$(parse_env_value "B3_CLIENT_SECRET")
  27. if [[ -z "$B3_CLIENT_ID" || -z "$B3_CLIENT_SECRET" ]]; then
  28. echo "[error] B3_CLIENT_ID and B3_CLIENT_SECRET must be defined in .env" >&2
  29. usage
  30. exit 1
  31. fi
  32. B3_AUTH_URL="https://apib3i.b3.com.br/auth/oauth/v2/token"
  33. B3_HEALTHCHECK_URL="https://apib3i.b3.com.br/api/healthcheck/{token}"
  34. echo "[info] Requesting OAuth token from $B3_AUTH_URL"
  35. TOKEN_RESPONSE=$(curl --silent --show-error --fail \
  36. -u "$B3_CLIENT_ID:$B3_CLIENT_SECRET" \
  37. -H "Content-Type: application/x-www-form-urlencoded" \
  38. -d 'grant_type=client_credentials' \
  39. "$B3_AUTH_URL")
  40. TOKEN=$(echo "$TOKEN_RESPONSE" | jq -r '.access_token // empty')
  41. if [[ -z "$TOKEN" ]]; then
  42. echo "[error] Failed to extract access_token from response:" >&2
  43. echo "$TOKEN_RESPONSE" >&2
  44. exit 1
  45. fi
  46. echo "[info] Token retrieved successfully"
  47. HEALTH_URL=${B3_HEALTHCHECK_URL//\{token\}/$TOKEN}
  48. echo "[info] Calling healthcheck at $HEALTH_URL"
  49. HEALTH_RESPONSE=$(curl --silent --show-error --fail "$HEALTH_URL")
  50. echo "[info] Healthcheck response:"
  51. echo "$HEALTH_RESPONSE" | jq .