diff --git a/Cargo.lock b/Cargo.lock index 5dde4b8..503ead1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2512,6 +2512,7 @@ dependencies = [ "tempfile", "thiserror 2.0.21", "tokio", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 903d5e1..0122d59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,5 +30,8 @@ serde_json = "1.0.140" thiserror = "2.0.12" tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread", "time"] } +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging"] } + [dev-dependencies] tempfile = "3.20.0" diff --git a/src/adapters/outbound/dataverse/auth.rs b/src/adapters/outbound/dataverse/auth.rs index 8e73d2e..337c59e 100644 --- a/src/adapters/outbound/dataverse/auth.rs +++ b/src/adapters/outbound/dataverse/auth.rs @@ -360,24 +360,36 @@ fn urldecode(value: &str) -> String { String::from_utf8_lossy(&decoded).into_owned() } -#[derive(Clone, Copy)] -enum BrowserPlatform { - #[cfg(target_os = "macos")] - MacOs, - #[cfg(any(target_os = "windows", test))] - Windows, - #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))] - Unix, +#[cfg(any(target_os = "windows", test))] +fn wide_null_terminated(value: &str) -> Vec { + value.encode_utf16().chain(std::iter::once(0)).collect() } -fn browser_launcher_candidates(platform: BrowserPlatform) -> &'static [&'static str] { - match platform { - #[cfg(target_os = "macos")] - BrowserPlatform::MacOs => &["open"], - #[cfg(any(target_os = "windows", test))] - BrowserPlatform::Windows => &["explorer.exe"], - #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))] - BrowserPlatform::Unix => &["xdg-open", "open"], +#[cfg(target_os = "windows")] +fn open_with_system_browser(url: &str) -> io::Result<()> { + use std::ptr::null_mut; + use windows_sys::Win32::UI::Shell::ShellExecuteW; + + let operation = wide_null_terminated("open"); + let url = wide_null_terminated(url); + // ShellExecuteW delegates the URL directly to Windows' registered HTTP handler. + let result = unsafe { + ShellExecuteW( + null_mut(), + operation.as_ptr(), + url.as_ptr(), + std::ptr::null(), + std::ptr::null(), + 1, + ) + } as isize; + + if result > 32 { + Ok(()) + } else { + Err(io::Error::other(format!( + "Windows konnte den Standardbrowser nicht öffnen (ShellExecuteW: {result})" + ))) } } @@ -432,22 +444,26 @@ fn open_browser(url: &str, browser: Option<&str>) -> io::Result<()> { } } - #[cfg(target_os = "macos")] - let platform = BrowserPlatform::MacOs; #[cfg(target_os = "windows")] - let platform = BrowserPlatform::Windows; - #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))] - let platform = BrowserPlatform::Unix; + return open_with_system_browser(url); + + #[cfg(not(target_os = "windows"))] + { + #[cfg(target_os = "macos")] + let candidates: &[&str] = &["open"]; + #[cfg(not(target_os = "macos"))] + let candidates: &[&str] = &["xdg-open", "open"]; - for program in browser_launcher_candidates(platform) { - if Command::new(program).arg(url).spawn().is_ok() { - return Ok(()); + for program in candidates { + if Command::new(program).arg(url).spawn().is_ok() { + return Ok(()); + } } + Err(io::Error::new( + io::ErrorKind::NotFound, + "kein Browser-Launcher gefunden (BROWSER setzen)", + )) } - Err(io::Error::new( - io::ErrorKind::NotFound, - "kein Browser-Launcher gefunden (BROWSER setzen)", - )) } fn wait_for_code(listener: TcpListener, timeout: Duration) -> Result { @@ -532,11 +548,11 @@ mod tests { use serde_json::json; #[test] - fn windows_uses_the_system_browser_launcher() { - assert_eq!( - browser_launcher_candidates(BrowserPlatform::Windows), - &["explorer.exe"] - ); + fn windows_browser_url_is_passed_without_shell_interpretation() { + let url = "https://login.example/authorize?a=one%2Ftwo&b=ä"; + let wide = wide_null_terminated(url); + assert_eq!(wide.last(), Some(&0)); + assert_eq!(String::from_utf16(&wide[..wide.len() - 1]).unwrap(), url); } #[test]