Auth storage
AuthStore (models/auth/store.py): ~/.openprogram/auth/<provider_id>/credentials.json, one ordered list per provider, atomic write, a cross-process lock held from read to write (mutate), reload when the file changes underneath. An API key's secret is in its company's file, auth/_keys/<company>.json, written the same way.
What requests count (uses, last use, uses today, the rotation cursor) lives beside it in usage.json, so a request never rewrites the secrets file. CredentialProvider serves what is on disk and never re-discovers; import is an explicit step.
| Provider | Login method | Storage | Refresh |
|---|---|---|---|
openai-codex | pkce_oauth, or a copy of ~/.codex/auth.json | copy, or delegated pointer | OpenProgram through ~/.codex/auth.json (see Refresh) |
github-copilot | device_code | copy | OpenProgram |
xai-subscription | pkce_oauth, import_from_cli (~/.grok/auth.json), paste_token | store | per method |
anthropic | api_key | copy | none. Subscription credentials are refused. |
gemini-subscription | import_from_cli (~/.gemini/oauth_creds.json) | delegated pointer | external CLI; gemini auth login when stale |
| other key providers | api_key | copy | none |
Bedrock and Vertex use the cloud SDK's own chain. qwen has a credential source but no runtime, so no login is offered. claude-code and related ids are unsupported.
Credential kinds
kind | Secret storage |
|---|---|
api_key | copy of the key |
oauth, device_code | copy: access, refresh, expires_at_ms, client id, token endpoint |
import | pointer only: store_path and key paths, re-read each use |
credential_process | argv run on demand |
methods/cli_import.py has mode="link" (default) and mode="copy", which builds a writable store-owned oauth credential.
Login registry
auth/login/login_method_registry.py is the one provider → [method] table; the first entry is the default, and the CLI, web and TUI all render it. Handlers are in auth/methods/ and auth/login/. Google's Code-Assist OAuth embeds a client secret OpenProgram cannot ship, so that provider stays delegated; Anthropic has no published third-party OAuth client.
Each method carries a title and a one-line description in English and Chinese, and an import names the file it reads; the web setup dialog shows them as its list of ways in.
Refresh
A refresh token rotates: the first holder to spend it gets a new pair, and any later spend of the old one is refused (invalid_grant), which signs that holder out. So a refresh is spent by one process at a time, and each process re-reads the login after it gets the lock.
| Rule | Behaviour |
|---|---|
| Before sending | CredentialProvider.prepare refreshes a login whose expires_at_ms is within 60 s; the request goes out with a token that has not expired |
| Cross-process lock | refresh_lock.py: an advisory lock on <file>.refresh.lock beside the provider's list, separate from its write lock. Two workers, or a worker and its cron subprocess, take turns |
| Inside the lock | Re-read the file. A login another process refreshed meanwhile is used as is; only a still-stale one is refreshed and saved |
| Rejected before expiry | An HTTP 401 refreshes that login once and sends the same request again at once, without a transport retry. One INFO line records the refresh |
One ChatGPT login per machine
A ChatGPT sign-in is one refresh-token family, and on a machine it lives in the Codex CLI's ~/.codex/auth.json (or $CODEX_HOME). The Codex CLI refreshes it, an import credential reads it (the development instance has only this one), and an OpenProgram oauth login of the same account keeps a copy. All of them take that file's refresh lock.
| Holder | Refresh |
|---|---|
import | Reads the file for every request. When the token's own exp is within 60 s, or the backend answers 401, it spends the file's refresh token and writes the new tokens back to the file, as the Codex CLI does |
oauth copy | Before each request, adopts a later login of the same account found in the file and saves it to the store. When it must refresh, it first re-reads the file under the lock and adopts a later login there instead of spending a refresh token another holder already spent; otherwise it refreshes and mirrors the result into the file |
| Different account in the file | Neither adopted nor overwritten |
| Writing the file | Created owner-only (0600), synced to disk, then renamed over the file: for a delegated reader it holds the only copy of the rotated refresh token. A write that fails is logged as a warning naming the path, never a token |
| Saving an adoption | Only while the store still holds the token it was adopted over. A refresh that saved a newer login in between keeps it |
The credential layer makes the chosen login ready (prepare) in a worker thread, never on the event loop, since it can wait for another process's refresh lock and a refresh request.
Unreadable is not empty
| Step | Behaviour |
|---|---|
Write (credentials/io._atomic_write) | Keeps the replaced file as <file>.bak when it is valid JSON (state/last_good.keep_previous) |
Read of invalid JSON (last_good.load_json) | Copies the bytes once to <file>.bad-<UTC time>-<digest>, answers the valid .bak, else nothing |
| Users | read_config, update_config, the auth store |
| A key removed or replaced | Its list's .bak and .bad- copies are deleted too, so the old key leaves the disk |
Backups (operations/backup.py) | Leave out .bak and .bad- copies, so redaction cannot be bypassed |
Follows magpie's lastgood (yetone/magpie, MIT).
Peers
| Project | Design |
|---|---|
| opencode | Own PKCE and auth.json, no adoption. provider → AuthHook with method:"auto" or "code"; on-demand single-flight refresh. Anthropic and Google are API key only. |
| openclaw | auth-profiles.json by <provider>:<label>; rotation state in a sibling file; oauth, api_key, token; refresh under a cross-process lock that re-reads inside the lock (the part worth copying); one handler picks browser or paste. Reads other CLIs' files directly, which this design does not. |
Implementation status
Not in place: copying the Gemini subscription token into the store as the default import.