mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-19 06:26:14 -05:00
feat(auth): refine embedded webview integration
This commit is contained in:
12 files changed
+401
-44
No files matched your search
@@ -12,6 +12,25 @@ use crate::{error::AuthDataParseError, utils::base64::decode_to_string};
|
||||
|
||||
pub type AuthDataParseResult = anyhow::Result<SamlAuthData, AuthDataParseError>;
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
|
||||
pub enum AuthWindowTheme {
|
||||
#[default]
|
||||
System,
|
||||
Light,
|
||||
Dark,
|
||||
}
|
||||
|
||||
impl AuthWindowTheme {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::System => "system",
|
||||
Self::Light => "light",
|
||||
Self::Dark => "dark",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SamlAuthData {
|
||||
|
||||
@@ -4,6 +4,8 @@ use anyhow::bail;
|
||||
use common::binary_paths;
|
||||
use tokio::process::Command;
|
||||
|
||||
#[cfg(feature = "webview-auth")]
|
||||
use crate::auth::AuthWindowTheme;
|
||||
use crate::{auth::SamlAuthResult, credential::Credential, os_profile::OsProfile};
|
||||
|
||||
use super::command_traits::CommandExt;
|
||||
@@ -27,6 +29,10 @@ pub struct SamlAuthLauncher<'a> {
|
||||
clean: bool,
|
||||
#[cfg(feature = "webview-auth")]
|
||||
default_browser: bool,
|
||||
#[cfg(feature = "webview-auth")]
|
||||
window_title: Option<&'a str>,
|
||||
#[cfg(feature = "webview-auth")]
|
||||
window_theme: Option<AuthWindowTheme>,
|
||||
browser: Option<&'a str>,
|
||||
verbose: Option<&'a str>,
|
||||
}
|
||||
@@ -52,6 +58,10 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
clean: false,
|
||||
#[cfg(feature = "webview-auth")]
|
||||
default_browser: false,
|
||||
#[cfg(feature = "webview-auth")]
|
||||
window_title: None,
|
||||
#[cfg(feature = "webview-auth")]
|
||||
window_theme: None,
|
||||
browser: None,
|
||||
verbose: None,
|
||||
}
|
||||
@@ -122,6 +132,18 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "webview-auth")]
|
||||
pub fn window_title(mut self, window_title: &'a str) -> Self {
|
||||
self.window_title = Some(window_title);
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "webview-auth")]
|
||||
pub fn window_theme(mut self, window_theme: AuthWindowTheme) -> Self {
|
||||
self.window_theme = Some(window_theme);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn browser(mut self, browser: Option<&'a str>) -> Self {
|
||||
self.browser = browser;
|
||||
self
|
||||
@@ -194,6 +216,14 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
if self.default_browser {
|
||||
auth_cmd.arg("--default-browser");
|
||||
}
|
||||
|
||||
if let Some(window_title) = self.window_title {
|
||||
auth_cmd.arg("--window-title").arg(window_title);
|
||||
}
|
||||
|
||||
if let Some(window_theme) = self.window_theme {
|
||||
auth_cmd.arg("--window-theme").arg(window_theme.as_str());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(browser) = self.browser {
|
||||
|
||||
@@ -1,19 +1,38 @@
|
||||
use super::lock_file::gpservice_lock_info;
|
||||
use std::path::Path;
|
||||
|
||||
async fn read_port() -> anyhow::Result<String> {
|
||||
let lock_info = gpservice_lock_info().await?;
|
||||
|
||||
Ok(lock_info.port.to_string())
|
||||
}
|
||||
use super::lock_file::{gpservice_lock_info, gpservice_lock_info_at};
|
||||
|
||||
pub async fn http_endpoint() -> anyhow::Result<String> {
|
||||
let port = read_port().await?;
|
||||
let port = gpservice_lock_info().await?.port;
|
||||
|
||||
Ok(format!("http://127.0.0.1:{}", port))
|
||||
}
|
||||
|
||||
pub async fn ws_endpoint() -> anyhow::Result<String> {
|
||||
let port = read_port().await?;
|
||||
let port = gpservice_lock_info().await?.port;
|
||||
|
||||
Ok(format!("ws://127.0.0.1:{}/ws", port))
|
||||
}
|
||||
|
||||
pub async fn ws_endpoint_from_lock_file(path: impl AsRef<Path>) -> anyhow::Result<String> {
|
||||
let port = gpservice_lock_info_at(path).await?.port;
|
||||
|
||||
Ok(format!("ws://127.0.0.1:{}/ws", port))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn websocket_endpoint_uses_the_selected_lock_file() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let lock_file = temp.path().join("gpservice.lock");
|
||||
std::fs::write(&lock_file, "123:4242").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
ws_endpoint_from_lock_file(lock_file).await.unwrap(),
|
||||
"ws://127.0.0.1:4242/ws"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use common::constants::GP_SERVICE_LOCK_FILE;
|
||||
use thiserror::Error;
|
||||
@@ -9,6 +9,10 @@ pub struct LockFile {
|
||||
pid: u32,
|
||||
}
|
||||
|
||||
pub fn dev_service_lock_file_path(bootstrap_socket: impl AsRef<Path>) -> PathBuf {
|
||||
bootstrap_socket.as_ref().with_file_name("gpservice.lock")
|
||||
}
|
||||
|
||||
impl LockFile {
|
||||
pub fn new<P: Into<PathBuf>>(path: P, pid: u32) -> Self {
|
||||
Self { path: path.into(), pid }
|
||||
@@ -95,7 +99,11 @@ impl LockInfo {
|
||||
}
|
||||
|
||||
pub async fn gpservice_lock_info() -> Result<LockInfo, LockFileError> {
|
||||
LockInfo::from_file(GP_SERVICE_LOCK_FILE).await
|
||||
gpservice_lock_info_at(GP_SERVICE_LOCK_FILE).await
|
||||
}
|
||||
|
||||
pub async fn gpservice_lock_info_at(path: impl AsRef<Path>) -> Result<LockInfo, LockFileError> {
|
||||
LockInfo::from_file(path).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -126,4 +134,12 @@ mod tests {
|
||||
Err(LockFileError::InvalidPort(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dev_service_lock_file_is_next_to_the_bootstrap_socket() {
|
||||
assert_eq!(
|
||||
dev_service_lock_file_path("/var/run/gpservice-dev-123/dev-bootstrap.sock"),
|
||||
PathBuf::from("/var/run/gpservice-dev-123/gpservice.lock")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,40 @@
|
||||
use tauri::WebviewWindow;
|
||||
|
||||
/// Returns the frame-to-content height difference for a standard titled macOS window.
|
||||
/// This is nonzero only on macOS.
|
||||
pub fn get_title_bar_height() -> f64 {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
macos_title_bar_height(os_info::get().version())
|
||||
}
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn macos_title_bar_height(version: &os_info::Version) -> f64 {
|
||||
if version.ge(&os_info::Version::from_string("26.0.0")) {
|
||||
32.0
|
||||
} else {
|
||||
28.0
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WindowExt {
|
||||
fn raise(&self) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
impl WindowExt for WebviewWindow {
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
#[cfg(target_os = "macos")]
|
||||
fn raise(&self) -> anyhow::Result<()> {
|
||||
self.show()?;
|
||||
self.set_focus()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
fn raise(&self) -> anyhow::Result<()> {
|
||||
self.show()?;
|
||||
Ok(())
|
||||
@@ -17,6 +46,24 @@ impl WindowExt for WebviewWindow {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn uses_the_current_macos_title_bar_height() {
|
||||
assert_eq!(macos_title_bar_height(&os_info::Version::from_string("25.9.0")), 28.0);
|
||||
assert_eq!(macos_title_bar_height(&os_info::Version::from_string("26.0.0")), 32.0);
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[test]
|
||||
fn native_title_bar_height_is_zero_without_a_native_macos_title_bar() {
|
||||
assert_eq!(get_title_bar_height(), 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
||||
mod unix {
|
||||
use std::{process::ExitStatus, time::Duration};
|
||||
|
||||
Reference in new issue
Block a user