feat: prepare 3.0.0 release

This commit is contained in:
Kevin Yue committed 2026-10-09 18:21:44 +08:00
1 parent e98fd7894a
commit b7b708e9cd
252 files changed
+26210 -7457

No files matched your search

+155 -18
View File
@@ -1,14 +1,23 @@
use std::{path::PathBuf, process::Stdio};
use std::{
net::IpAddr,
path::{Path, PathBuf},
};
use anyhow::bail;
use common::binary_paths;
use tokio::process::Command;
use tokio_util::sync::CancellationToken;
use crate::{auth::SamlAuthResult, credential::Credential, os_profile::OsProfile};
#[cfg(feature = "webview-auth")]
use crate::auth::AuthWindowTheme;
use crate::{auth::SamlAuthResult, credential::Credential, log_format::LogFormat, os_profile::OsProfile};
use super::command_traits::CommandExt;
use super::{command_runner::run_controlled, command_traits::CommandExt};
const MAX_AUTH_RESULT_BYTES: usize = 256 * 1024;
pub struct SamlAuthLauncher<'a> {
cancellation: CancellationToken,
server: &'a str,
auth_executable: Option<&'a str>,
gateway: bool,
@@ -27,13 +36,20 @@ 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>,
browser_listen: Option<IpAddr>,
verbose: Option<&'a str>,
log_format: LogFormat,
}
impl<'a> SamlAuthLauncher<'a> {
pub fn new(server: &'a str) -> Self {
Self {
cancellation: CancellationToken::new(),
server,
auth_executable: None,
gateway: false,
@@ -52,8 +68,14 @@ 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,
browser_listen: None,
verbose: None,
log_format: LogFormat::Text,
}
}
@@ -122,23 +144,54 @@ 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
}
pub fn browser_listen(mut self, browser_listen: Option<IpAddr>) -> Self {
self.browser_listen = browser_listen;
self
}
/// Render the child's logs the same way as ours.
///
/// gpauth inherits this process's stderr, so both processes must use the same
/// format to keep the stream consistent.
pub fn log_format(mut self, log_format: LogFormat) -> Self {
self.log_format = log_format;
self
}
pub fn verbose(mut self, verbose: Option<&'a str>) -> Self {
self.verbose = verbose;
self
}
/// Launch the authenticator binary as the current user or SUDO_USER if available.
pub async fn launch(self) -> anyhow::Result<Credential> {
let program = self
.auth_executable
.map(PathBuf::from)
.unwrap_or_else(binary_paths::gpauth);
let mut auth_cmd = Command::new(&program);
pub fn cancellation(mut self, cancellation: &CancellationToken) -> Self {
self.cancellation = cancellation.clone();
self
}
/// The command line this launcher will run.
///
/// Split out from `launch` so a test can observe the argv the child actually
/// receives: asserting on the builder's own fields would not catch an
/// argument that never gets appended.
fn build_command(&self, program: &Path) -> Command {
let mut auth_cmd = Command::new(program);
auth_cmd.arg(self.server);
if self.gateway {
@@ -194,28 +247,65 @@ 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 {
auth_cmd.arg("--browser").arg(browser);
}
if let Some(browser_listen) = self.browser_listen {
auth_cmd.arg("--browser-listen").arg(browser_listen.to_string());
}
auth_cmd.arg("--log-format").arg(self.log_format.as_str());
if let Some(verbose) = self.verbose {
auth_cmd.arg(verbose);
}
let mut non_root_cmd = auth_cmd.into_non_root()?;
let child = non_root_cmd.kill_on_drop(true).stdout(Stdio::piped()).spawn();
auth_cmd
}
let child = match child {
Ok(child) => child,
Err(err) => {
bail!("Failed to spawn {}: {}", program.display(), err);
/// Launch the authenticator binary as the current user or SUDO_USER if available.
pub async fn launch(self) -> anyhow::Result<Credential> {
let program = self
.auth_executable
.map(PathBuf::from)
.unwrap_or_else(binary_paths::gpauth);
let auth_cmd = self.build_command(&program);
let command = auth_cmd.into_non_root()?.into_std();
let cancellation = self.cancellation;
let output = tokio::task::spawn_blocking(move || {
let check = || {
if cancellation.is_cancelled() {
Err(std::io::Error::new(
std::io::ErrorKind::Interrupted,
"Authentication cancelled",
))
} else {
Ok(())
}
};
run_controlled(command, &check, MAX_AUTH_RESULT_BYTES)
})
.await?;
let output = match output {
Ok(output) => output,
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {
return Err(crate::auth::AuthenticationCancelled.into());
}
Err(error) => return Err(error.into()),
};
let output = child.wait_with_output().await?;
let Ok(auth_result) = serde_json::from_slice::<SamlAuthResult>(&output.stdout) else {
bail!("Failed to parse auth data")
};
@@ -256,6 +346,45 @@ mod tests {
assert_eq!(launcher.client_version, Some("6.0.0"));
}
fn child_args(launcher: SamlAuthLauncher) -> Vec<String> {
launcher
.build_command(Path::new("gpauth"))
.as_std()
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())
.collect()
}
/// gpauth writes to the same stderr as its parent, so a caller reading the
/// parent's JSON would hit gpauth's text records mid-stream unless the format
/// is passed down.
#[test]
fn json_is_forwarded_to_the_child() {
let args = child_args(SamlAuthLauncher::new("portal.example.com").log_format(LogFormat::Json));
assert!(
args.windows(2).any(|pair| pair == ["--log-format", "json"]),
"expected --log-format json in {args:?}"
);
}
/// The launcher always passes the selected format so the parent and child
/// cannot silently drift onto different output formats.
#[test]
fn text_is_forwarded_to_the_child() {
for launcher in [
SamlAuthLauncher::new("portal.example.com").log_format(LogFormat::Text),
SamlAuthLauncher::new("portal.example.com"),
] {
let args = child_args(launcher);
assert!(
args.windows(2).any(|pair| pair == ["--log-format", "text"]),
"expected --log-format text in {args:?}"
);
}
}
#[test]
fn client_certificate_options_are_stored() {
let launcher = SamlAuthLauncher::new("portal.example.com")
@@ -267,4 +396,12 @@ mod tests {
assert_eq!(launcher.sslkey, Some("/tmp/client.key"));
assert_eq!(launcher.key_password, Some("secret"));
}
#[test]
fn browser_listen_address_is_stored() {
let listen_ip = "192.168.107.15".parse().unwrap();
let launcher = SamlAuthLauncher::new("portal.example.com").browser_listen(Some(listen_ip));
assert_eq!(launcher.browser_listen, Some(listen_ip));
}
}