mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-23 15:46:13 -05:00
fix: refine macOS authentication app lifecycle
This commit is contained in:
5 files changed
+38
-8
No files matched your search
@@ -2,6 +2,8 @@
|
|||||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
<plist version="1.0">
|
<plist version="1.0">
|
||||||
<dict>
|
<dict>
|
||||||
|
<key>LSUIElement</key>
|
||||||
|
<true/>
|
||||||
<key>CFBundleURLTypes</key>
|
<key>CFBundleURLTypes</key>
|
||||||
<array>
|
<array>
|
||||||
<dict>
|
<dict>
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ pub fn run() -> anyhow::Result<()> {
|
|||||||
let timeout_handled = Arc::clone(&handled);
|
let timeout_handled = Arc::clone(&handled);
|
||||||
let app = tauri::Builder::default()
|
let app = tauri::Builder::default()
|
||||||
.setup(move |app| {
|
.setup(move |app| {
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
|
||||||
|
|
||||||
let app_handle = app.handle().clone();
|
let app_handle = app.handle().clone();
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
tokio::time::sleep(Duration::from_secs(10)).await;
|
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ pub async fn authenticate(
|
|||||||
|
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.setup(move |app| {
|
.setup(move |app| {
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
|
||||||
|
|
||||||
apply_auth_theme(app.handle(), window_theme);
|
apply_auth_theme(app.handle(), window_theme);
|
||||||
let app_handle = app.handle().clone();
|
let app_handle = app.handle().clone();
|
||||||
|
|
||||||
|
|||||||
@@ -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 anyhow::{Context, bail};
|
||||||
use objc2_app_kit::NSWorkspace;
|
use objc2_app_kit::NSWorkspace;
|
||||||
@@ -9,6 +9,11 @@ const CALLBACK_URL: &str = "globalprotectcallback:";
|
|||||||
const HANDLER_BUNDLE_ID: &str = "com.yuezk.gpauth";
|
const HANDLER_BUNDLE_ID: &str = "com.yuezk.gpauth";
|
||||||
const LOGIN_APP_NAME: &str = "GP Connect Login.app";
|
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<()> {
|
pub(super) fn ensure_auth_callback_handler() -> anyhow::Result<()> {
|
||||||
let workspace = NSWorkspace::sharedWorkspace();
|
let workspace = NSWorkspace::sharedWorkspace();
|
||||||
let app_paths = callback_app_paths();
|
let app_paths = callback_app_paths();
|
||||||
@@ -28,6 +33,7 @@ pub(super) fn ensure_auth_callback_handler() -> anyhow::Result<()> {
|
|||||||
.first()
|
.first()
|
||||||
.context("GP Connect Login.app is required for external-browser authentication; install GP Connect")?;
|
.context("GP Connect Login.app is required for external-browser authentication; install GP Connect")?;
|
||||||
let app_url = file_url(app_path);
|
let app_url = file_url(app_path);
|
||||||
|
register_app(&app_url)?;
|
||||||
workspace.setDefaultApplicationAtURL_toOpenURLsWithScheme_completionHandler(
|
workspace.setDefaultApplicationAtURL_toOpenURLsWithScheme_completionHandler(
|
||||||
&app_url,
|
&app_url,
|
||||||
&NSString::from_str(CALLBACK_SCHEME),
|
&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")
|
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)> {
|
fn current_handler(workspace: &NSWorkspace) -> Option<(String, PathBuf)> {
|
||||||
let callback_url = NSURL::URLWithString(&NSString::from_str(CALLBACK_URL))?;
|
let callback_url = NSURL::URLWithString(&NSString::from_str(CALLBACK_URL))?;
|
||||||
let app_url = workspace.URLForApplicationToOpenURL(&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()) {
|
if let Some(user_dir) = std::env::var_os("HOME").filter(|path| !path.is_empty()) {
|
||||||
let applications = PathBuf::from(user_dir).join("Applications");
|
let applications = PathBuf::from(user_dir).join("Applications");
|
||||||
candidates.extend([
|
candidates.push(
|
||||||
applications
|
applications
|
||||||
.join("GP Connect.app/Contents/Helpers")
|
.join("GP Connect.app/Contents/Helpers")
|
||||||
.join(LOGIN_APP_NAME),
|
.join(LOGIN_APP_NAME),
|
||||||
applications.join(LOGIN_APP_NAME),
|
);
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
candidates.extend([
|
candidates.push(PathBuf::from("/Applications/GP Connect.app/Contents/Helpers").join(LOGIN_APP_NAME));
|
||||||
PathBuf::from("/Applications/GP Connect.app/Contents/Helpers").join(LOGIN_APP_NAME),
|
|
||||||
PathBuf::from("/Applications").join(LOGIN_APP_NAME),
|
|
||||||
]);
|
|
||||||
|
|
||||||
candidates
|
candidates
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ use gpapi::{
|
|||||||
utils::{redact::redact_uri, window::get_title_bar_height},
|
utils::{redact::redact_uri, window::get_title_bar_height},
|
||||||
};
|
};
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
use tauri::Manager;
|
||||||
use tauri::{
|
use tauri::{
|
||||||
AppHandle, WebviewUrl, WebviewWindow, WindowEvent,
|
AppHandle, WebviewUrl, WebviewWindow, WindowEvent,
|
||||||
webview::{PageLoadEvent, PageLoadPayload},
|
webview::{PageLoadEvent, PageLoadPayload},
|
||||||
@@ -318,6 +320,14 @@ impl<'a> WebviewAuthenticator<'a> {
|
|||||||
|
|
||||||
info!("Raising auth window...");
|
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;
|
use gpapi::utils::window::WindowExt;
|
||||||
let result = auth_window.raise();
|
let result = auth_window.raise();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user