diff --git a/Cargo.lock b/Cargo.lock
index 543e405..c5da13e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -223,6 +223,7 @@ dependencies = [
"html-escape",
"log",
"objc2",
+ "objc2-app-kit",
"objc2-foundation",
"objc2-web-kit",
"regex",
diff --git a/apps/gpauth/Info.plist b/apps/gpauth/Info.plist
new file mode 100644
index 0000000..e8b03b9
--- /dev/null
+++ b/apps/gpauth/Info.plist
@@ -0,0 +1,17 @@
+
+
+
+
+ CFBundleURLTypes
+
+
+ CFBundleURLName
+ GlobalProtect authentication callback
+ CFBundleURLSchemes
+
+ globalprotectcallback
+
+
+
+
+
diff --git a/apps/gpauth/src/callback_app.rs b/apps/gpauth/src/callback_app.rs
new file mode 100644
index 0000000..e08cf25
--- /dev/null
+++ b/apps/gpauth/src/callback_app.rs
@@ -0,0 +1,55 @@
+use std::{
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ },
+ time::Duration,
+};
+
+use tauri::RunEvent;
+
+pub fn run() -> anyhow::Result<()> {
+ let handled = Arc::new(AtomicBool::new(false));
+ let timeout_handled = Arc::clone(&handled);
+ let app = tauri::Builder::default()
+ .setup(move |app| {
+ let app_handle = app.handle().clone();
+ tauri::async_runtime::spawn(async move {
+ tokio::time::sleep(Duration::from_secs(10)).await;
+ if !timeout_handled.swap(true, Ordering::AcqRel) {
+ app_handle.exit(1);
+ }
+ });
+ Ok(())
+ })
+ .build(crate::tauri_context::get())?;
+
+ app.run(move |app_handle, event| {
+ let RunEvent::Opened { urls } = event else {
+ return;
+ };
+ let Some(callback) = urls
+ .into_iter()
+ .find(|url| url.scheme().eq_ignore_ascii_case("globalprotectcallback"))
+ else {
+ return;
+ };
+ if handled.swap(true, Ordering::AcqRel) {
+ return;
+ }
+
+ let app_handle = app_handle.clone();
+ tauri::async_runtime::spawn(async move {
+ let exit_code = match auth::forward_auth_callback(callback.as_str()).await {
+ Ok(()) => 0,
+ Err(err) => {
+ eprintln!("Failed to forward authentication callback: {err}");
+ 1
+ }
+ };
+ app_handle.exit(exit_code);
+ });
+ });
+
+ Ok(())
+}
diff --git a/apps/gpauth/src/cli.rs b/apps/gpauth/src/cli.rs
index b2dbdcc..7548dc7 100644
--- a/apps/gpauth/src/cli.rs
+++ b/apps/gpauth/src/cli.rs
@@ -1,5 +1,9 @@
-use auth::{BrowserAuthenticator, auth_prelogin, forward_auth_callback};
-use clap::{Parser, Subcommand};
+#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
+use auth::forward_auth_callback;
+use auth::{BrowserAuthenticator, auth_prelogin};
+use clap::Parser;
+#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
+use clap::Subcommand;
#[cfg(feature = "webview-auth")]
use gpapi::auth::AuthWindowTheme;
use gpapi::{
@@ -43,6 +47,7 @@ See 'gpauth -h' for more information.
"
)]
struct Cli {
+ #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
#[command(subcommand)]
command: Option,
@@ -115,6 +120,7 @@ struct Cli {
verbose: InfoLevelVerbosity,
}
+#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
#[derive(Subcommand)]
enum CliCommand {
#[command(hide = true)]
@@ -249,6 +255,7 @@ fn init_logger(cli: &Cli) {
pub async fn run() {
let cli = Cli::parse();
+ #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
if let Some(CliCommand::AuthCallback { callback }) = &cli.command {
if let Err(err) = forward_auth_callback(callback).await {
eprintln!("Failed to forward authentication callback: {err}");
@@ -294,6 +301,7 @@ mod tests {
}
#[test]
+ #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
fn callback_arguments_parse_separately() {
let cli = Cli::try_parse_from([
"gpauth",
diff --git a/apps/gpauth/src/main.rs b/apps/gpauth/src/main.rs
index 393941d..e41357a 100644
--- a/apps/gpauth/src/main.rs
+++ b/apps/gpauth/src/main.rs
@@ -2,10 +2,34 @@
mod cli;
+#[cfg(all(target_os = "macos", feature = "webview-auth"))]
+mod callback_app;
+
+#[cfg(feature = "webview-auth")]
+mod tauri_context;
+
#[cfg(feature = "webview-auth")]
mod webview_auth;
#[tokio::main]
async fn main() {
+ #[cfg(all(target_os = "macos", feature = "webview-auth"))]
+ if is_callback_app_launch() {
+ if let Err(err) = callback_app::run() {
+ eprintln!("Failed to handle authentication callback: {err}");
+ }
+ return;
+ }
+
cli::run().await;
}
+
+#[cfg(all(target_os = "macos", feature = "webview-auth"))]
+fn is_callback_app_launch() -> bool {
+ std::env::args_os().len() == 1
+ && std::env::current_exe().is_ok_and(|executable| {
+ executable
+ .ancestors()
+ .any(|path| path.extension().is_some_and(|extension| extension == "app"))
+ })
+}
diff --git a/apps/gpauth/src/tauri_context.rs b/apps/gpauth/src/tauri_context.rs
new file mode 100644
index 0000000..f0c7476
--- /dev/null
+++ b/apps/gpauth/src/tauri_context.rs
@@ -0,0 +1,3 @@
+pub fn get() -> tauri::Context {
+ tauri::generate_context!()
+}
diff --git a/apps/gpauth/src/webview_auth.rs b/apps/gpauth/src/webview_auth.rs
index 2add52a..cbd154e 100644
--- a/apps/gpauth/src/webview_auth.rs
+++ b/apps/gpauth/src/webview_auth.rs
@@ -39,7 +39,7 @@ pub async fn authenticate(
Ok(())
})
- .build(tauri::generate_context!())?
+ .build(crate::tauri_context::get())?
.run(move |_app_handle, event| {
if let RunEvent::Exit = event {
if let Some(file) = openssl_conf.take() {
diff --git a/crates/auth/Cargo.toml b/crates/auth/Cargo.toml
index be35c59..c270f54 100644
--- a/crates/auth/Cargo.toml
+++ b/crates/auth/Cargo.toml
@@ -33,6 +33,7 @@ webkit2gtk = { version = "2", optional = true }
[target.'cfg(target_os = "macos")'.dependencies]
block2 = { version = "0.6", optional = true }
objc2 = { version = "0.6", optional = true }
+objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSWorkspace", "block2"], optional = true }
objc2-foundation = { version = "0.3", optional = true }
objc2-web-kit = { version = "0.3", optional = true }
@@ -42,6 +43,10 @@ browser-auth = [
"dep:tiny_http",
"dep:uuid",
"dep:uzers",
+ "dep:block2",
+ "dep:objc2",
+ "dep:objc2-app-kit",
+ "dep:objc2-foundation",
]
webview-auth = [
"gpapi/tauri",
diff --git a/crates/auth/src/callback.rs b/crates/auth/src/callback.rs
index e8907eb..4acd92f 100644
--- a/crates/auth/src/callback.rs
+++ b/crates/auth/src/callback.rs
@@ -24,6 +24,9 @@ const STATUS_ACCEPTED: u8 = 0;
const STATUS_INVALID_CALLBACK: u8 = 1;
const STATUS_UNSUPPORTED_VERSION: u8 = 2;
+#[cfg(target_os = "macos")]
+mod macos;
+
pub struct AuthCallbackReceiver {
listener: UnixListener,
socket_path: PathBuf,
@@ -127,7 +130,17 @@ fn query_callback_handler(mime_type: &str) -> anyhow::Result {
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}
-#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")))]
+#[cfg(target_os = "macos")]
+pub fn ensure_auth_callback_handler() -> anyhow::Result<()> {
+ macos::ensure_auth_callback_handler()
+}
+
+#[cfg(not(any(
+ target_os = "linux",
+ target_os = "freebsd",
+ target_os = "openbsd",
+ target_os = "macos"
+)))]
pub fn ensure_auth_callback_handler() -> anyhow::Result<()> {
Ok(())
}
diff --git a/crates/auth/src/callback/macos.rs b/crates/auth/src/callback/macos.rs
new file mode 100644
index 0000000..c4a947c
--- /dev/null
+++ b/crates/auth/src/callback/macos.rs
@@ -0,0 +1,107 @@
+use std::{ffi::OsStr, path::PathBuf, thread, time::Duration};
+
+use anyhow::{Context, bail};
+use objc2_app_kit::NSWorkspace;
+use objc2_foundation::{NSBundle, NSString, NSURL};
+
+const CALLBACK_SCHEME: &str = "globalprotectcallback";
+const CALLBACK_URL: &str = "globalprotectcallback:";
+const HANDLER_BUNDLE_ID: &str = "com.yuezk.gpauth";
+const LOGIN_APP_NAME: &str = "GP Connect Login.app";
+
+pub(super) fn ensure_auth_callback_handler() -> anyhow::Result<()> {
+ let workspace = NSWorkspace::sharedWorkspace();
+ let app_paths = callback_app_paths();
+ let selected_handler = current_handler(&workspace);
+ match selected_handler.as_ref() {
+ Some((handler, path))
+ if handler == HANDLER_BUNDLE_ID && app_paths.iter().any(|candidate| same_path(candidate, path)) =>
+ {
+ return Ok(());
+ }
+ Some((handler, _)) if handler == HANDLER_BUNDLE_ID => {}
+ None => {}
+ Some((handler, _)) => bail!("The globalprotectcallback URL scheme is owned by {handler}"),
+ }
+
+ let app_path = app_paths
+ .first()
+ .context("GP Connect Login.app is required for external-browser authentication; install GP Connect")?;
+ let app_url = file_url(app_path);
+ workspace.setDefaultApplicationAtURL_toOpenURLsWithScheme_completionHandler(
+ &app_url,
+ &NSString::from_str(CALLBACK_SCHEME),
+ None,
+ );
+
+ for _ in 0..50 {
+ if current_handler(&workspace)
+ .is_some_and(|(handler, path)| handler == HANDLER_BUNDLE_ID && same_path(app_path, &path))
+ {
+ return Ok(());
+ }
+ thread::sleep(Duration::from_millis(100));
+ }
+
+ bail!("The globalprotectcallback URL handler registration did not take effect")
+}
+
+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)?;
+ let identifier = bundle_identifier(&app_url)?;
+ let path = app_url.path().map(|path| PathBuf::from(path.to_string()))?;
+ Some((identifier, path))
+}
+
+fn callback_app_paths() -> Vec {
+ let mut candidates = Vec::new();
+
+ if let Ok(executable) = std::env::current_exe() {
+ for path in executable.ancestors() {
+ if path.file_name() == Some(OsStr::new("Helpers")) {
+ candidates.push(path.join(LOGIN_APP_NAME));
+ }
+ if path.extension() == Some(OsStr::new("app")) {
+ candidates.push(path.to_path_buf());
+ }
+ }
+ }
+
+ 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([
+ 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
+ .into_iter()
+ .filter(|path| path.is_dir() && bundle_identifier(&file_url(path)).as_deref() == Some(HANDLER_BUNDLE_ID))
+ .collect()
+}
+
+fn same_path(left: &std::path::Path, right: &std::path::Path) -> bool {
+ match (left.canonicalize(), right.canonicalize()) {
+ (Ok(left), Ok(right)) => left == right,
+ _ => left == right,
+ }
+}
+
+fn file_url(path: &std::path::Path) -> objc2::rc::Retained {
+ NSURL::fileURLWithPath(&NSString::from_str(&path.to_string_lossy()))
+}
+
+fn bundle_identifier(app_url: &NSURL) -> Option {
+ NSBundle::bundleWithURL(app_url)?
+ .bundleIdentifier()
+ .map(|identifier| identifier.to_string())
+}
diff --git a/crates/common/src/constants.rs b/crates/common/src/constants.rs
index 44a863d..fa3cf04 100644
--- a/crates/common/src/constants.rs
+++ b/crates/common/src/constants.rs
@@ -16,7 +16,6 @@ pub const INSTALLED_VPNC_SCRIPT: &str = "/var/db/gpclient/scripts/vpnc-script";
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
pub const INSTALLED_VPNC_SCRIPT_METADATA: &str = "/var/db/gpclient/scripts/vpnc-script.metadata";
pub const MAX_VPNC_SCRIPT_SIZE: usize = 1024 * 1024;
-pub const GP_CALLBACK_PORT_FILENAME: &str = "gpcallback.port";
// Release binaries - macOS (Apple Silicon Homebrew)
#[cfg(all(not(debug_assertions), target_os = "macos", target_arch = "aarch64"))]