#!/usr/bin/env bash # ConsoleAI agent login (RFC 8628 device code). Prints a short code + link, waits for the owner to approve it, # then saves this agent's ConsoleAI MCP token. The token is never printed. # curl -fsSL https://cws-314e2d28ca.hq.abuz8ai.com/connect/login.sh | bash -s -- OpenClaw # bash login.sh Hermes --config ~/.cursor/mcp.json (also merge into a JSON MCP config: mcpServers.consoleai) # Needs: curl, python3. Writes: ~/.consoleai/.token and ~/.consoleai/.mcp.json (both chmod 600). set -euo pipefail BASE="${CONSOLEAI_URL:-https://cws-314e2d28ca.hq.abuz8ai.com}" NAME="${1:-}"; [ $# -gt 0 ] && shift CONFIG="" while [ $# -gt 0 ]; do case "$1" in --config) CONFIG="${2:-}"; shift 2 || true;; *) echo "unknown option: $1" >&2; exit 2;; esac; done if [ -z "$NAME" ]; then echo "usage: login.sh [--config path/to/mcp.json]" >&2; exit 2; fi command -v curl >/dev/null || { echo "needs curl" >&2; exit 2; } PY="$(command -v python3 || true)"; [ -n "$PY" ] || { echo "needs python3" >&2; exit 2; } jf(){ "$PY" -c 'import sys,json try: d=json.load(sys.stdin) except Exception: d={} v=d.get(sys.argv[1],""); print(v if v is not None else "")' "$1"; } R="$(curl -sS -X POST "$BASE/device_authorization" --data-urlencode "client_name=$NAME")" DC="$(printf '%s' "$R" | jf device_code)"; UC="$(printf '%s' "$R" | jf user_code)" if [ -z "$DC" ]; then echo "ConsoleAI refused: $(printf '%s' "$R" | jf error_description)" >&2; exit 1; fi VU="$(printf '%s' "$R" | jf verification_uri)"; VC="$(printf '%s' "$R" | jf verification_uri_complete)" IV="$(printf '%s' "$R" | jf interval)"; EX="$(printf '%s' "$R" | jf expires_in)"; IV="${IV:-5}"; EX="${EX:-600}" CID="$(printf '%s' "$R" | jf client_id)" echo "" echo " ConsoleAI login for: $NAME" echo " 1. On your phone or computer open: $VC" echo " (or open $VU and type the code)" echo " 2. Sign in with your owner Google account." echo " 3. Check the code matches, then tap Approve: $UC" echo "" echo " Waiting (code expires in $((EX/60)) minutes)..." CLIENT_ID="cai-device-$(printf '%s' "$NAME" | tr 'A-Z' 'a-z' | sed -E 's/[^a-z0-9]+/-/g; s/^-+|-+$//g' | cut -c1-40)" [ -n "$CID" ] && CLIENT_ID="$CID" END=$(( $(date +%s) + EX )); OK=0 while [ "$(date +%s)" -lt "$END" ]; do sleep "$IV" T="$(curl -sS -X POST "$BASE/token" --data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:device_code" --data-urlencode "device_code=$DC" --data-urlencode "client_id=$CLIENT_ID")" ERR="$(printf '%s' "$T" | jf error)" case "$ERR" in authorization_pending) continue;; slow_down) IV=$((IV+5)); continue;; "") OK=1; break;; *) echo "Login stopped: $ERR — $(printf '%s' "$T" | jf error_description)" >&2; exit 1;; esac done [ "$OK" = 1 ] || { echo "Login timed out. Run it again." >&2; exit 1; } SEAT="$(printf '%s' "$T" | jf seat)"; DAYS=$(( $(printf '%s' "$T" | jf expires_in) / 86400 )) mkdir -p "$HOME/.consoleai"; chmod 700 "$HOME/.consoleai"; umask 077 printf '%s' "$T" | MCP="$BASE/mcp" OUT="$HOME/.consoleai/$SEAT" CFG="$CONFIG" "$PY" -c ' import sys, json, os t = json.load(sys.stdin); tok = t["access_token"]; mcp = os.environ["MCP"]; out = os.environ["OUT"] entry = {"type": "http", "url": mcp, "headers": {"Authorization": "Bearer " + tok}} open(out + ".token", "w").write(tok) json.dump({"mcpServers": {"consoleai": entry}}, open(out + ".mcp.json", "w"), indent=2) cfg = os.environ.get("CFG") if cfg: cfg = os.path.expanduser(cfg) d = {} if os.path.exists(cfg): d = json.load(open(cfg)); open(cfg + ".bak-consoleai", "w").write(json.dumps(d, indent=2)) d.setdefault("mcpServers", {})["consoleai"] = entry json.dump(d, open(cfg, "w"), indent=2) ' echo "" echo " Approved. Seat: $SEAT (member). Token saved (not shown), valid about $DAYS days:" echo " $HOME/.consoleai/$SEAT.token" echo " $HOME/.consoleai/$SEAT.mcp.json (MCP server entry: $BASE/mcp)" [ -n "$CONFIG" ] && echo " merged into $CONFIG as mcpServers.consoleai (backup: $CONFIG.bak-consoleai)" echo " Restart the agent so it reloads its MCP servers."