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 |
|---|---|---|---|
anthropic | api_key | copy | none. Subscription credentials are refused. |
| 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/. 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 |
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 (state/private_files._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. |