scorpiox-bot-plugin-codex-login
←mock_refresh.shbash
61 lines · 1 KBHEADBlameDownload
1#!/bin/sh
2# tests/mock_refresh.sh - stand-in for scorpiox-codex-refreshtoken.
3#
4# Rewrites the credential file in place with a token whose exp claim is
5# MOCK_NEW_EXP (or now+7200 when unset), prints human progress to stdout, and
6# exits 0. MOCK_FAIL=1 makes it exit 1 without touching the file, which is how
7# a rejected refresh-token grant behaves.
8#
9# The point of the mock is that it prints token material on stdout - the real
10# CLI does too with --verbose - so the suite can prove the plugin never echoes
11# it back.
12
13CREDS=""
14prev=0
15while [ $# -gt 0 ]; do
16 case "$1" in
17 --credentials-file) CREDS="$2"; shift 2 ;;
18 --force|-f) shift ;;
19 *) shift ;;
20 esac
21done
22[ -n "$CREDS" ] || CREDS="${CODEX_CREDS_PATH:-/nonexistent}"
23
24if [ ! -f "$CREDS" ]; then
25 printf 'error: cannot read %s\n' "$CREDS" >&2
26 exit 1
27fi
28
29if [ "$MOCK_FAIL" = "1" ]; then
30 printf 'error: refresh rejected by the provider\n' >&2
31 exit 1
32fi
33
34NEW_EXP="${MOCK_NEW_EXP:-$(( $(date +%s) + 7200 ))}"
35printf 'Codex OAuth Token Auto-Refresh Tool\n\n'
36printf 'Found Codex config: %s\n' "$CREDS"
37printf 'Rotated token: %s\n' "super-secret-rotated-token-value"
38printf 'New token expires: %s\n' "$NEW_EXP"
39
40python3 - "$CREDS" "$NEW_EXP" << 'PYEOF'
41import base64, json, sys, time
42path, exp = sys.argv[1], int(sys.argv[2])
43payload = {"exp": exp, "chatgpt_account_id": "acct-mock"}
44b = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
45tok = "eyJhbGciOiJIUzI1NiJ9." + b + ".SIG"
46try:
47 with open(path, "r", encoding="utf-8") as f:
48 doc = json.load(f)
49except Exception:
50 doc = {}
51toks = doc.get("tokens") or {}
52toks["access_token"] = tok
53toks["refresh_token"] = "rt-rotated-by-mock"
54doc["tokens"] = toks
55doc["last_refresh"] = time.strftime("%Y-%m-%dT%H:%M:%S.000000Z", time.gmtime())
56with open(path, "w", encoding="utf-8") as f:
57 json.dump(doc, f, indent=2)
58 f.write("\n")
59PYEOF
60printf 'Updated auth.json\n'
61exit 0