]> git.giacomolli.uk - onechat/commit
Log in to ChatGPT and refresh the tokens
authorAnderson A. Giacomolli <agiacomolli@gmail.com>
Fri, 6 Nov 2026 17:29:10 +0000 (14:29 -0300)
committerAnderson A. Giacomolli <agiacomolli@gmail.com>
Fri, 6 Nov 2026 17:29:10 +0000 (14:29 -0300)
commit9b1705c5647864ef482078e1e169d6dfeade98b6
treef1162673a5bac3eddb1d9167ff642d945eb98b52
parent7b5d616eaddaee5fa8f73dea9a6593b27c06248e
Log in to ChatGPT and refresh the tokens

onechat login runs the device code login of the Codex client. It shows
the link and the code, polls until the user approves the code on any
device, trades the grant for tokens, reads the account ID from the auth
claim of the token, and writes auth.json in the data directory with mode
0600. The command needs no browser on the machine.

Before each call to the ChatGPT backend, the authorizer reads the expiry
from the access token. Within 60 seconds of the expiry it refreshes the
tokens, keeps a new refresh token when one comes, and replaces the file
atomically. A mutex lets one refresh run at a time. A missing file or a
failed refresh gives errLoginNeeded and leaves the file as it is. Errors
hold the HTTP status but never an answer body, so no token reaches a
log.

The endpoint in config.json selects the ChatGPT backend with this
authorizer or the OpenAI API with OPENAI_API_KEY, with no fallback from
one to the other. OneChat keeps its own token file, because the Codex
CLI rotates the same refresh token and two programs with one file log
each other out.

Issue: b14d537
README.rst
auth.go [new file with mode: 0644]
auth_test.go [new file with mode: 0644]
main.go
responses.go