OpenAI 认证
使用 OpenAI models 时,Codex 支持两种登录方式:
使用 ChatGPT 登录,以使用 subscription access。
使用 API key 登录,以使用 usage-based access。
Codex cloud 要求使用 ChatGPT 登录。Codex CLI 和 IDE extension 同时支持这两种登录方式。
你的登录方式也会决定适用哪些 admin controls 和 data-handling policies。
使用 ChatGPT 登录时,Codex usage 会遵循你的 ChatGPT workspace permissions、RBAC,以及 ChatGPT Enterprise retention 和 residency settings。
使用 API key 时,usage 会改为遵循你的 API organization retention 和 data-sharing settings。
对于 CLI,在没有有效 session 时,Sign in with ChatGPT 是默认 authentication path。
使用 ChatGPT 登录
当你从 Codex app、CLI 或 IDE Extension 使用 ChatGPT 登录时,Codex 会打开浏览器窗口,让你完成登录流程。登录完成后,浏览器会把 access token 返回给 CLI 或 IDE extension。
如果你的环境已经提供 ChatGPT access token,CLI 可以从 stdin 读取:
printenv CODEX_ACCESS_TOKEN | codex login --with-access-token 使用 API key 登录
你也可以使用 API key 登录 Codex app、CLI 或 IDE Extension。请从 OpenAI dashboard 获取 API key。
OpenAI 会通过你的 OpenAI Platform account 按 standard API rates 计费 API key usage。请参阅 API pricing page 。
API key authentication 支持 local Codex workflows,但依赖 ChatGPT workspace access 或 cloud services 的部分 features 会受限或不可用。请在 Feature availability 中按 plan 对比支持情况。
使用 API key 登录时,Codex 使用 standard API pricing,而不是 ChatGPT plan 中包含的 credits。
OpenAI 建议在 programmatic Codex CLI workflows 中使用 API key authentication,例如 CI/CD jobs。不要把 Codex execution 暴露在 untrusted 或 public environments 中。
为 enterprise automation 使用 Codex access tokens
在 ChatGPT Enterprise workspaces 中,admins 可以授予 access token permission,让被允许的成员为 trusted、non-interactive Codex local workflows 创建 Codex access tokens。当 automation 需要 ChatGPT workspace access、ChatGPT-managed Codex entitlements,或无需浏览器登录也要使用 enterprise workspace controls 时,请使用 access token。
Access tokens 面向 trusted scripts、schedulers 和 private CI runners。一般 OpenAI API calls 仍应继续使用 Platform API keys。
有关 setup steps、permissions、rotation 和 revocation guidance,请参阅 Access tokens 。
保护你的 Codex cloud account
Codex cloud 会直接与你的 codebase 交互,因此它比许多其他 ChatGPT features 需要更强的安全性。请启用 multi-factor authentication (MFA)。
如果你使用 social login provider(Google、Microsoft、Apple),你不需要在 ChatGPT account 上启用 MFA,但可以通过对应的 social login provider 设置 MFA。
如果你通过 single sign-on (SSO) 访问 ChatGPT,你所在 organization 的 SSO administrator 应该为所有 users 强制启用 MFA。
如果你使用 email 和 password 登录,在访问 Codex cloud 前,必须先在 account 上设置 MFA。
如果你的 account 支持多种登录方式,并且其中一种是 email 和 password,那么即使你用另一种方式登录,也必须先设置 MFA 才能访问 Codex。
Login caching
当你使用 ChatGPT 或 API key 登录 Codex app、CLI 或 IDE Extension 时,Codex 会缓存你的 login details,并在下次启动 CLI 或 extension 时复用。CLI 和 extension 共享同一份 cached login details。如果你从其中任一处退出登录,下次启动 CLI 或 extension 时都需要重新登录。
Codex 会在 ~/.codex/auth.json 这个 plaintext file 中本地缓存 login details,或缓存到你的 OS-specific credential store 中。
对于 Sign in with ChatGPT sessions,Codex 会在使用过程中、token 过期前自动 refresh tokens,因此 active sessions 通常不需要再次 browser login。
Credential storage
使用 cli_auth_credentials_store 控制 Codex CLI 将 cached credentials 存储在哪里:
# file | keyring | auto
cli_auth_credentials_store = "keyring" file 会把 credentials 存储在 CODEX_HOME 下的 auth.json 中;CODEX_HOME 默认为 ~/.codex。
keyring 会把 credentials 存储在你的 operating system credential store 中。
auto 会在可用时使用 OS credential store,否则 fallback 到 auth.json。
如果使用 file-based storage,请像对待 password 一样对待 ~/.codex/auth.json:它包含 access tokens。不要提交它,不要粘贴到 tickets 中,也不要在 chat 中分享。
强制登录方式或 workspace
在 managed environments 中,admins 可以限制 users 被允许使用哪种方式 authenticate:
# Only allow ChatGPT login or only allow API key login.
forced_login_method = "chatgpt" # or "api"
# When using ChatGPT login, restrict users to a specific workspace.
forced_chatgpt_workspace_id = "00000000-0000-0000-0000-000000000000" 如果 active credentials 与配置的 restrictions 不匹配,Codex 会让 user 退出登录并退出。
这些 settings 通常通过 Managed configuration 应用,而不是作为 per-user setup。请参阅 Managed configuration 。
Login diagnostics
直接运行 codex login 会在你配置的 log directory 下写入专用 codex-login.log file。当你需要 debug browser-login 或 device-code failures,或 support 要求提供 login-specific logs 时,请使用它。
Custom CA bundles
如果你的 network 使用 corporate TLS proxy 或 private root CA,请在登录前将 CODEX_CA_CERTIFICATE 设置为 PEM bundle。未设置 CODEX_CA_CERTIFICATE 时,Codex 会 fallback 到 SSL_CERT_FILE。同一组 custom CA settings 适用于 login、常规 HTTPS requests 和 secure WebSocket connections。
export CODEX_CA_CERTIFICATE=/path/to/corporate-root-ca.pem
codex login 在 headless devices 上登录
如果你正在通过 Codex CLI 登录 ChatGPT,存在一些 browser-based login UI 可能无法工作的情况:
你正在 remote 或 headless environment 中运行 CLI。
你的 local networking configuration 阻止了 Codex 在你登录后用于把 OAuth token 返回给 CLI 的 localhost callback。
在这些情况下,优先使用 device code authentication (beta)。在 interactive login UI 中选择 Sign in with Device Code,或直接运行 codex login --device-auth。如果 device code authentication 在你的环境中无法工作,请使用下面的 fallback methods。
首选:Device code authentication (beta)
在 ChatGPT security settings(personal account)或 ChatGPT workspace permissions(workspace admin)中启用 device code login。
在运行 Codex 的 terminal 中,选择以下一种方式:
在 interactive login UI 中选择 Sign in with Device Code。
运行 codex login --device-auth。
在浏览器中打开链接,完成登录,然后输入 one-time code。
如果 server 未启用 device code login,Codex 会 fallback 到 standard browser-based login flow。
Fallback:本地认证并复制 auth cache
如果你可以在有浏览器的机器上完成 login flow,就可以把 cached credentials 复制到 headless machine。
在可以使用 browser-based login flow 的机器上运行 codex login。
确认 login cache 存在于 ~/.codex/auth.json。
将 ~/.codex/auth.json 复制到 headless machine 上的 ~/.codex/auth.json。
请像对待 password 一样对待 ~/.codex/auth.json:它包含 access tokens。不要提交它,不要粘贴到 tickets 中,也不要在 chat 中分享。
如果你的 OS 将 credentials 存储在 credential store 中,而不是 ~/.codex/auth.json,这种方法可能不适用。关于如何配置 file-based storage,请参阅 Credential storage 。
通过 SSH 复制到 remote machine:
ssh user@remote 'mkdir -p ~/.codex'
scp ~/.codex/auth.json user@remote:~/.codex/auth.json 或者使用避免 scp 的 one-liner:
ssh user@remote 'mkdir -p ~/.codex && cat > ~/.codex/auth.json' < ~/.codex/auth.json 复制到 Docker container 中:
# Replace MY_CONTAINER with the name or ID of your container.
CONTAINER_HOME=$(docker exec MY_CONTAINER printenv HOME)
docker exec MY_CONTAINER mkdir -p "$CONTAINER_HOME/.codex"
docker cp ~/.codex/auth.json MY_CONTAINER:"$CONTAINER_HOME/.codex/auth.json" 关于在 trusted CI/CD runners 上使用同一模式的更高级版本,请参阅 Maintain Codex account auth in CI/CD (advanced) 。该指南说明如何让 Codex 在常规运行期间 refresh auth.json,并保留更新后的文件供下一个 job 使用。API keys 仍是 automation 的推荐默认方式。
Fallback:通过 SSH 转发 localhost callback
如果你可以在 local machine 和 remote host 之间 forward ports,就可以通过 tunneling Codex 的 local callback server(默认 localhost:1455)使用 standard browser-based flow。
从你的 local machine 启动 port forwarding:
ssh -L 1455:localhost:1455 user@remote 在该 SSH session 中运行 codex login,并在 local machine 上按打印出的地址继续操作。
Alternative model providers
当你在 configuration file 中定义 custom model provider 时,可以选择以下 authentication methods:
OpenAI authentication:设置 requires_openai_auth = true 以使用 OpenAI authentication。然后你可以使用 ChatGPT 或 API key 登录。当你通过 LLM proxy server 访问 OpenAI models 时,这会很有用。requires_openai_auth = true 时,Codex 会忽略 env_key。
Environment variable authentication:设置 env_key = "<ENV_VARIABLE_NAME>",以使用名为 <ENV_VARIABLE_NAME> 的 local environment variable 中的 provider-specific API key。
No authentication:如果你未设置 requires_openai_auth(或将其设为 false),并且也未设置 env_key,Codex 会假定该 provider 不需要 authentication。这适用于 local models。