Skip to content
Merged
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
39 changes: 35 additions & 4 deletions src/adapters/outbound/dataverse/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,27 @@ 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,
}

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"],
}
}

/// Opens the authorization URL with `$BROWSER` or the platform browser launcher.
fn open_browser(url: &str, browser: Option<&str>) -> io::Result<()> {
if let Some(browser) = browser {
Expand Down Expand Up @@ -412,11 +433,13 @@ fn open_browser(url: &str, browser: Option<&str>) -> io::Result<()> {
}

#[cfg(target_os = "macos")]
let candidates: &[&str] = &["open"];
#[cfg(not(target_os = "macos"))]
let candidates: &[&str] = &["xdg-open", "open"];
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;

for program in candidates {
for program in browser_launcher_candidates(platform) {
if Command::new(program).arg(url).spawn().is_ok() {
return Ok(());
}
Expand Down Expand Up @@ -508,6 +531,14 @@ mod tests {
use super::*;
use serde_json::json;

#[test]
fn windows_uses_the_system_browser_launcher() {
assert_eq!(
browser_launcher_candidates(BrowserPlatform::Windows),
&["explorer.exe"]
);
}

#[test]
fn scope_requests_dataverse_delegated_permission_and_offline_access() {
let config = json!({"environmentUrl": "https://org.crm4.dynamics.com/"});
Expand Down
Loading