fix: refine macOS authentication app lifecycle

This commit is contained in:
Kevin Yue committed 2026-08-06 10:59:56 +08:00
1 parent 588790f2d5
commit 17d1555155
5 files changed
+38 -8

No files matched your search

+2
View File
@@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSUIElement</key>
<true/>
<key>CFBundleURLTypes</key>
<array>
<dict>
+3
View File
@@ -13,6 +13,9 @@ pub fn run() -> anyhow::Result<()> {
let timeout_handled = Arc::clone(&handled);
let app = tauri::Builder::default()
.setup(move |app| {
#[cfg(target_os = "macos")]
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
let app_handle = app.handle().clone();
tauri::async_runtime::spawn(async move {
tokio::time::sleep(Duration::from_secs(10)).await;
+3
View File
@@ -19,6 +19,9 @@ pub async fn authenticate(
tauri::Builder::default()
.setup(move |app| {
#[cfg(target_os = "macos")]
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
apply_auth_theme(app.handle(), window_theme);
let app_handle = app.handle().clone();
+20 -8
View File
@@ -1,4 +1,4 @@
use std::{ffi::OsStr, path::PathBuf, thread, time::Duration};
use std::{ffi::OsStr, os::raw::c_void, path::PathBuf, thread, time::Duration};
use anyhow::{Context, bail};
use objc2_app_kit::NSWorkspace;
@@ -9,6 +9,11 @@ const CALLBACK_URL: &str = "globalprotectcallback:";
const HANDLER_BUNDLE_ID: &str = "com.yuezk.gpauth";
const LOGIN_APP_NAME: &str = "GP Connect Login.app";
#[link(name = "CoreServices", kind = "framework")]
unsafe extern "C" {
fn LSRegisterURL(url: *const c_void, update: u8) -> i32;
}
pub(super) fn ensure_auth_callback_handler() -> anyhow::Result<()> {
let workspace = NSWorkspace::sharedWorkspace();
let app_paths = callback_app_paths();
@@ -28,6 +33,7 @@ pub(super) fn ensure_auth_callback_handler() -> anyhow::Result<()> {
.first()
.context("GP Connect Login.app is required for external-browser authentication; install GP Connect")?;
let app_url = file_url(app_path);
register_app(&app_url)?;
workspace.setDefaultApplicationAtURL_toOpenURLsWithScheme_completionHandler(
&app_url,
&NSString::from_str(CALLBACK_SCHEME),
@@ -46,6 +52,16 @@ pub(super) fn ensure_auth_callback_handler() -> anyhow::Result<()> {
bail!("The globalprotectcallback URL handler registration did not take effect")
}
fn register_app(app_url: &NSURL) -> anyhow::Result<()> {
// A nested application is not necessarily known to LaunchServices until it
// has been launched. Register it explicitly before assigning its URL scheme.
let status = unsafe { LSRegisterURL(app_url as *const NSURL as *const c_void, 1) };
if status != 0 {
bail!("Failed to register GP Connect Login.app with LaunchServices (OSStatus {status})");
}
Ok(())
}
fn current_handler(workspace: &NSWorkspace) -> Option<(String, PathBuf)> {
let callback_url = NSURL::URLWithString(&NSString::from_str(CALLBACK_URL))?;
let app_url = workspace.URLForApplicationToOpenURL(&callback_url)?;
@@ -70,18 +86,14 @@ fn callback_app_paths() -> Vec<PathBuf> {
if let Some(user_dir) = std::env::var_os("HOME").filter(|path| !path.is_empty()) {
let applications = PathBuf::from(user_dir).join("Applications");
candidates.extend([
candidates.push(
applications
.join("GP Connect.app/Contents/Helpers")
.join(LOGIN_APP_NAME),
applications.join(LOGIN_APP_NAME),
]);
);
}
candidates.extend([
PathBuf::from("/Applications/GP Connect.app/Contents/Helpers").join(LOGIN_APP_NAME),
PathBuf::from("/Applications").join(LOGIN_APP_NAME),
]);
candidates.push(PathBuf::from("/Applications/GP Connect.app/Contents/Helpers").join(LOGIN_APP_NAME));
candidates
.into_iter()
+10
View File
@@ -8,6 +8,8 @@ use gpapi::{
utils::{redact::redact_uri, window::get_title_bar_height},
};
use log::{info, warn};
#[cfg(target_os = "macos")]
use tauri::Manager;
use tauri::{
AppHandle, WebviewUrl, WebviewWindow, WindowEvent,
webview::{PageLoadEvent, PageLoadPayload},
@@ -318,6 +320,14 @@ impl<'a> WebviewAuthenticator<'a> {
info!("Raising auth window...");
#[cfg(target_os = "macos")]
if let Err(err) = auth_window
.app_handle()
.set_activation_policy(tauri::ActivationPolicy::Regular)
{
warn!("Failed to show the application in the Dock: {}", err);
}
use gpapi::utils::window::WindowExt;
let result = auth_window.raise();