feat: gpauth support macos

This commit is contained in:
Kevin Yue
2025-01-03 12:21:49 +00:00
parent 0c9b8e6c63
commit 25f1182556
23 changed files with 516 additions and 502 deletions

View File

@@ -6,8 +6,8 @@ version.workspace = true
edition.workspace = true
license.workspace = true
[build-dependencies]
tauri-build = { version = "2", features = [], optional = true }
# [build-dependencies]
# tauri-build = { version = "2", features = [], optional = true }
[dependencies]
gpapi = { path = "../../crates/gpapi", features = ["clap"] }
@@ -25,8 +25,10 @@ tempfile.workspace = true
compile-time.workspace = true
# webview auth dependencies
tauri = { workspace = true, optional = true }
# tauri = { workspace = true, optional = true }
tao = { version = "0.31", optional = true }
[features]
default = ["webview-auth"]
webview-auth = ["auth/webview-auth", "dep:tauri", "dep:tauri-build"]
webview-auth = ["auth/webview-auth", "dep:tao"]

View File

@@ -1,4 +1,4 @@
fn main() {
#[cfg(feature = "webview-auth")]
tauri_build::build()
// #[cfg(feature = "webview-auth")]
// tauri_build::build()
}

View File

@@ -1,6 +1,6 @@
use std::borrow::Cow;
use auth::{auth_prelogin, Authenticator, BrowserAuthenticator};
use auth::{auth_prelogin, BrowserAuthenticator};
use clap::Parser;
use gpapi::{
auth::{SamlAuthData, SamlAuthResult},
@@ -119,19 +119,20 @@ impl Cli {
};
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 +141,13 @@ impl Cli {
}
#[cfg(feature = "webview-auth")]
crate::webview_auth::authenticate(&self, authenticator, openssl_conf)?;
{
let builder = auth::WebviewAuthenticator::builder(server, gp_params)
.auth_request(auth_request)
.clean(self.clean);
crate::webview_auth::authenticate(builder, openssl_conf).await?;
}
// crate::webview_auth::authenticate(self, openssl_conf).await?;
Ok(())
}

View File

@@ -1,6 +1,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod cli;
#[cfg(feature = "webview-auth")]
mod webview_auth;

View File

@@ -1,41 +1,63 @@
use auth::{Authenticator, WebviewAuthenticator};
use auth::WebviewAuthenticatorBuilder;
use log::info;
use tauri::RunEvent;
use tao::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoopBuilder},
};
use tempfile::NamedTempFile;
use crate::cli::{print_auth_result, Cli};
pub fn authenticate(
cli: &Cli,
authenticator: Authenticator<'static>,
pub async fn authenticate<'a>(
builder: WebviewAuthenticatorBuilder<'a>,
mut openssl_conf: Option<NamedTempFile>,
) -> anyhow::Result<()> {
let authenticator = authenticator.with_clean(cli.clean);
let event_loop = EventLoopBuilder::with_user_event().build();
let authenticator = builder.build(&event_loop)?;
tauri::Builder::default()
.setup(move |app| {
let app_handle = app.handle().clone();
authenticator.authenticate().await?;
tauri::async_runtime::spawn(async move {
let auth_result = authenticator.webview_authenticate(&app_handle).await;
print_auth_result(auth_result);
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
// Ensure the app exits after the authentication process
app_handle.exit(0);
});
Ok(())
})
.build(tauri::generate_context!())?
.run(move |_app_handle, event| {
if let RunEvent::Exit = event {
if let Some(file) = openssl_conf.take() {
if let Err(err) = file.close() {
info!("Error closing OpenSSL config file: {}", err);
}
if let Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} = event
{
*control_flow = ControlFlow::Exit;
if let Some(file) = openssl_conf.take() {
if let Err(err) = file.close() {
info!("Error closing OpenSSL config file: {}", err);
}
}
});
}
});
Ok(())
// 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;
// print_auth_result(auth_result);
// // Ensure the app exits after the authentication process
// app_handle.exit(0);
// });
// Ok(())
// })
// .build(tauri::generate_context!())?
// .run(move |_app_handle, event| {
// if let RunEvent::Exit = event {
// if let Some(file) = openssl_conf.take() {
// if let Err(err) = file.close() {
// info!("Error closing OpenSSL config file: {}", err);
// }
// }
// }
// });
}