Docs: Index · 文档索引 · Implementation Plan
Build a self-hosted gateway that accepts OpenAI-style API requests from downstream clients while authenticating upstream requests with Codex OAuth tokens. Downstream access control uses a single fixed API key stored in runtime configuration.
- Implement interactive CLI login for Codex OAuth.
- Store all runtime artifacts in the service run directory.
- Expose OpenAI-compatible endpoints:
GET /v1/modelsPOST /v1/chat/completions(including streaming pass-through)
- Validate downstream
Authorization: Bearer <fixed_key>from config.
All files are rooted in process workdir (cwd) unless --workdir overrides it.
./config.yaml: gateway config, fixed downstream key, upstream URL, OAuth endpoints../oauth-token.json: OAuth access/refresh token cache../logs/(optional): structured logs.
Single Go binary with two command groups:
codex-gateway auth login: runs interactive OAuth device flow and saves tokens.codex-gateway serve: starts HTTP server with OpenAI-compatible API.
Core components:
- Config loader: reads and validates
config.yaml. - Token store: reads/writes token JSON in runtime directory.
- OAuth client: device authorization + token polling + refresh.
- Token manager: ensures valid access token before upstream calls.
- Upstream client: sends proxied OpenAI requests to upstream base URL.
- HTTP server: auth middleware, endpoints, health check, error mapping.
- Operator runs
codex-gateway auth login. - CLI requests a device code from OAuth device authorization endpoint.
- CLI prints verification URL and user code for browser-based confirmation.
- CLI polls token endpoint until success or terminal error.
- CLI writes token data to
./oauth-token.json.
- Client calls gateway with OpenAI-compatible request + fixed bearer key.
- Middleware checks bearer token against configured fixed key.
- Handler asks token manager for valid upstream access token.
- Gateway forwards request to upstream endpoint with OAuth bearer token.
- Gateway relays response body/status to client (JSON or SSE stream).
- Before each upstream call, token manager checks expiration buffer.
- If near expiry, it refreshes via OAuth token endpoint.
- On success, refreshed token is persisted atomically.
- On refresh failure, request returns actionable error requiring
auth login.
401 Unauthorized: downstream fixed API key missing/invalid.503 Service Unavailable: OAuth token absent or refresh failed.502 Bad Gateway: upstream request failed or returned unusable response.
All error responses follow OpenAI-style shape:
{
"error": {
"message": "human readable",
"type": "gateway_error",
"code": "string_code"
}
}- Token file permissions should be user-only when possible.
- Never log access_token, refresh_token, or downstream fixed API key.
- Keep config/token in private server directory with restricted ownership.
- Unit tests for config validation and token persistence.
- Unit tests for auth middleware behavior.
- HTTP integration tests using
httptestfor proxy endpoints. - OAuth refresh tests with fake OAuth server endpoints.
- Full OpenAI surface (embeddings/audio/images/fine-tuning).
- Multi-tenant key management.
- Distributed token storage.