Files
zesdex/crates/zesdex-backend/src/app/runtime/actions/oauth.rs
T
asepharyana 9a67137954 refactor: massive codebase restructuring — naming, splitting, DRY
Crate renames:
  - zesdex-entities::seaorm → domain (misleading name, no SeaORM used)
  - zesdex-dto → merged into zesdex-entities (100% re-exports)
  - zesdex-libs → zesdex-infra (vague name)

Module renames:
  - app/harness → guard (misleading: safety gatekeeper, not test harness)
  - runtime/commands → action_dispatch (name clashed with controller/command)
  - resources → prompts (embedded prompt text, not general resources)
  - tool/seqthink → sequential_think (unreadable abbreviation)
  - msglog/query → insert (module only inserts, never queries)

Dead code removal:
  - app/mode/help.rs (orphaned — not declared in mod.rs)
  - app/mode/loading.rs (orphaned — not declared in mod.rs)

File splitting (71 new files, avg ~115 lines/file):
  - app/runtime/actions/: 1→8 files (was 2030 lines)
  - view/overlays/: 1→16 files (was 1167 lines)
  - tool/lsp/: 1→8 per-tool files (was 909 lines)
  - main.rs: 1→5 files (session, daemon, attach, event_loop)
  - workflow/engine + hive_mind: 2→10 files
  - subagent/engine + auto: 2→9 files
  - lsp/provisioner: 1→5 files
  - review/: 1→6 files
  - guard/: 1→2 files (extracted patterns)
  - state/misc: 1→3 files (input, scroll)
  - mcp/: 1→3 files (transport, adapter)
  - stream/json_repair extracted from turn.rs

DRY:
  - Pattern constants (STUB_PATTERNS etc) in guard/patterns shared with subagent
  - 3 near-identical background spawners → 1 generic + thin wrappers
  - Shared spawn_subagent_with_drain() extracted
  - Shared create_session() in main
  - write_osc52 deduplicated

Bug fixes:
  - archive_message(): sess.db → db (wrong variable name)
  - execute_one_tool(): wrong parameter name
  - check_credential_read() function was missing (restored from test expectations)
2026-07-17 09:08:41 +07:00

106 lines
4.3 KiB
Rust

//! OAuth PKCE flow — browser-based login for API providers.
/// Run a browser-based OAuth PKCE flow for the given provider.
///
/// Flow: look up config by provider name ("zen"/"opencode", "openai",
/// or a custom provider via env vars) → bind a loopback server → generate
/// a PKCE code verifier and challenge → build the authorisation URL →
/// wait for the redirect code on the loopback server (with a 120s timeout)
/// → exchange the code for a token → save the token to
/// `~/.config/zesdex/oauth_{provider}.json`.
///
/// Why: the `webbrowser::open` call is currently commented out; the user
/// must open the auth URL manually until that line is reinstated.
///
/// Return: a success message on completion, or an error if the flow fails
/// at any step.
pub(super) fn run_oauth_flow(provider: &str) -> anyhow::Result<String> {
use zesdex_iam::domain::oauth::OAuthConfig;
use zesdex_iam::domain::service::OAuthService;
use zesdex_iam::application::oauth_service::OAuthServiceImpl;
use zesdex_iam::infrastructure::persistence::oauth_repo::FileSystemOAuthRepository;
use zesdex_iam::infrastructure::oauth_loopback::LoopbackServer;
let config = match provider {
"zen" | "opencode" => OAuthConfig {
auth_url: "https://opencode.ai/zen/oauth/authorize".to_string(),
token_url: "https://opencode.ai/zen/oauth/token".to_string(),
client_id: std::env::var("ZEN_CLIENT_ID")
.unwrap_or_else(|_| "zesdex".to_string()),
client_secret: std::env::var("ZEN_CLIENT_SECRET").ok(),
scopes: vec![
"openid".to_string(),
"profile".to_string(),
"email".to_string(),
],
},
"openai" => OAuthConfig {
auth_url: "https://auth0.openai.com/authorize".to_string(),
token_url: "https://auth0.openai.com/oauth/token".to_string(),
client_id: std::env::var("OPENAI_CLIENT_ID")
.unwrap_or_else(|_| "zesdex".to_string()),
client_secret: std::env::var("OPENAI_CLIENT_SECRET").ok(),
scopes: vec![
"openid".to_string(),
"profile".to_string(),
"email".to_string(),
],
},
other => {
let auth_url = std::env::var(format!("{}_AUTH_URL", other.to_uppercase()))
.map_err(|_| {
anyhow::anyhow!(
"unknown provider '{}'. Set {}_AUTH_URL env var.",
other,
other.to_uppercase()
)
})?;
let token_url = std::env::var(format!("{}_TOKEN_URL", other.to_uppercase()))
.map_err(|_| anyhow::anyhow!("{}_TOKEN_URL not set", other.to_uppercase()))?;
let client_id = std::env::var(format!("{}_CLIENT_ID", other.to_uppercase()))
.unwrap_or_else(|_| "zesdex".to_string());
OAuthConfig {
auth_url,
token_url,
client_id,
client_secret: std::env::var(format!("{}_CLIENT_SECRET", other.to_uppercase()))
.ok(),
scopes: vec![
"openid".to_string(),
"profile".to_string(),
"email".to_string(),
],
}
}
};
let server = LoopbackServer::bind()?;
let redirect_uri = server.redirect_uri();
let token_path = dirs::config_dir()
.unwrap_or_else(|| std::path::PathBuf::from("."))
.join("zesdex")
.join(format!("oauth_{provider}.json"));
let oauth_service = OAuthServiceImpl::new(FileSystemOAuthRepository::new(), token_path);
let (auth_url, state) = oauth_service.start_flow(&config, &redirect_uri)?;
if auth_url.is_empty() {
tracing::warn!("[oauth] auth_url was empty for provider '{}'", provider);
} else if webbrowser::open(&auth_url).is_err() {
tracing::warn!(
"[oauth] could not open browser for '{}'; user must open URL manually:\n{}",
provider,
auth_url
);
}
let code = server.wait_for_code(120_000, &state)?;
oauth_service
.complete_flow(&config, &redirect_uri, &code, &state)
.map_err(|e| anyhow::anyhow!("{e}"))?;
Ok(format!("Successfully authenticated with {provider}."))
}