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:
@@ -1057,11 +1057,13 @@ fn persist_tokens(
|
||||
async fn request_chatgpt_token_refresh(
|
||||
refresh_token: String,
|
||||
client: &CodexHttpClient,
|
||||
source_surface_stable_id: Option<String>,
|
||||
) -> Result<RefreshResponse, RefreshTokenError> {
|
||||
let refresh_request = RefreshRequest {
|
||||
client_id: oauth_client_id(),
|
||||
grant_type: "refresh_token",
|
||||
refresh_token,
|
||||
source_surface_stable_id,
|
||||
};
|
||||
|
||||
let endpoint = refresh_token_endpoint();
|
||||
@@ -1157,6 +1159,8 @@ struct RefreshRequest {
|
||||
client_id: String,
|
||||
grant_type: &'static str,
|
||||
refresh_token: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
source_surface_stable_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
@@ -1526,6 +1530,7 @@ pub struct AuthManager {
|
||||
chatgpt_base_url: Option<String>,
|
||||
refresh_lock: Semaphore,
|
||||
external_auth: RwLock<Option<Arc<dyn ExternalAuth>>>,
|
||||
source_surface_stable_id: RwLock<Option<String>>,
|
||||
}
|
||||
|
||||
/// Configuration view required to construct a shared [`AuthManager`].
|
||||
@@ -1630,6 +1635,7 @@ impl AuthManager {
|
||||
chatgpt_base_url,
|
||||
refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
external_auth: RwLock::new(None),
|
||||
source_surface_stable_id: RwLock::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1652,6 +1658,7 @@ impl AuthManager {
|
||||
chatgpt_base_url: None,
|
||||
refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
external_auth: RwLock::new(None),
|
||||
source_surface_stable_id: RwLock::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1673,6 +1680,7 @@ impl AuthManager {
|
||||
chatgpt_base_url: None,
|
||||
refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
external_auth: RwLock::new(None),
|
||||
source_surface_stable_id: RwLock::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1694,6 +1702,7 @@ impl AuthManager {
|
||||
external_auth: RwLock::new(Some(
|
||||
Arc::new(BearerTokenRefresher::new(config)) as Arc<dyn ExternalAuth>
|
||||
)),
|
||||
source_surface_stable_id: RwLock::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1906,6 +1915,20 @@ impl AuthManager {
|
||||
self.enable_codex_api_key_env
|
||||
}
|
||||
|
||||
pub fn source_surface_stable_id(&self) -> Option<String> {
|
||||
self.source_surface_stable_id
|
||||
.read()
|
||||
.map(|value| value.clone())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn set_source_surface_stable_id(&self, value: Option<String>) {
|
||||
let value = crate::valid_source_surface_stable_id(value.as_deref()).map(str::to_owned);
|
||||
if let Ok(mut current) = self.source_surface_stable_id.write() {
|
||||
*current = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience constructor returning an `Arc` wrapper.
|
||||
pub async fn shared(
|
||||
codex_home: PathBuf,
|
||||
@@ -2210,7 +2233,12 @@ impl AuthManager {
|
||||
auth: &ChatgptAuth,
|
||||
refresh_token: String,
|
||||
) -> Result<(), RefreshTokenError> {
|
||||
let refresh_response = request_chatgpt_token_refresh(refresh_token, auth.client()).await?;
|
||||
let refresh_response = request_chatgpt_token_refresh(
|
||||
refresh_token,
|
||||
auth.client(),
|
||||
self.source_surface_stable_id(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
persist_tokens(
|
||||
auth.storage(),
|
||||
|
||||
@@ -199,6 +199,7 @@ pub async fn complete_device_code_login(
|
||||
&redirect_uri,
|
||||
&pkce,
|
||||
&code_resp.authorization_code,
|
||||
opts.source_surface_stable_id.as_deref(),
|
||||
)
|
||||
.await
|
||||
.map_err(|err| std::io::Error::other(format!("device code exchange failed: {err}")))?;
|
||||
|
||||
@@ -6,6 +6,14 @@ mod device_code_auth;
|
||||
mod pkce;
|
||||
mod server;
|
||||
|
||||
const SOURCE_SURFACE_STABLE_ID_MAX_LENGTH: usize = 128;
|
||||
|
||||
fn valid_source_surface_stable_id(value: Option<&str>) -> Option<&str> {
|
||||
value.filter(|value| {
|
||||
!value.trim().is_empty() && value.len() <= SOURCE_SURFACE_STABLE_ID_MAX_LENGTH
|
||||
})
|
||||
}
|
||||
|
||||
pub use codex_client::BuildCustomCaTransportError as BuildLoginHttpClientError;
|
||||
pub use codex_config::types::AuthCredentialsStoreMode;
|
||||
pub use device_code_auth::DeviceCode;
|
||||
|
||||
@@ -71,6 +71,7 @@ pub struct ServerOptions {
|
||||
pub codex_streamlined_login: bool,
|
||||
pub cli_auth_credentials_store_mode: AuthCredentialsStoreMode,
|
||||
pub auth_keyring_backend_kind: AuthKeyringBackendKind,
|
||||
pub source_surface_stable_id: Option<String>,
|
||||
}
|
||||
|
||||
impl ServerOptions {
|
||||
@@ -93,6 +94,7 @@ impl ServerOptions {
|
||||
codex_streamlined_login: false,
|
||||
cli_auth_credentials_store_mode,
|
||||
auth_keyring_backend_kind,
|
||||
source_surface_stable_id: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,6 +164,7 @@ pub fn run_login_server(opts: ServerOptions) -> io::Result<LoginServer> {
|
||||
&pkce,
|
||||
&state,
|
||||
opts.forced_chatgpt_workspace_id.as_deref(),
|
||||
opts.source_surface_stable_id.as_deref(),
|
||||
);
|
||||
|
||||
if opts.open_browser {
|
||||
@@ -336,8 +339,15 @@ async fn process_request(
|
||||
}
|
||||
};
|
||||
|
||||
match exchange_code_for_tokens(&opts.issuer, &opts.client_id, redirect_uri, pkce, &code)
|
||||
.await
|
||||
match exchange_code_for_tokens(
|
||||
&opts.issuer,
|
||||
&opts.client_id,
|
||||
redirect_uri,
|
||||
pkce,
|
||||
&code,
|
||||
opts.source_surface_stable_id.as_deref(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(tokens) => {
|
||||
if let Err(message) = ensure_workspace_allowed(
|
||||
@@ -489,6 +499,7 @@ fn build_authorize_url(
|
||||
pkce: &PkceCodes,
|
||||
state: &str,
|
||||
forced_chatgpt_workspace_ids: Option<&[String]>,
|
||||
source_surface_stable_id: Option<&str>,
|
||||
) -> String {
|
||||
let mut query = vec![
|
||||
("response_type".to_string(), "code".to_string()),
|
||||
@@ -512,6 +523,14 @@ fn build_authorize_url(
|
||||
if let Some(workspace_ids) = forced_chatgpt_workspace_ids {
|
||||
query.push(("allowed_workspace_id".to_string(), workspace_ids.join(",")));
|
||||
}
|
||||
if let Some(source_surface_stable_id) =
|
||||
crate::valid_source_surface_stable_id(source_surface_stable_id)
|
||||
{
|
||||
query.push((
|
||||
"source_surface_stable_id".to_string(),
|
||||
source_surface_stable_id.to_string(),
|
||||
));
|
||||
}
|
||||
let qs = query
|
||||
.into_iter()
|
||||
.map(|(k, v)| format!("{k}={}", urlencoding::encode(&v)))
|
||||
@@ -719,6 +738,7 @@ pub(crate) async fn exchange_code_for_tokens(
|
||||
redirect_uri: &str,
|
||||
pkce: &PkceCodes,
|
||||
code: &str,
|
||||
source_surface_stable_id: Option<&str>,
|
||||
) -> io::Result<ExchangedTokens> {
|
||||
#[derive(serde::Deserialize)]
|
||||
struct TokenResponse {
|
||||
@@ -735,16 +755,23 @@ pub(crate) async fn exchange_code_for_tokens(
|
||||
redirect_uri = %redirect_uri,
|
||||
"starting oauth token exchange"
|
||||
);
|
||||
let mut body = format!(
|
||||
"grant_type=authorization_code&code={}&redirect_uri={}&client_id={}&code_verifier={}",
|
||||
urlencoding::encode(code),
|
||||
urlencoding::encode(redirect_uri),
|
||||
urlencoding::encode(client_id),
|
||||
urlencoding::encode(&pkce.code_verifier)
|
||||
);
|
||||
if let Some(source_surface_stable_id) =
|
||||
crate::valid_source_surface_stable_id(source_surface_stable_id)
|
||||
{
|
||||
body.push_str("&source_surface_stable_id=");
|
||||
body.push_str(&urlencoding::encode(source_surface_stable_id));
|
||||
}
|
||||
let resp = client
|
||||
.post(token_endpoint)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.body(format!(
|
||||
"grant_type=authorization_code&code={}&redirect_uri={}&client_id={}&code_verifier={}",
|
||||
urlencoding::encode(code),
|
||||
urlencoding::encode(redirect_uri),
|
||||
urlencoding::encode(client_id),
|
||||
urlencoding::encode(&pkce.code_verifier)
|
||||
))
|
||||
.body(body)
|
||||
.send()
|
||||
.await;
|
||||
let resp = match resp {
|
||||
|
||||
@@ -62,6 +62,8 @@ async fn refresh_token_succeeds_updates_storage() -> Result<()> {
|
||||
bedrock_api_key: None,
|
||||
};
|
||||
ctx.write_auth(&initial_auth).await?;
|
||||
ctx.auth_manager
|
||||
.set_source_surface_stable_id(Some("codex-source-stable-id".to_string()));
|
||||
|
||||
ctx.auth_manager
|
||||
.refresh_token_from_authority()
|
||||
@@ -75,6 +77,7 @@ async fn refresh_token_succeeds_updates_storage() -> Result<()> {
|
||||
"client_id": "staging-client",
|
||||
"grant_type": "refresh_token",
|
||||
"refresh_token": INITIAL_REFRESH_TOKEN,
|
||||
"source_surface_stable_id": "codex-source-stable-id",
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
@@ -130,6 +130,7 @@ async fn end_to_end_login_flow_persists_auth_json() -> Result<()> {
|
||||
forced_chatgpt_workspace_id: Some(vec![chatgpt_account_id.to_string()]),
|
||||
codex_streamlined_login: false,
|
||||
auth_keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let server = run_login_server(opts)?;
|
||||
assert!(
|
||||
@@ -193,6 +194,7 @@ async fn creates_missing_codex_home_dir() -> Result<()> {
|
||||
forced_chatgpt_workspace_id: None,
|
||||
codex_streamlined_login: false,
|
||||
auth_keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let server = run_login_server(opts)?;
|
||||
let login_port = server.actual_port;
|
||||
@@ -237,6 +239,7 @@ async fn login_server_includes_forced_workspaces_as_one_query_param() -> Result<
|
||||
]),
|
||||
codex_streamlined_login: false,
|
||||
auth_keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let server = run_login_server(opts)?;
|
||||
let auth_url = Url::parse(&server.auth_url)?;
|
||||
@@ -276,6 +279,7 @@ async fn forced_chatgpt_workspace_id_mismatch_blocks_login() -> Result<()> {
|
||||
forced_chatgpt_workspace_id: Some(vec![WORKSPACE_ID_ALLOWED.to_string()]),
|
||||
codex_streamlined_login: false,
|
||||
auth_keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let server = run_login_server(opts)?;
|
||||
assert!(
|
||||
@@ -337,6 +341,7 @@ async fn oauth_access_denied_missing_entitlement_blocks_login_with_clear_error()
|
||||
forced_chatgpt_workspace_id: None,
|
||||
codex_streamlined_login: false,
|
||||
auth_keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let server = run_login_server(opts)?;
|
||||
let login_port = server.actual_port;
|
||||
@@ -406,6 +411,7 @@ async fn oauth_access_denied_unknown_reason_uses_generic_error_page() -> Result<
|
||||
forced_chatgpt_workspace_id: None,
|
||||
codex_streamlined_login: false,
|
||||
auth_keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
let server = run_login_server(opts)?;
|
||||
let login_port = server.actual_port;
|
||||
@@ -553,6 +559,7 @@ async fn cancels_previous_login_server_when_port_is_in_use() -> Result<()> {
|
||||
forced_chatgpt_workspace_id: None,
|
||||
codex_streamlined_login: false,
|
||||
auth_keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
|
||||
let first_server = run_login_server(first_opts)?;
|
||||
@@ -575,6 +582,7 @@ async fn cancels_previous_login_server_when_port_is_in_use() -> Result<()> {
|
||||
forced_chatgpt_workspace_id: None,
|
||||
codex_streamlined_login: false,
|
||||
auth_keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
source_surface_stable_id: None,
|
||||
};
|
||||
|
||||
let second_server = run_login_server(second_opts)?;
|
||||
|
||||
Reference in New Issue
Block a user