diff --git a/apps/gpauth/src/cli.rs b/apps/gpauth/src/cli.rs index f84feae..58d686b 100644 --- a/apps/gpauth/src/cli.rs +++ b/apps/gpauth/src/cli.rs @@ -1,7 +1,7 @@ use auth::{BrowserAuthenticator, auth_prelogin}; use clap::Parser; use gpapi::{ - auth::{SamlAuthData, SamlAuthResult}, + auth::{AuthenticationCancelled, SamlAuthData, SamlAuthResult}, clap::{Args, InfoLevelVerbosity, args::Os, handle_error}, gp_params::GpParams, os_profile::{ClientOs, OsProfile}, @@ -232,6 +232,7 @@ pub fn print_auth_result(auth_result: anyhow::Result, host_id: Opt }; SamlAuthResult::Success(auth_data) } + Err(err) if err.downcast_ref::().is_some() => SamlAuthResult::Cancelled, Err(err) => SamlAuthResult::Failure(format!("{}", err)), }; diff --git a/crates/auth/src/webview/webview_auth.rs b/crates/auth/src/webview/webview_auth.rs index d695832..e03da1f 100644 --- a/crates/auth/src/webview/webview_auth.rs +++ b/crates/auth/src/webview/webview_auth.rs @@ -2,7 +2,7 @@ use std::{sync::Arc, time::Duration}; use anyhow::bail; use gpapi::{ - auth::SamlAuthData, + auth::{AuthenticationCancelled, SamlAuthData}, gp_params::GpParams, os_profile::{WebviewUserAgent, WebviewUserAgentTransform}, utils::redact::redact_uri, @@ -130,7 +130,7 @@ impl<'a> WebviewAuthenticator<'a> { loop { match auth_messenger.subscribe().await? { - AuthEvent::Close => bail!("Authentication cancelled"), + AuthEvent::Close => bail!(AuthenticationCancelled), AuthEvent::RaiseWindow => self.raise_window(&auth_window), #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] AuthEvent::Error(AuthError::TlsError) => bail!(gpapi::error::PortalError::TlsError), diff --git a/crates/gpapi/src/auth.rs b/crates/gpapi/src/auth.rs index f786771..4657211 100644 --- a/crates/gpapi/src/auth.rs +++ b/crates/gpapi/src/auth.rs @@ -1,4 +1,7 @@ -use std::borrow::{Borrow, Cow}; +use std::{ + borrow::{Borrow, Cow}, + fmt, +}; use anyhow::bail; use log::{info, warn}; @@ -25,13 +28,26 @@ pub struct SamlAuthData { #[serde(rename_all = "camelCase")] pub enum SamlAuthResult { Success(SamlAuthData), + Cancelled, Failure(String), } +#[derive(Debug)] +pub struct AuthenticationCancelled; + +impl fmt::Display for AuthenticationCancelled { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("Authentication cancelled") + } +} + +impl std::error::Error for AuthenticationCancelled {} + impl SamlAuthResult { pub fn is_success(&self) -> bool { match self { SamlAuthResult::Success(_) => true, + SamlAuthResult::Cancelled => false, SamlAuthResult::Failure(_) => false, } } @@ -39,6 +55,7 @@ impl SamlAuthResult { pub fn host_id(&self) -> Option<&str> { match self { SamlAuthResult::Success(auth_data) => auth_data.host_id(), + SamlAuthResult::Cancelled => None, SamlAuthResult::Failure(_) => None, } } @@ -246,4 +263,15 @@ mod tests { let value = serde_json::to_value(result).unwrap(); assert!(value["success"].get("hostId").is_none()); } + + #[test] + fn auth_result_cancelled_is_structured() { + let value = serde_json::to_value(SamlAuthResult::Cancelled).unwrap(); + + assert_eq!(value, "cancelled"); + + let result: SamlAuthResult = serde_json::from_value(value).unwrap(); + assert!(!result.is_success()); + assert_eq!(result.host_id(), None); + } } diff --git a/crates/gpapi/src/credential.rs b/crates/gpapi/src/credential.rs index 6c94537..2617fcc 100644 --- a/crates/gpapi/src/credential.rs +++ b/crates/gpapi/src/credential.rs @@ -2,7 +2,7 @@ use anyhow::bail; use serde::{Deserialize, Serialize}; use specta::Type; -use crate::auth::{SamlAuthData, SamlAuthResult}; +use crate::auth::{AuthenticationCancelled, SamlAuthData, SamlAuthResult}; #[derive(Debug, Serialize, Deserialize, Type, Clone)] #[serde(rename_all = "camelCase")] @@ -250,6 +250,7 @@ impl TryFrom for Credential { fn try_from(value: SamlAuthResult) -> anyhow::Result { match value { SamlAuthResult::Success(auth_data) => Ok(Self::from(auth_data)), + SamlAuthResult::Cancelled => bail!(AuthenticationCancelled), SamlAuthResult::Failure(err) => bail!(err), } } diff --git a/crates/openconnect/build.rs b/crates/openconnect/build.rs index d95977a..4a7fef6 100644 --- a/crates/openconnect/build.rs +++ b/crates/openconnect/build.rs @@ -181,4 +181,4 @@ fn main() { .include("src/ffi") .include(&oc_dst.join("include")) .compile("vpn"); -} \ No newline at end of file +}