fix(oauth): close window from navigation callback, always return true

on_navigation returning false does not reliably block HTTP 302 redirects
on WebKitGTK (Linux). Closing the window from within the callback is
more reliable than calling close() from an async Tokio thread.

Changes:
- Always return true from on_navigation (never try to block)
- Close oauth window inside on_navigation callback after code capture
- Best-effort close via app.get_webview_window() after await (fallback)
- Add logging of every URL visited for debugging
- Idempotent: ignore duplicate code captures if callback fires twice
This commit is contained in:
vicrodh committed 2026-02-23 15:16:05 -06:00
1 parent 35ac015bb7
commit fea13ecee7
1 file changed
+20 -6
+20 -6
View File
@@ -1061,13 +1061,14 @@ pub async fn v2_start_oauth_login(
let code_holder_nav = Arc::clone(&code_holder); let code_holder_nav = Arc::clone(&code_holder);
let notify_nav = Arc::clone(&notify); let notify_nav = Arc::clone(&notify);
let app_for_nav = app.clone();
// Build and open the OAuth WebView window // Build and open the OAuth WebView window
let parsed_url: tauri::Url = oauth_url let parsed_url: tauri::Url = oauth_url
.parse() .parse()
.map_err(|e| format!("Invalid OAuth URL: {}", e))?; .map_err(|e| format!("Invalid OAuth URL: {}", e))?;
let oauth_window = tauri::WebviewWindowBuilder::new( let _oauth_window = tauri::WebviewWindowBuilder::new(
&app, &app,
"qobuz-oauth", "qobuz-oauth",
tauri::WebviewUrl::External(parsed_url), tauri::WebviewUrl::External(parsed_url),
@@ -1076,18 +1077,29 @@ pub async fn v2_start_oauth_login(
.inner_size(520.0, 720.0) .inner_size(520.0, 720.0)
.resizable(true) .resizable(true)
.on_navigation(move |url| { .on_navigation(move |url| {
log::info!("[OAuth] on_navigation: {}", url);
// Intercept redirect to play.qobuz.com/discover?code=... // Intercept redirect to play.qobuz.com/discover?code=...
if url.host_str() == Some("play.qobuz.com") { if url.host_str() == Some("play.qobuz.com") {
for (key, value) in url.query_pairs() { for (key, value) in url.query_pairs() {
if key == "code" { if key == "code" {
let mut holder = code_holder_nav.lock().unwrap();
// Only act on the first code (idempotent)
if holder.is_none() {
log::info!("[OAuth] Intercepted OAuth code from navigation"); log::info!("[OAuth] Intercepted OAuth code from navigation");
*code_holder_nav.lock().unwrap() = Some(value.to_string()); *holder = Some(value.to_string());
drop(holder);
notify_nav.notify_one(); notify_nav.notify_one();
return false; // Block — we handle the code, don't load discover // Close from within the navigation callback — more reliable on WebKitGTK
if let Some(win) = app_for_nav.get_webview_window("qobuz-oauth") {
let _ = win.close();
log::info!("[OAuth] OAuth window closed from navigation callback");
}
}
break;
} }
} }
} }
true // Allow all other navigation (login form, CSRF redirects, etc.) true // Always allow navigation — never block (blocking is unreliable on WebKitGTK)
}) })
.build() .build()
.map_err(|e| format!("Failed to open OAuth window: {}", e))?; .map_err(|e| format!("Failed to open OAuth window: {}", e))?;
@@ -1100,8 +1112,10 @@ pub async fn v2_start_oauth_login(
.await .await
.is_err(); .is_err();
// Always close the OAuth window // Best-effort close in case callback didn't fire (timeout, window manually closed, etc.)
let _ = oauth_window.close(); if let Some(win) = app.get_webview_window("qobuz-oauth") {
let _ = win.close();
}
if timed_out { if timed_out {
return Ok(V2LoginResponse { return Ok(V2LoginResponse {