Files
codex/codex-rs/login/src/success_page_tests.rs
T
rafael-jac 1fd0858e86 [login] support hosted success redirects (#28745)
## What

Adds an optional hosted login-success redirect path for app-server login
requests.

- Keeps the existing localhost success page as the default.
- Lets app-server callers opt into a hosted success page with an
optional protocol field.
- Persists credentials before redirecting to the hosted success page.
- Keeps org setup and existing CLI/device-code login flows on the local
success page.
- Accepts an optional typed `appBrand` value and forwards it to the
hosted page as `app_brand` so web can select the correct asset.
- Generates the app-server protocol schema updates for the new optional
fields.

## Why

This supports the hosted Codex login success page rollout without
changing existing login behavior by default. The Codex Apps frontend can
gate the opt-in with Statsig after the hosted web page.

## Rollout safety

- Old callers omit the new field and continue using localhost.
- New callers talking to old app-server builds remain safe because the
Codex Apps side treats the field as optional and defaults the flag off.
- Missing brand values default to Codex.
- The hosted redirect always uses the app-login source so the hosted
page can reopen Codex; the existing streamlined-login visual flag
remains separate.

## Validation

- `just fmt`
- `just fix -p codex-login -p codex-app-server-protocol -p
codex-app-server -p codex-app-server-test-client -p codex-tui`
- `just test -p codex-login`
- `just test -p codex-app-server-protocol`
- `just write-app-server-schema`
- `git diff HEAD --check`

The focused login and protocol run passed all 380 tests.

I also started the broader `just test -p codex-app-server`; it compiled
successfully, then many tests failed on this machine because spawned
test servers tried to use the ambient `/Users/rafaelj/.codex/sqlite`
state DB, which is read-only in this sandbox. I stopped that run after
confirming the failures shared that environment issue.
2026-07-07 19:48:05 +00:00

121 lines
3.9 KiB
Rust

use base64::Engine;
use pretty_assertions::assert_eq;
use serde_json::json;
use super::*;
#[test]
fn compose_success_url_uses_local_page_by_default() {
let LoginSuccessRedirect::Local(url) = compose_success_url(
/*port*/ 1455,
DEFAULT_ISSUER,
"e30.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnt9fQ.sig",
"e30.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnt9fQ.sig",
/*codex_streamlined_login*/ false,
&LoginSuccessPage::default(),
) else {
panic!("expected local success redirect");
};
let url = Url::parse(&url).expect("success URL should parse");
assert_eq!(url.host_str(), Some("localhost"));
assert_eq!(url.path(), "/success");
assert_eq!(
url.query_pairs()
.find(|(key, _)| key == "codex_streamlined_login"),
None
);
}
#[test]
fn compose_success_url_uses_streamlined_local_page_when_requested() {
let LoginSuccessRedirect::Local(url) = compose_success_url(
/*port*/ 1455,
DEFAULT_ISSUER,
"e30.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnt9fQ.sig",
"e30.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnt9fQ.sig",
/*codex_streamlined_login*/ true,
&LoginSuccessPage::default(),
) else {
panic!("expected local success redirect");
};
let url = Url::parse(&url).expect("success URL should parse");
assert_eq!(
url.query_pairs()
.find(|(key, _)| key == "codex_streamlined_login")
.map(|(_, value)| value.into_owned()),
Some("true".to_string())
);
}
#[test]
fn compose_success_url_uses_hosted_page_when_requested() {
assert_eq!(
compose_success_url(
/*port*/ 1455,
DEFAULT_ISSUER,
"e30.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnt9fQ.sig",
"e30.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnt9fQ.sig",
/*codex_streamlined_login*/ false,
&LoginSuccessPage::Hosted {
url: Url::parse(CODEX_OPEN_APP_URL).expect("open app URL should parse"),
app_brand: LoginSuccessPageBrand::Chatgpt,
},
),
LoginSuccessRedirect::Hosted(
"https://chatgpt.com/codex/open-app?source=login&app_brand=chatgpt".to_string()
)
);
}
#[test]
fn compose_success_url_keeps_setup_on_local_page() {
let encode = |bytes: &[u8]| base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes);
let payload = encode(
serde_json::to_string(&json!({
"https://api.openai.com/auth": {
"completed_platform_onboarding": false,
"is_org_owner": true,
"organization_id": "org_123",
"project_id": "proj_123",
}
}))
.expect("payload should serialize")
.as_bytes(),
);
let access_payload = encode(
serde_json::to_string(&json!({
"https://api.openai.com/auth": {
"chatgpt_plan_type": "team",
}
}))
.expect("payload should serialize")
.as_bytes(),
);
let id_token = format!("e30.{payload}.sig");
let LoginSuccessRedirect::Local(url) = compose_success_url(
/*port*/ 1455,
DEFAULT_ISSUER,
&id_token,
&format!("e30.{access_payload}.sig"),
/*codex_streamlined_login*/ true,
&LoginSuccessPage::Hosted {
url: Url::parse(CODEX_OPEN_APP_URL).expect("open app URL should parse"),
app_brand: LoginSuccessPageBrand::Codex,
},
) else {
panic!("expected local success redirect");
};
let url = Url::parse(&url).expect("success URL should parse");
assert_eq!(url.host_str(), Some("localhost"));
assert_eq!(url.path(), "/success");
assert_eq!(
url.query_pairs()
.find(|(key, _)| key == "needs_setup")
.map(|(_, value)| value.into_owned()),
Some("true".to_string())
);
}