Compare commits

..

No commits in common. "5c6a1c77f72c675e016dbc30e45e6b595463c183" and "a1c63f8498dcc49e50085308f225ee7913e79875" have entirely different histories.

18 changed files with 106 additions and 248 deletions

30
Cargo.lock generated
View File

@ -570,7 +570,7 @@ dependencies = [
[[package]] [[package]]
name = "common" name = "common"
version = "2.3.7" version = "2.3.4"
dependencies = [ dependencies = [
"is_executable", "is_executable",
] ]
@ -1430,7 +1430,7 @@ dependencies = [
[[package]] [[package]]
name = "gpapi" name = "gpapi"
version = "2.3.7" version = "2.3.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64 0.21.5", "base64 0.21.5",
@ -1465,7 +1465,7 @@ dependencies = [
[[package]] [[package]]
name = "gpauth" name = "gpauth"
version = "2.3.7" version = "2.3.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
@ -1486,7 +1486,7 @@ dependencies = [
[[package]] [[package]]
name = "gpclient" name = "gpclient"
version = "2.3.7" version = "2.3.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
@ -1508,7 +1508,7 @@ dependencies = [
[[package]] [[package]]
name = "gpgui-helper" name = "gpgui-helper"
version = "2.3.7" version = "2.3.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
@ -1526,7 +1526,7 @@ dependencies = [
[[package]] [[package]]
name = "gpservice" name = "gpservice"
version = "2.3.7" version = "2.3.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"axum", "axum",
@ -2426,12 +2426,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.45" version = "0.1.45"
@ -2555,7 +2549,7 @@ dependencies = [
[[package]] [[package]]
name = "openconnect" name = "openconnect"
version = "2.3.7" version = "2.3.4"
dependencies = [ dependencies = [
"cc", "cc",
"common", "common",
@ -4149,13 +4143,12 @@ dependencies = [
[[package]] [[package]]
name = "time" name = "time"
version = "0.3.36" version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" checksum = "f657ba42c3f86e7680e53c8cd3af8abbe56b5491790b46e22e19c0d57463583e"
dependencies = [ dependencies = [
"deranged", "deranged",
"itoa 1.0.10", "itoa 1.0.10",
"num-conv",
"powerfmt", "powerfmt",
"serde", "serde",
"time-core", "time-core",
@ -4170,11 +4163,10 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
[[package]] [[package]]
name = "time-macros" name = "time-macros"
version = "0.2.18" version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" checksum = "26197e33420244aeb70c3e8c78376ca46571bc4e701e4791c2cd9f57dcb3a43f"
dependencies = [ dependencies = [
"num-conv",
"time-core", "time-core",
] ]

View File

@ -5,7 +5,7 @@ members = ["crates/*", "apps/gpclient", "apps/gpservice", "apps/gpauth", "apps/g
[workspace.package] [workspace.package]
rust-version = "1.70" rust-version = "1.70"
version = "2.3.7" version = "2.3.4"
authors = ["Kevin Yue <k3vinyue@gmail.com>"] authors = ["Kevin Yue <k3vinyue@gmail.com>"]
homepage = "https://github.com/yuezk/GlobalProtect-openconnect" homepage = "https://github.com/yuezk/GlobalProtect-openconnect"
edition = "2021" edition = "2021"

View File

@ -44,20 +44,12 @@ Options:
See 'gpclient help <command>' for more information on a specific command. See 'gpclient help <command>' for more information on a specific command.
``` ```
To use the external browser for authentication with the CLI version, you need to use the following command: To use the default browser for authentication with the CLI version, you need to use the following command:
```bash ```bash
sudo -E gpclient connect --browser default <portal> sudo -E gpclient connect --default-browser <portal>
``` ```
Or you can try the following command if the above command does not work:
```bash
gpauth <portal> --browser default 2>/dev/null | sudo gpclient connect <portal> --cookie-on-stdin
```
You can specify the browser with the `--browser <browser>` option, e.g., `--browser firefox`, `--browser chrome`, etc.
### GUI ### GUI
The GUI version is also available after you installed it. You can launch it from the application menu or run `gpclient launch-gui` in the terminal. The GUI version is also available after you installed it. You can launch it from the application menu or run `gpclient launch-gui` in the terminal.

View File

@ -1,6 +1,5 @@
[package] [package]
name = "gpauth" name = "gpauth"
authors.workspace = true
version.workspace = true version.workspace = true
edition.workspace = true edition.workspace = true
license.workspace = true license.workspace = true

View File

@ -1,5 +1,3 @@
use std::{env::temp_dir, fs, os::unix::fs::PermissionsExt};
use clap::Parser; use clap::Parser;
use gpapi::{ use gpapi::{
auth::{SamlAuthData, SamlAuthResult}, auth::{SamlAuthData, SamlAuthResult},
@ -13,69 +11,37 @@ use log::{info, LevelFilter};
use serde_json::json; use serde_json::json;
use tauri::{App, AppHandle, RunEvent}; use tauri::{App, AppHandle, RunEvent};
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use tokio::{io::AsyncReadExt, net::TcpListener};
use crate::auth_window::{portal_prelogin, AuthWindow}; use crate::auth_window::{portal_prelogin, AuthWindow};
const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", compile_time::date_str!(), ")"); const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", compile_time::date_str!(), ")");
#[derive(Parser, Clone)] #[derive(Parser, Clone)]
#[command( #[command(version = VERSION)]
version = VERSION,
author,
about = "The authentication component for the GlobalProtect VPN client, supports the SSO authentication method.",
help_template = "\
{before-help}{name} {version}
{author}
{about}
{usage-heading} {usage}
{all-args}{after-help}
See 'gpauth -h' for more information.
"
)]
struct Cli { struct Cli {
#[arg(help = "The portal server to authenticate")]
server: String, server: String,
#[arg(long)]
#[arg(long, help = "Treating the server as a gateway")]
gateway: bool, gateway: bool,
#[arg(long)]
#[arg(long, help = "The SAML authentication request")]
saml_request: Option<String>, saml_request: Option<String>,
#[arg(long, default_value = GP_USER_AGENT)]
#[arg(long, default_value = GP_USER_AGENT, help = "The user agent to use")]
user_agent: String, user_agent: String,
#[arg(long, default_value = "Linux")] #[arg(long, default_value = "Linux")]
os: Os, os: Os,
#[arg(long)] #[arg(long)]
os_version: Option<String>, os_version: Option<String>,
#[arg(long)]
#[arg(long, help = "The HiDPI mode, useful for high-resolution screens")]
hidpi: bool, hidpi: bool,
#[arg(long)]
#[arg(long, help = "Get around the OpenSSL `unsafe legacy renegotiation` error")]
fix_openssl: bool, fix_openssl: bool,
#[arg(long)]
#[arg(long, help = "Ignore TLS errors")]
ignore_tls_errors: bool, ignore_tls_errors: bool,
#[arg(long)]
#[arg(long, help = "Clean the cache of the embedded browser")]
clean: bool, clean: bool,
#[arg(long)]
#[arg(long, help = "Use the default browser for authentication")]
default_browser: bool, default_browser: bool,
#[arg(long)]
#[arg( external_browser: Option<String>,
long,
help = "The browser to use for authentication, e.g., `default`, `firefox`, `chrome`, `chromium`, or the path to the browser executable"
)]
browser: Option<String>,
} }
impl Cli { impl Cli {
@ -95,8 +61,8 @@ impl Cli {
None => portal_prelogin(&self.server, &gp_params).await?, None => portal_prelogin(&self.server, &gp_params).await?,
}; };
let browser_auth = if let Some(browser) = &self.browser { let browser_auth = if let Some(external_browser) = &self.external_browser {
Some(BrowserAuthenticator::new_with_browser(&saml_request, browser)) Some(BrowserAuthenticator::new_with_browser(&saml_request, external_browser))
} else if self.default_browser { } else if self.default_browser {
Some(BrowserAuthenticator::new(&saml_request)) Some(BrowserAuthenticator::new(&saml_request))
} else { } else {
@ -108,15 +74,6 @@ impl Cli {
info!("Please continue the authentication process in the default browser"); info!("Please continue the authentication process in the default browser");
let auth_result = match wait_auth_data().await {
Ok(auth_data) => SamlAuthResult::Success(auth_data),
Err(err) => SamlAuthResult::Failure(format!("{}", err)),
};
info!("Authentication completed");
println!("{}", json!(auth_result));
return Ok(()); return Ok(());
} }
@ -224,35 +181,3 @@ pub async fn run() {
std::process::exit(1); std::process::exit(1);
} }
} }
async fn wait_auth_data() -> anyhow::Result<SamlAuthData> {
// Start a local server to receive the browser authentication data
let listener = TcpListener::bind("127.0.0.1:0").await?;
let port = listener.local_addr()?.port();
let port_file = temp_dir().join("gpcallback.port");
// Write the port to a file
fs::write(&port_file, port.to_string())?;
fs::set_permissions(&port_file, fs::Permissions::from_mode(0o600))?;
// Remove the previous log file
let callback_log = temp_dir().join("gpcallback.log");
let _ = fs::remove_file(&callback_log);
info!("Listening authentication data on port {}", port);
info!(
"If it hangs, please check the logs at `{}` for more information",
callback_log.display()
);
let (mut socket, _) = listener.accept().await?;
info!("Received the browser authentication data from the socket");
let mut data = String::new();
socket.read_to_string(&mut data).await?;
// Remove the port file
fs::remove_file(&port_file)?;
let auth_data = SamlAuthData::from_gpcallback(&data)?;
Ok(auth_data)
}

View File

@ -1,5 +1,3 @@
use std::{env::temp_dir, fs::File};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use gpapi::utils::openssl; use gpapi::utils::openssl;
use log::{info, LevelFilter}; use log::{info, LevelFilter};
@ -87,29 +85,14 @@ impl Cli {
} }
} }
fn init_logger(command: &CliCommand) { fn init_logger() {
let mut builder = env_logger::builder(); env_logger::builder().filter_level(LevelFilter::Info).init();
builder.filter_level(LevelFilter::Info);
// Output the log messages to a file if the command is the auth callback
if let CliCommand::LaunchGui(args) = command {
let auth_data = args.auth_data.as_deref().unwrap_or_default();
if !auth_data.is_empty() {
if let Ok(log_file) = File::create(temp_dir().join("gpcallback.log")) {
let target = Box::new(log_file);
builder.target(env_logger::Target::Pipe(target));
}
}
}
builder.init();
} }
pub(crate) async fn run() { pub(crate) async fn run() {
let cli = Cli::parse(); let cli = Cli::parse();
init_logger(&cli.command); init_logger();
info!("gpclient started: {}", VERSION); info!("gpclient started: {}", VERSION);
if let Err(err) = cli.run().await { if let Err(err) = cli.run().await {

View File

@ -1,10 +1,8 @@
use std::{cell::RefCell, fs, sync::Arc}; use std::{cell::RefCell, fs, sync::Arc};
use anyhow::bail;
use clap::Args; use clap::Args;
use common::vpn_utils::find_csd_wrapper; use common::vpn_utils::find_csd_wrapper;
use gpapi::{ use gpapi::{
auth::SamlAuthResult,
clap::args::Os, clap::args::Os,
credential::{Credential, PasswordCredential}, credential::{Credential, PasswordCredential},
error::PortalError, error::PortalError,
@ -21,8 +19,9 @@ use gpapi::{
use inquire::{Password, PasswordDisplayMode, Select, Text}; use inquire::{Password, PasswordDisplayMode, Select, Text};
use log::info; use log::info;
use openconnect::Vpn; use openconnect::Vpn;
use tokio::{io::AsyncReadExt, net::TcpListener};
use crate::{cli::SharedArgs, GP_CLIENT_LOCK_FILE}; use crate::{cli::SharedArgs, GP_CLIENT_LOCK_FILE, GP_CLIENT_PORT_FILE};
#[derive(Args)] #[derive(Args)]
pub(crate) struct ConnectArgs { pub(crate) struct ConnectArgs {
@ -38,9 +37,6 @@ pub(crate) struct ConnectArgs {
#[arg(long, help = "Read the password from standard input")] #[arg(long, help = "Read the password from standard input")]
passwd_on_stdin: bool, passwd_on_stdin: bool,
#[arg(long, help = "Read the cookie from standard input")]
cookie_on_stdin: bool,
#[arg(long, short, help = "The VPNC script to use")] #[arg(long, short, help = "The VPNC script to use")]
script: Option<String>, script: Option<String>,
@ -93,7 +89,7 @@ pub(crate) struct ConnectArgs {
#[arg(long, help = "Disable DTLS and ESP")] #[arg(long, help = "Disable DTLS and ESP")]
no_dtls: bool, no_dtls: bool,
#[arg(long, help = "The HiDPI mode, useful for high-resolution screens")] #[arg(long, help = "The HiDPI mode, useful for high resolution screens")]
hidpi: bool, hidpi: bool,
#[arg(long, help = "Do not reuse the remembered authentication cookie")] #[arg(long, help = "Do not reuse the remembered authentication cookie")]
@ -104,9 +100,9 @@ pub(crate) struct ConnectArgs {
#[arg( #[arg(
long, long,
help = "Use the specified browser to authenticate, e.g., `default`, `firefox`, `chrome`, `chromium`, or the path to the browser executable" help = "Use the specified browser to authenticate, e.g., firefox, chromium, chrome, or the path to the browser"
)] )]
browser: Option<String>, external_browser: Option<String>,
} }
impl ConnectArgs { impl ConnectArgs {
@ -151,10 +147,6 @@ impl<'a> ConnectHandler<'a> {
} }
pub(crate) async fn handle(&self) -> anyhow::Result<()> { pub(crate) async fn handle(&self) -> anyhow::Result<()> {
if self.args.default_browser && self.args.browser.is_some() {
bail!("Cannot use `--default-browser` and `--browser` options at the same time");
}
self.latest_key_password.replace(self.args.key_password.clone()); self.latest_key_password.replace(self.args.key_password.clone());
loop { loop {
@ -335,17 +327,13 @@ impl<'a> ConnectHandler<'a> {
} }
async fn obtain_credential(&self, prelogin: &Prelogin, server: &str) -> anyhow::Result<Credential> { async fn obtain_credential(&self, prelogin: &Prelogin, server: &str) -> anyhow::Result<Credential> {
if self.args.cookie_on_stdin {
return read_cookie_from_stdin();
}
let is_gateway = prelogin.is_gateway(); let is_gateway = prelogin.is_gateway();
match prelogin { match prelogin {
Prelogin::Saml(prelogin) => { Prelogin::Saml(prelogin) => {
let use_default_browser = prelogin.support_default_browser() && self.args.default_browser; let use_default_browser = prelogin.support_default_browser() && self.args.default_browser;
let browser = if prelogin.support_default_browser() { let external_browser = if prelogin.support_default_browser() {
self.args.browser.as_deref() self.args.external_browser.as_deref()
} else { } else {
None None
}; };
@ -361,13 +349,22 @@ impl<'a> ConnectHandler<'a> {
.ignore_tls_errors(self.shared_args.ignore_tls_errors) .ignore_tls_errors(self.shared_args.ignore_tls_errors)
.clean(self.args.clean) .clean(self.args.clean)
.default_browser(use_default_browser) .default_browser(use_default_browser)
.browser(browser) .external_browser(external_browser)
.launch() .launch()
.await?; .await?;
Ok(cred) if let Some(cred) = cred {
} return Ok(cred);
}
if !use_default_browser {
// This should never happen
unreachable!("SAML authentication failed without using the default browser");
}
info!("Waiting for the browser authentication to complete...");
wait_credentials().await
}
Prelogin::Standard(prelogin) => { Prelogin::Standard(prelogin) => {
let prefix = if is_gateway { "Gateway" } else { "Portal" }; let prefix = if is_gateway { "Gateway" } else { "Portal" };
println!("{} ({}: {})", prelogin.auth_message(), prefix, server); println!("{} ({}: {})", prelogin.auth_message(), prefix, server);
@ -397,17 +394,25 @@ impl<'a> ConnectHandler<'a> {
} }
} }
fn read_cookie_from_stdin() -> anyhow::Result<Credential> { async fn wait_credentials() -> anyhow::Result<Credential> {
info!("Reading cookie from standard input"); // Start a local server to receive the browser authentication data
let listener = TcpListener::bind("127.0.0.1:0").await?;
let port = listener.local_addr()?.port();
let mut cookie = String::new(); // Write the port to a file
std::io::stdin().read_line(&mut cookie)?; fs::write(GP_CLIENT_PORT_FILE, port.to_string())?;
let Ok(auth_result) = serde_json::from_str::<SamlAuthResult>(cookie.trim_end()) else { info!("Listening authentication data on port {}", port);
bail!("Failed to parse auth data") let (mut socket, _) = listener.accept().await?;
};
Credential::try_from(auth_result) info!("Received the browser authentication data from the socket");
let mut data = String::new();
socket.read_to_string(&mut data).await?;
// Remove the port file
fs::remove_file(GP_CLIENT_PORT_FILE)?;
Credential::from_gpcallback(&data)
} }
fn write_pid_file() { fn write_pid_file() {

View File

@ -9,13 +9,15 @@ use gpapi::{
use log::info; use log::info;
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
use crate::GP_CLIENT_PORT_FILE;
#[derive(Args)] #[derive(Args)]
pub(crate) struct LaunchGuiArgs { pub(crate) struct LaunchGuiArgs {
#[arg( #[arg(
required = false, required = false,
help = "The authentication data, used for the default browser authentication" help = "The authentication data, used for the default browser authentication"
)] )]
pub auth_data: Option<String>, auth_data: Option<String>,
#[arg(long, help = "Launch the GUI minimized")] #[arg(long, help = "Launch the GUI minimized")]
minimized: bool, minimized: bool,
} }
@ -38,7 +40,6 @@ impl<'a> LaunchGuiHandler<'a> {
let auth_data = self.args.auth_data.as_deref().unwrap_or_default(); let auth_data = self.args.auth_data.as_deref().unwrap_or_default();
if !auth_data.is_empty() { if !auth_data.is_empty() {
info!("Received auth callback data");
// Process the authentication data, its format is `globalprotectcallback:<data>` // Process the authentication data, its format is `globalprotectcallback:<data>`
return feed_auth_data(auth_data).await; return feed_auth_data(auth_data).await;
} }
@ -80,26 +81,16 @@ impl<'a> LaunchGuiHandler<'a> {
} }
async fn feed_auth_data(auth_data: &str) -> anyhow::Result<()> { async fn feed_auth_data(auth_data: &str) -> anyhow::Result<()> {
let (res_gui, res_cli) = tokio::join!(feed_auth_data_gui(auth_data), feed_auth_data_cli(auth_data)); let _ = tokio::join!(feed_auth_data_gui(auth_data), feed_auth_data_cli(auth_data));
if let Err(err) = res_gui {
info!("Failed to feed auth data to the GUI: {}", err);
}
if let Err(err) = res_cli {
info!("Failed to feed auth data to the CLI: {}", err);
}
// Cleanup the temporary file // Cleanup the temporary file
let html_file = temp_dir().join("gpauth.html"); let html_file = temp_dir().join("gpauth.html");
if let Err(err) = std::fs::remove_file(&html_file) { let _ = std::fs::remove_file(html_file);
info!("Failed to remove {}: {}", html_file.display(), err);
}
Ok(()) Ok(())
} }
async fn feed_auth_data_gui(auth_data: &str) -> anyhow::Result<()> { async fn feed_auth_data_gui(auth_data: &str) -> anyhow::Result<()> {
info!("Feeding auth data to the GUI");
let service_endpoint = http_endpoint().await?; let service_endpoint = http_endpoint().await?;
reqwest::Client::default() reqwest::Client::default()
@ -113,10 +104,7 @@ async fn feed_auth_data_gui(auth_data: &str) -> anyhow::Result<()> {
} }
async fn feed_auth_data_cli(auth_data: &str) -> anyhow::Result<()> { async fn feed_auth_data_cli(auth_data: &str) -> anyhow::Result<()> {
info!("Feeding auth data to the CLI"); let port = tokio::fs::read_to_string(GP_CLIENT_PORT_FILE).await?;
let port_file = temp_dir().join("gpcallback.port");
let port = tokio::fs::read_to_string(port_file).await?;
let mut stream = tokio::net::TcpStream::connect(format!("127.0.0.1:{}", port.trim())).await?; let mut stream = tokio::net::TcpStream::connect(format!("127.0.0.1:{}", port.trim())).await?;
stream.write_all(auth_data.as_bytes()).await?; stream.write_all(auth_data.as_bytes()).await?;
@ -136,7 +124,7 @@ async fn try_active_gui() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
fn get_log_file() -> anyhow::Result<PathBuf> { pub fn get_log_file() -> anyhow::Result<PathBuf> {
let dirs = ProjectDirs::from("com.yuezk", "GlobalProtect-openconnect", "gpclient") let dirs = ProjectDirs::from("com.yuezk", "GlobalProtect-openconnect", "gpclient")
.ok_or_else(|| anyhow::anyhow!("Failed to get project dirs"))?; .ok_or_else(|| anyhow::anyhow!("Failed to get project dirs"))?;

View File

@ -4,6 +4,7 @@ mod disconnect;
mod launch_gui; mod launch_gui;
pub(crate) const GP_CLIENT_LOCK_FILE: &str = "/var/run/gpclient.lock"; pub(crate) const GP_CLIENT_LOCK_FILE: &str = "/var/run/gpclient.lock";
pub(crate) const GP_CLIENT_PORT_FILE: &str = "/var/run/gpclient.port";
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {

View File

@ -1,23 +1,5 @@
# Changelog # Changelog
## 2.3.7 - 2024-08-16
- Fix the Rust type inference regression [issue in 1.80](https://github.com/rust-lang/rust/issues/125319).
## 2.3.6 - 2024-08-15
- CLI: enhance the `gpauth` command to support external browser authentication
- CLI: add the `--cookie-on-stdin` option to support read the cookie from stdin
- CLI: support usage: `gpauth <portal> --browser <browser> 2>/dev/null | sudo gpclient connect <portal> --cookie-on-stdin`
- CLI: fix the `--browser <browser>` option not working
## 2.3.5 - 2024-08-14
- Support configure `no-dtls` option
- GUI: fix the tray icon disk usage issue (#398)
- CLI: support specify the browser with `--browser <browser>` option (#405, #407, #397)
- CLI: fix the `--os` option not working
## 2.3.4 - 2024-07-08 ## 2.3.4 - 2024-07-08
- Support the Internal Host Detection (fix [#377](https://github.com/yuezk/GlobalProtect-openconnect/issues/377)) - Support the Internal Host Detection (fix [#377](https://github.com/yuezk/GlobalProtect-openconnect/issues/377))

View File

@ -85,6 +85,7 @@ impl SamlAuthData {
return Ok(auth_data); return Ok(auth_data);
} }
info!("Parsing SAML auth data...");
let auth_data = decode_to_string(auth_data).map_err(|e| { let auth_data = decode_to_string(auth_data).map_err(|e| {
warn!("Failed to decode SAML auth data: {}", e); warn!("Failed to decode SAML auth data: {}", e);
AuthDataParseError::Invalid AuthDataParseError::Invalid

View File

@ -1,10 +1,9 @@
use std::collections::HashMap; use std::collections::HashMap;
use anyhow::bail;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use specta::Type; use specta::Type;
use crate::auth::{SamlAuthData, SamlAuthResult}; use crate::auth::SamlAuthData;
#[derive(Debug, Serialize, Deserialize, Type, Clone)] #[derive(Debug, Serialize, Deserialize, Type, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -111,11 +110,11 @@ impl AuthCookieCredential {
pub struct CachedCredential { pub struct CachedCredential {
username: String, username: String,
password: Option<String>, password: Option<String>,
auth_cookie: Option<AuthCookieCredential>, auth_cookie: AuthCookieCredential,
} }
impl CachedCredential { impl CachedCredential {
pub fn new(username: String, password: Option<String>, auth_cookie: Option<AuthCookieCredential>) -> Self { pub fn new(username: String, password: Option<String>, auth_cookie: AuthCookieCredential) -> Self {
Self { Self {
username, username,
password, password,
@ -131,12 +130,12 @@ impl CachedCredential {
self.password.as_deref() self.password.as_deref()
} }
pub fn auth_cookie(&self) -> Option<&AuthCookieCredential> { pub fn auth_cookie(&self) -> &AuthCookieCredential {
self.auth_cookie.as_ref() &self.auth_cookie
} }
pub fn set_auth_cookie(&mut self, auth_cookie: AuthCookieCredential) { pub fn set_auth_cookie(&mut self, auth_cookie: AuthCookieCredential) {
self.auth_cookie = Some(auth_cookie); self.auth_cookie = auth_cookie;
} }
pub fn set_username(&mut self, username: String) { pub fn set_username(&mut self, username: String) {
@ -150,7 +149,11 @@ impl CachedCredential {
impl From<PasswordCredential> for CachedCredential { impl From<PasswordCredential> for CachedCredential {
fn from(value: PasswordCredential) -> Self { fn from(value: PasswordCredential) -> Self {
Self::new(value.username().to_owned(), Some(value.password().to_owned()), None) Self::new(
value.username().to_owned(),
Some(value.password().to_owned()),
AuthCookieCredential::new("", "", ""),
)
} }
} }
#[derive(Debug, Serialize, Deserialize, Type, Clone)] #[derive(Debug, Serialize, Deserialize, Type, Clone)]
@ -194,16 +197,11 @@ impl Credential {
Some(cred.prelogon_user_auth_cookie()), Some(cred.prelogon_user_auth_cookie()),
None, None,
), ),
// Use the empty string as the password if auth_cookie is present
Credential::Cached(cred) => ( Credential::Cached(cred) => (
if cred.auth_cookie.is_some() { cred.password(),
None
} else {
cred.password()
},
None, None,
cred.auth_cookie.as_ref().map(|c| c.user_auth_cookie()), Some(cred.auth_cookie.user_auth_cookie()),
cred.auth_cookie.as_ref().map(|c| c.prelogon_user_auth_cookie()), Some(cred.auth_cookie.prelogon_user_auth_cookie()),
None, None,
), ),
}; };
@ -232,17 +230,6 @@ impl From<SamlAuthData> for Credential {
} }
} }
impl TryFrom<SamlAuthResult> for Credential {
type Error = anyhow::Error;
fn try_from(value: SamlAuthResult) -> anyhow::Result<Self> {
match value {
SamlAuthResult::Success(auth_data) => Ok(Self::from(auth_data)),
SamlAuthResult::Failure(err) => bail!(err),
}
}
}
impl From<PasswordCredential> for Credential { impl From<PasswordCredential> for Credential {
fn from(value: PasswordCredential) -> Self { fn from(value: PasswordCredential) -> Self {
Self::Password(value) Self::Password(value)

View File

@ -29,7 +29,7 @@ pub async fn gateway_login(gateway: &str, cred: &Credential, gp_params: &GpParam
params.extend(extra_params); params.extend(extra_params);
params.insert("server", &gateway); params.insert("server", &gateway);
info!("Perform gateway login, user_agent: {}", gp_params.user_agent()); info!("Gateway login, user_agent: {}", gp_params.user_agent());
let res = client let res = client
.post(&login_url) .post(&login_url)

View File

@ -109,7 +109,7 @@ pub async fn retrieve_config(portal: &str, cred: &Credential, gp_params: &GpPara
params.insert("server", &server); params.insert("server", &server);
params.insert("host", &server); params.insert("host", &server);
info!("Retrieve the portal config, user_agent: {}", gp_params.user_agent()); info!("Portal config, user_agent: {}", gp_params.user_agent());
let res = client let res = client
.post(&url) .post(&url)

View File

@ -116,8 +116,6 @@ pub async fn prelogin(portal: &str, gp_params: &GpParams) -> anyhow::Result<Prel
let client = Client::try_from(gp_params)?; let client = Client::try_from(gp_params)?;
info!("Perform prelogin, user_agent: {}", gp_params.user_agent());
let res = client let res = client
.post(&prelogin_url) .post(&prelogin_url)
.form(&params) .form(&params)

View File

@ -19,7 +19,7 @@ pub struct SamlAuthLauncher<'a> {
ignore_tls_errors: bool, ignore_tls_errors: bool,
clean: bool, clean: bool,
default_browser: bool, default_browser: bool,
browser: Option<&'a str>, external_browser: Option<&'a str>,
} }
impl<'a> SamlAuthLauncher<'a> { impl<'a> SamlAuthLauncher<'a> {
@ -36,7 +36,7 @@ impl<'a> SamlAuthLauncher<'a> {
ignore_tls_errors: false, ignore_tls_errors: false,
clean: false, clean: false,
default_browser: false, default_browser: false,
browser: None, external_browser: None,
} }
} }
@ -90,13 +90,13 @@ impl<'a> SamlAuthLauncher<'a> {
self self
} }
pub fn browser(mut self, browser: Option<&'a str>) -> Self { pub fn external_browser(mut self, external_browser: Option<&'a str>) -> Self {
self.browser = browser; self.external_browser = external_browser;
self self
} }
/// Launch the authenticator binary as the current user or SUDO_USER if available. /// Launch the authenticator binary as the current user or SUDO_USER if available.
pub async fn launch(self) -> anyhow::Result<Credential> { pub async fn launch(self) -> anyhow::Result<Option<Credential>> {
let mut auth_cmd = Command::new(GP_AUTH_BINARY); let mut auth_cmd = Command::new(GP_AUTH_BINARY);
auth_cmd.arg(self.server); auth_cmd.arg(self.server);
@ -140,8 +140,8 @@ impl<'a> SamlAuthLauncher<'a> {
auth_cmd.arg("--default-browser"); auth_cmd.arg("--default-browser");
} }
if let Some(browser) = self.browser { if let Some(external_browser) = self.external_browser {
auth_cmd.arg("--browser").arg(browser); auth_cmd.arg("--external-browser").arg(external_browser);
} }
let mut non_root_cmd = auth_cmd.into_non_root()?; let mut non_root_cmd = auth_cmd.into_non_root()?;
@ -152,10 +152,17 @@ impl<'a> SamlAuthLauncher<'a> {
.wait_with_output() .wait_with_output()
.await?; .await?;
if self.default_browser {
return Ok(None);
}
let Ok(auth_result) = serde_json::from_slice::<SamlAuthResult>(&output.stdout) else { let Ok(auth_result) = serde_json::from_slice::<SamlAuthResult>(&output.stdout) else {
bail!("Failed to parse auth data") bail!("Failed to parse auth data")
}; };
Credential::try_from(auth_result) match auth_result {
SamlAuthResult::Success(auth_data) => Ok(Some(Credential::from(auth_data))),
SamlAuthResult::Failure(msg) => bail!(msg),
}
} }
} }

View File

@ -19,7 +19,7 @@ impl BrowserAuthenticator<'_> {
pub fn new_with_browser<'a>(auth_request: &'a str, browser: &'a str) -> BrowserAuthenticator<'a> { pub fn new_with_browser<'a>(auth_request: &'a str, browser: &'a str) -> BrowserAuthenticator<'a> {
BrowserAuthenticator { BrowserAuthenticator {
auth_request, auth_request,
browser: if browser == "default" { None } else { Some(browser) }, browser: Some(browser),
} }
} }

View File

@ -42,8 +42,6 @@
overrideMain = {...}: { overrideMain = {...}: {
postPatch = '' postPatch = ''
substituteInPlace crates/common/src/vpn_utils.rs \
--replace-fail /etc/vpnc/vpnc-script ${pkgs.vpnc-scripts}/bin/vpnc-script
substituteInPlace crates/gpapi/src/lib.rs \ substituteInPlace crates/gpapi/src/lib.rs \
--replace-fail /usr/bin/gpclient $out/bin/gpclient \ --replace-fail /usr/bin/gpclient $out/bin/gpclient \
--replace-fail /usr/bin/gpservice $out/bin/gpservice \ --replace-fail /usr/bin/gpservice $out/bin/gpservice \