| 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 | |
| 13 | CREDS="" |
| 14 | prev=0 |
| 15 | while [ $# -gt 0 ]; do |
| 16 | case "$1" in |
| 17 | --credentials-file) CREDS="$2"; shift 2 ;; |
| 18 | --force|-f) shift ;; |
| 19 | *) shift ;; |
| 20 | esac |
| 21 | done |
| 22 | [ -n "$CREDS" ] || CREDS="${CODEX_CREDS_PATH:-/nonexistent}" |
| 23 | |
| 24 | if [ ! -f "$CREDS" ]; then |
| 25 | printf 'error: cannot read %s\n' "$CREDS" >&2 |
| 26 | exit 1 |
| 27 | fi |
| 28 | |
| 29 | if [ "$MOCK_FAIL" = "1" ]; then |
| 30 | printf 'error: refresh rejected by the provider\n' >&2 |
| 31 | exit 1 |
| 32 | fi |
| 33 | |
| 34 | NEW_EXP="${MOCK_NEW_EXP:-$(( $(date +%s) + 7200 ))}" |
| 35 | printf 'Codex OAuth Token Auto-Refresh Tool\n\n' |
| 36 | printf 'Found Codex config: %s\n' "$CREDS" |
| 37 | printf 'Rotated token: %s\n' "super-secret-rotated-token-value" |
| 38 | printf 'New token expires: %s\n' "$NEW_EXP" |
| 39 | |
| 40 | python3 - "$CREDS" "$NEW_EXP" << 'PYEOF' |
| 41 | import base64, json, sys, time |
| 42 | path, exp = sys.argv[1], int(sys.argv[2]) |
| 43 | payload = {"exp": exp, "chatgpt_account_id": "acct-mock"} |
| 44 | b = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=") |
| 45 | tok = "eyJhbGciOiJIUzI1NiJ9." + b + ".SIG" |
| 46 | try: |
| 47 | with open(path, "r", encoding="utf-8") as f: |
| 48 | doc = json.load(f) |
| 49 | except Exception: |
| 50 | doc = {} |
| 51 | toks = doc.get("tokens") or {} |
| 52 | toks["access_token"] = tok |
| 53 | toks["refresh_token"] = "rt-rotated-by-mock" |
| 54 | doc["tokens"] = toks |
| 55 | doc["last_refresh"] = time.strftime("%Y-%m-%dT%H:%M:%S.000000Z", time.gmtime()) |
| 56 | with open(path, "w", encoding="utf-8") as f: |
| 57 | json.dump(doc, f, indent=2) |
| 58 | f.write("\n") |
| 59 | PYEOF |
| 60 | printf 'Updated auth.json\n' |
| 61 | exit 0 |