Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
82 changes: 49 additions & 33 deletions src/adapters/outbound/dataverse/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16> {
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})"
)))
}
}

Expand Down Expand Up @@ -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<String, AuthError> {
Expand Down Expand Up @@ -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]
Expand Down
Loading