mirror of
				https://github.com/yuezk/GlobalProtect-openconnect.git
				synced 2025-05-20 07:26:58 -04:00 
			
		
		
		
	feat: gpauth support macos
This commit is contained in:
		@@ -1,6 +1,4 @@
 | 
			
		||||
use std::borrow::Cow;
 | 
			
		||||
 | 
			
		||||
use auth::{auth_prelogin, Authenticator, BrowserAuthenticator};
 | 
			
		||||
use auth::{auth_prelogin, BrowserAuthenticator};
 | 
			
		||||
use clap::Parser;
 | 
			
		||||
use gpapi::{
 | 
			
		||||
  auth::{SamlAuthData, SamlAuthResult},
 | 
			
		||||
@@ -33,7 +31,7 @@ const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", compile_time::dat
 | 
			
		||||
See 'gpauth -h' for more information.
 | 
			
		||||
"
 | 
			
		||||
)]
 | 
			
		||||
pub(crate) struct Cli {
 | 
			
		||||
struct Cli {
 | 
			
		||||
  #[arg(help = "The portal server to authenticate")]
 | 
			
		||||
  server: String,
 | 
			
		||||
 | 
			
		||||
@@ -110,28 +108,26 @@ impl Cli {
 | 
			
		||||
    let openssl_conf = self.prepare_env()?;
 | 
			
		||||
 | 
			
		||||
    let server = normalize_server(&self.server)?;
 | 
			
		||||
    let server: &'static str = Box::leak(server.into_boxed_str());
 | 
			
		||||
    let gp_params: &'static GpParams = Box::leak(Box::new(self.build_gp_params()));
 | 
			
		||||
    let gp_params = self.build_gp_params();
 | 
			
		||||
 | 
			
		||||
    let auth_request = match self.saml_request.as_deref() {
 | 
			
		||||
      Some(auth_request) => Cow::Borrowed(auth_request),
 | 
			
		||||
      None => Cow::Owned(auth_prelogin(server, gp_params).await?),
 | 
			
		||||
      Some(auth_request) => auth_request.to_string(),
 | 
			
		||||
      None => auth_prelogin(&server, &gp_params).await?,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let auth_request: &'static str = Box::leak(auth_request.into_owned().into_boxed_str());
 | 
			
		||||
    let authenticator = Authenticator::new(&server, gp_params).with_auth_request(&auth_request);
 | 
			
		||||
 | 
			
		||||
    #[cfg(feature = "webview-auth")]
 | 
			
		||||
    let browser = self
 | 
			
		||||
      .browser
 | 
			
		||||
      .as_deref()
 | 
			
		||||
      .or_else(|| self.default_browser.then_some("default"));
 | 
			
		||||
      .or_else(|| self.default_browser.then(|| "default"));
 | 
			
		||||
 | 
			
		||||
    #[cfg(not(feature = "webview-auth"))]
 | 
			
		||||
    let browser = self.browser.as_deref().or(Some("default"));
 | 
			
		||||
 | 
			
		||||
    if browser.is_some() {
 | 
			
		||||
      let auth_result = authenticator.browser_authenticate(browser).await;
 | 
			
		||||
    if let Some(browser) = browser {
 | 
			
		||||
      let authenticator = BrowserAuthenticator::new(&auth_request, browser);
 | 
			
		||||
      let auth_result = authenticator.authenticate().await;
 | 
			
		||||
 | 
			
		||||
      print_auth_result(auth_result);
 | 
			
		||||
 | 
			
		||||
      // explicitly drop openssl_conf to avoid the unused variable warning
 | 
			
		||||
@@ -140,7 +136,7 @@ impl Cli {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[cfg(feature = "webview-auth")]
 | 
			
		||||
    crate::webview_auth::authenticate(&self, authenticator, openssl_conf)?;
 | 
			
		||||
    crate::webview_auth::authenticate(server, gp_params, auth_request, self.clean, openssl_conf).await?;
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
 | 
			
		||||
 | 
			
		||||
mod cli;
 | 
			
		||||
 | 
			
		||||
#[cfg(feature = "webview-auth")]
 | 
			
		||||
mod webview_auth;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,23 +1,28 @@
 | 
			
		||||
use auth::{Authenticator, WebviewAuthenticator};
 | 
			
		||||
use auth::WebviewAuthenticator;
 | 
			
		||||
use gpapi::gp_params::GpParams;
 | 
			
		||||
use log::info;
 | 
			
		||||
use tauri::RunEvent;
 | 
			
		||||
use tempfile::NamedTempFile;
 | 
			
		||||
 | 
			
		||||
use crate::cli::{print_auth_result, Cli};
 | 
			
		||||
use crate::cli::print_auth_result;
 | 
			
		||||
 | 
			
		||||
pub fn authenticate(
 | 
			
		||||
  cli: &Cli,
 | 
			
		||||
  authenticator: Authenticator<'static>,
 | 
			
		||||
pub async fn authenticate(
 | 
			
		||||
  server: String,
 | 
			
		||||
  gp_params: GpParams,
 | 
			
		||||
  auth_request: String,
 | 
			
		||||
  clean: bool,
 | 
			
		||||
  mut openssl_conf: Option<NamedTempFile>,
 | 
			
		||||
) -> anyhow::Result<()> {
 | 
			
		||||
  let authenticator = authenticator.with_clean(cli.clean);
 | 
			
		||||
 | 
			
		||||
  tauri::Builder::default()
 | 
			
		||||
    .setup(move |app| {
 | 
			
		||||
      let app_handle = app.handle().clone();
 | 
			
		||||
 | 
			
		||||
      tauri::async_runtime::spawn(async move {
 | 
			
		||||
        let auth_result = authenticator.webview_authenticate(&app_handle).await;
 | 
			
		||||
        let authenticator = WebviewAuthenticator::new(&server, &gp_params)
 | 
			
		||||
          .with_auth_request(&auth_request)
 | 
			
		||||
          .with_clean(clean);
 | 
			
		||||
 | 
			
		||||
        let auth_result = authenticator.authenticate(&app_handle).await;
 | 
			
		||||
        print_auth_result(auth_result);
 | 
			
		||||
 | 
			
		||||
        // Ensure the app exits after the authentication process
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user