mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
[codex][device-stable-id][12/n] Send source stable ID with managed auth requests
This commit is contained in:
@@ -1881,6 +1881,8 @@ Codex supports these authentication modes. The current mode is surfaced in `acco
|
||||
- **ChatGPT managed (`chatgpt`)** (recommended): Codex owns the ChatGPT OAuth flow and refresh tokens. Start via `account/login/start` with `type: "chatgpt"` for the browser flow or `type: "chatgptDeviceCode"` for device code; Codex persists tokens to disk and refreshes them automatically.
|
||||
- **Personal access token (`personalAccessToken`)**: Codex uses a ChatGPT-backed personal access token loaded outside the app-server login RPCs, such as with `codex login --with-access-token` or `CODEX_ACCESS_TOKEN`.
|
||||
|
||||
Single-client stdio clients may include `sourceSurfaceStableId` on `account/login/start` and `account/read`. Codex forwards the current value on authorize, code-exchange, and refresh requests. Remote-control connections to that process inherit the value but cannot replace it; standalone shared and in-process transports use Codex's installation ID instead. The value is optional experiment-analysis context and does not affect authentication decisions.
|
||||
|
||||
### API Overview
|
||||
|
||||
- `account/read` — fetch current account info; optionally refresh tokens.
|
||||
@@ -1919,6 +1921,7 @@ Response examples:
|
||||
Field notes:
|
||||
|
||||
- `refreshToken` (bool): set `true` to force a token refresh.
|
||||
- `sourceSurfaceStableId` (string, optional): updates the value Codex forwards on later managed-auth requests. Send the same value on `account/login/start` so the authorize and code-exchange requests match.
|
||||
- `requiresOpenaiAuth` reflects the active provider; when `false`, Codex can run without OpenAI credentials.
|
||||
- Amazon Bedrock reports `credentialSource: "codexManaged"` when it uses a Bedrock API key managed by Codex. Otherwise it reports `credentialSource: "awsManaged"` for the external AWS credential path. This identifies the selected credential source; it does not validate that the AWS credential chain can resolve credentials.
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ use crate::outgoing_message::OutgoingMessage;
|
||||
use crate::outgoing_message::OutgoingMessageSender;
|
||||
use crate::outgoing_message::QueuedOutgoingMessage;
|
||||
use crate::transport::CHANNEL_CAPACITY;
|
||||
use crate::transport::ConnectionOrigin;
|
||||
use crate::transport::OutboundConnectionState;
|
||||
use crate::transport::route_outgoing_envelope;
|
||||
use codex_analytics::AppServerRpcTransport;
|
||||
@@ -382,6 +383,7 @@ async fn start_uninitialized(args: InProcessStartArgs) -> IoResult<InProcessClie
|
||||
let auth_manager =
|
||||
AuthManager::shared_from_config(args.config.as_ref(), args.enable_codex_api_key_env)
|
||||
.await;
|
||||
auth_manager.set_source_surface_stable_id(Some(installation_id.clone()));
|
||||
let analytics_events_client =
|
||||
analytics_events_client_from_config(Arc::clone(&auth_manager), args.config.as_ref());
|
||||
let outgoing_message_sender = Arc::new(OutgoingMessageSender::new(
|
||||
@@ -442,7 +444,7 @@ async fn start_uninitialized(args: InProcessStartArgs) -> IoResult<InProcessClie
|
||||
plugin_startup_tasks: crate::PluginStartupTasks::Start,
|
||||
}));
|
||||
let mut thread_created_rx = processor.thread_created_receiver();
|
||||
let session = Arc::new(ConnectionSessionState::new());
|
||||
let session = Arc::new(ConnectionSessionState::new(ConnectionOrigin::InProcess));
|
||||
let mut listen_for_threads = true;
|
||||
|
||||
loop {
|
||||
|
||||
@@ -750,6 +750,9 @@ pub async fn run_main_with_transport_options(
|
||||
|
||||
let auth_manager =
|
||||
AuthManager::shared_from_config(&config, /*enable_codex_api_key_env*/ false).await;
|
||||
if !single_client_mode {
|
||||
auth_manager.set_source_surface_stable_id(Some(installation_id.clone()));
|
||||
}
|
||||
|
||||
let remote_control_enabled = remote_control_policy == RemoteControlPolicy::Allowed
|
||||
&& remote_control_explicitly_requested
|
||||
|
||||
@@ -46,6 +46,7 @@ use crate::skills_watcher::SkillsWatcher;
|
||||
use crate::thread_state::ConnectionCapabilities;
|
||||
use crate::thread_state::ThreadStateManager;
|
||||
use crate::transport::AppServerTransport;
|
||||
use crate::transport::ConnectionOrigin;
|
||||
use crate::transport::RemoteControlHandle;
|
||||
use codex_analytics::AnalyticsEventsClient;
|
||||
use codex_analytics::AppServerRpcTransport;
|
||||
@@ -213,6 +214,7 @@ pub(crate) struct MessageProcessor {
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ConnectionSessionState {
|
||||
pub(crate) rpc_gate: Arc<ConnectionRpcGate>,
|
||||
origin: ConnectionOrigin,
|
||||
initialized: OnceLock<InitializedConnectionSessionState>,
|
||||
}
|
||||
|
||||
@@ -225,16 +227,11 @@ pub(crate) struct InitializedConnectionSessionState {
|
||||
pub(crate) request_attestation: bool,
|
||||
}
|
||||
|
||||
impl Default for ConnectionSessionState {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl ConnectionSessionState {
|
||||
pub(crate) fn new() -> Self {
|
||||
pub(crate) fn new(origin: ConnectionOrigin) -> Self {
|
||||
Self {
|
||||
rpc_gate: Arc::new(ConnectionRpcGate::new()),
|
||||
origin,
|
||||
initialized: OnceLock::new(),
|
||||
}
|
||||
}
|
||||
@@ -884,6 +881,7 @@ impl MessageProcessor {
|
||||
let serialization_scope = codex_request.serialization_scope();
|
||||
let app_server_client_name = session.app_server_client_name().map(str::to_string);
|
||||
let client_version = session.client_version().map(str::to_string);
|
||||
let accepts_client_source_surface_stable_id = session.origin == ConnectionOrigin::Stdio;
|
||||
let error_request_id = connection_request_id.clone();
|
||||
let rpc_gate = Arc::clone(&session.rpc_gate);
|
||||
let processor = Arc::clone(self);
|
||||
@@ -899,6 +897,7 @@ impl MessageProcessor {
|
||||
request_context,
|
||||
app_server_client_name,
|
||||
client_version,
|
||||
accepts_client_source_surface_stable_id,
|
||||
)
|
||||
.await;
|
||||
if let Err(error) = result {
|
||||
@@ -928,6 +927,7 @@ impl MessageProcessor {
|
||||
request_context: RequestContext,
|
||||
app_server_client_name: Option<String>,
|
||||
client_version: Option<String>,
|
||||
accepts_client_source_surface_stable_id: bool,
|
||||
) -> Result<(), JSONRPCErrorError> {
|
||||
let connection_id = connection_request_id.connection_id;
|
||||
let request_id = ConnectionRequestId {
|
||||
@@ -1378,7 +1378,11 @@ impl MessageProcessor {
|
||||
}
|
||||
ClientRequest::LoginAccount { params, .. } => {
|
||||
self.account_processor
|
||||
.login_account(request_id.clone(), params)
|
||||
.login_account(
|
||||
request_id.clone(),
|
||||
params,
|
||||
accepts_client_source_surface_stable_id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
ClientRequest::LogoutAccount { .. } => {
|
||||
@@ -1390,7 +1394,9 @@ impl MessageProcessor {
|
||||
self.account_processor.cancel_login_account(params).await
|
||||
}
|
||||
ClientRequest::GetAccount { params, .. } => {
|
||||
self.account_processor.get_account(params).await
|
||||
self.account_processor
|
||||
.get_account(params, accepts_client_source_surface_stable_id)
|
||||
.await
|
||||
}
|
||||
ClientRequest::GetAuthStatus { params, .. } => {
|
||||
self.account_processor.get_auth_status(params).await
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::config_manager::ConfigManager;
|
||||
use crate::outgoing_message::ConnectionId;
|
||||
use crate::outgoing_message::OutgoingMessageSender;
|
||||
use crate::transport::AppServerTransport;
|
||||
use crate::transport::ConnectionOrigin;
|
||||
use anyhow::Result;
|
||||
use app_test_support::create_mock_responses_server_repeating_assistant;
|
||||
use app_test_support::write_mock_responses_config_toml;
|
||||
@@ -128,7 +129,7 @@ impl TracingHarness {
|
||||
_codex_home: codex_home,
|
||||
processor,
|
||||
outgoing_rx,
|
||||
session: Arc::new(ConnectionSessionState::new()),
|
||||
session: Arc::new(ConnectionSessionState::new(ConnectionOrigin::WebSocket)),
|
||||
tracing,
|
||||
};
|
||||
|
||||
|
||||
@@ -88,7 +88,23 @@ impl AccountRequestProcessor {
|
||||
&self,
|
||||
request_id: ConnectionRequestId,
|
||||
params: LoginAccountParams,
|
||||
accepts_client_source_surface_stable_id: bool,
|
||||
) -> Result<Option<ClientResponsePayload>, JSONRPCErrorError> {
|
||||
let source_surface_stable_id = match ¶ms {
|
||||
LoginAccountParams::Chatgpt {
|
||||
source_surface_stable_id,
|
||||
..
|
||||
}
|
||||
| LoginAccountParams::ChatgptDeviceCode {
|
||||
source_surface_stable_id,
|
||||
} => source_surface_stable_id.clone(),
|
||||
_ => None,
|
||||
};
|
||||
self.set_client_source_surface_stable_id(
|
||||
accepts_client_source_surface_stable_id,
|
||||
source_surface_stable_id,
|
||||
);
|
||||
|
||||
self.login_v2(request_id, params).await.map(|()| None)
|
||||
}
|
||||
|
||||
@@ -111,7 +127,13 @@ impl AccountRequestProcessor {
|
||||
pub(crate) async fn get_account(
|
||||
&self,
|
||||
params: GetAccountParams,
|
||||
accepts_client_source_surface_stable_id: bool,
|
||||
) -> Result<Option<ClientResponsePayload>, JSONRPCErrorError> {
|
||||
self.set_client_source_surface_stable_id(
|
||||
accepts_client_source_surface_stable_id,
|
||||
params.source_surface_stable_id.clone(),
|
||||
);
|
||||
|
||||
self.get_account_response(params)
|
||||
.await
|
||||
.map(|response| Some(response.into()))
|
||||
@@ -239,11 +261,12 @@ impl AccountRequestProcessor {
|
||||
}
|
||||
LoginAccountParams::Chatgpt {
|
||||
codex_streamlined_login,
|
||||
..
|
||||
} => {
|
||||
self.login_chatgpt_v2(request_id, codex_streamlined_login)
|
||||
.await;
|
||||
}
|
||||
LoginAccountParams::ChatgptDeviceCode => {
|
||||
LoginAccountParams::ChatgptDeviceCode { .. } => {
|
||||
self.login_chatgpt_device_code_v2(request_id).await;
|
||||
}
|
||||
LoginAccountParams::ChatgptAuthTokens {
|
||||
@@ -342,6 +365,7 @@ impl AccountRequestProcessor {
|
||||
let opts = LoginServerOptions {
|
||||
open_browser: false,
|
||||
codex_streamlined_login,
|
||||
source_surface_stable_id: self.auth_manager.source_surface_stable_id(),
|
||||
..LoginServerOptions::new(
|
||||
config.codex_home.to_path_buf(),
|
||||
oauth_client_id(),
|
||||
@@ -364,6 +388,16 @@ impl AccountRequestProcessor {
|
||||
Ok(opts)
|
||||
}
|
||||
|
||||
fn set_client_source_surface_stable_id(
|
||||
&self,
|
||||
accepts_client_source_surface_stable_id: bool,
|
||||
value: Option<String>,
|
||||
) {
|
||||
if accepts_client_source_surface_stable_id && let Some(value) = value {
|
||||
self.auth_manager.set_source_surface_stable_id(Some(value));
|
||||
}
|
||||
}
|
||||
|
||||
fn login_chatgpt_device_code_start_error(err: IoError) -> JSONRPCErrorError {
|
||||
let is_not_found = err.kind() == std::io::ErrorKind::NotFound;
|
||||
if is_not_found {
|
||||
|
||||
@@ -45,7 +45,7 @@ pub(crate) struct ConnectionState {
|
||||
|
||||
impl ConnectionState {
|
||||
pub(crate) fn new(
|
||||
_origin: ConnectionOrigin,
|
||||
origin: ConnectionOrigin,
|
||||
outbound_initialized: Arc<AtomicBool>,
|
||||
outbound_experimental_api_enabled: Arc<AtomicBool>,
|
||||
outbound_opted_out_notification_methods: Arc<RwLock<HashSet<String>>>,
|
||||
@@ -54,7 +54,7 @@ impl ConnectionState {
|
||||
outbound_initialized,
|
||||
outbound_experimental_api_enabled,
|
||||
outbound_opted_out_notification_methods,
|
||||
session: Arc::new(ConnectionSessionState::new()),
|
||||
session: Arc::new(ConnectionSessionState::new(origin)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1260,8 +1260,19 @@ impl TestAppServer {
|
||||
|
||||
/// Send an `account/login/start` JSON-RPC request for ChatGPT login.
|
||||
pub async fn send_login_account_chatgpt_request(&mut self) -> anyhow::Result<i64> {
|
||||
self.send_login_account_chatgpt_request_with_source_surface_stable_id(
|
||||
/*source_surface_stable_id*/ None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn send_login_account_chatgpt_request_with_source_surface_stable_id(
|
||||
&mut self,
|
||||
source_surface_stable_id: Option<&str>,
|
||||
) -> anyhow::Result<i64> {
|
||||
let params = serde_json::json!({
|
||||
"type": "chatgpt"
|
||||
"type": "chatgpt",
|
||||
"sourceSurfaceStableId": source_surface_stable_id,
|
||||
});
|
||||
self.send_request("account/login/start", Some(params)).await
|
||||
}
|
||||
|
||||
@@ -241,6 +241,7 @@ async fn logout_account_removes_auth_and_notifies() -> Result<()> {
|
||||
let get_id = mcp
|
||||
.send_get_account_request(GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
})
|
||||
.await?;
|
||||
let get_resp: JSONRPCResponse = timeout(
|
||||
@@ -308,6 +309,7 @@ async fn set_auth_token_updates_account_and_notifies() -> Result<()> {
|
||||
let get_id = mcp
|
||||
.send_get_account_request(GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
})
|
||||
.await?;
|
||||
let get_resp: JSONRPCResponse = timeout(
|
||||
@@ -376,6 +378,7 @@ async fn account_read_refresh_token_is_noop_in_external_mode() -> Result<()> {
|
||||
let get_id = mcp
|
||||
.send_get_account_request(GetAccountParams {
|
||||
refresh_token: true,
|
||||
source_surface_stable_id: None,
|
||||
})
|
||||
.await?;
|
||||
let get_resp: JSONRPCResponse = timeout(
|
||||
@@ -1521,7 +1524,11 @@ async fn login_account_chatgpt_includes_forced_workspace_query_param() -> Result
|
||||
let mut mcp = TestAppServer::new(codex_home.path()).await?;
|
||||
timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??;
|
||||
|
||||
let request_id = mcp.send_login_account_chatgpt_request().await?;
|
||||
let request_id = mcp
|
||||
.send_login_account_chatgpt_request_with_source_surface_stable_id(Some(
|
||||
"codex-source-stable-id",
|
||||
))
|
||||
.await?;
|
||||
let resp: JSONRPCResponse = timeout(
|
||||
DEFAULT_READ_TIMEOUT,
|
||||
mcp.read_stream_until_response_message(RequestId::Integer(request_id)),
|
||||
@@ -1536,6 +1543,16 @@ async fn login_account_chatgpt_includes_forced_workspace_query_param() -> Result
|
||||
auth_url.contains(&format!("allowed_workspace_id={WORKSPACE_ID_ALLOWED}")),
|
||||
"auth URL should include forced workspace"
|
||||
);
|
||||
let auth_url = Url::parse(&auth_url)?;
|
||||
let query = auth_url
|
||||
.query_pairs()
|
||||
.collect::<std::collections::HashMap<_, _>>();
|
||||
assert_eq!(
|
||||
query
|
||||
.get("source_surface_stable_id")
|
||||
.map(std::convert::AsRef::as_ref),
|
||||
Some("codex-source-stable-id")
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1600,6 +1617,7 @@ async fn get_account_no_auth() -> Result<()> {
|
||||
|
||||
let params = GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let request_id = mcp.send_get_account_request(params).await?;
|
||||
|
||||
@@ -1641,6 +1659,7 @@ async fn get_account_with_api_key() -> Result<()> {
|
||||
|
||||
let params = GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let request_id = mcp.send_get_account_request(params).await?;
|
||||
|
||||
@@ -1675,6 +1694,7 @@ async fn get_account_when_auth_not_required() -> Result<()> {
|
||||
|
||||
let params = GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let request_id = mcp.send_get_account_request(params).await?;
|
||||
|
||||
@@ -1716,6 +1736,7 @@ region = "us-west-2"
|
||||
|
||||
let params = GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let request_id = mcp.send_get_account_request(params).await?;
|
||||
|
||||
@@ -1760,6 +1781,7 @@ async fn get_account_with_managed_bedrock_provider() -> Result<()> {
|
||||
let request_id = mcp
|
||||
.send_get_account_request(GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
})
|
||||
.await?;
|
||||
let resp: JSONRPCResponse = timeout(
|
||||
@@ -1805,6 +1827,7 @@ async fn get_account_with_chatgpt() -> Result<()> {
|
||||
|
||||
let params = GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let request_id = mcp.send_get_account_request(params).await?;
|
||||
|
||||
@@ -1889,6 +1912,7 @@ async fn get_account_omits_chatgpt_after_permanent_refresh_failure() -> Result<(
|
||||
let request_id = mcp
|
||||
.send_get_account_request(GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
})
|
||||
.await?;
|
||||
|
||||
@@ -1932,6 +1956,7 @@ async fn get_account_with_chatgpt_missing_plan_claim_returns_unknown() -> Result
|
||||
|
||||
let params = GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let request_id = mcp.send_get_account_request(params).await?;
|
||||
|
||||
|
||||
@@ -190,6 +190,7 @@ async fn consume_timeout_releases_account_auth_queue() -> Result<()> {
|
||||
let account_id = mcp
|
||||
.send_get_account_request(GetAccountParams {
|
||||
refresh_token: false,
|
||||
source_surface_stable_id: None,
|
||||
})
|
||||
.await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user