fix: handle authentication cancellation in auth results

This commit is contained in:
Kevin Yue committed 2026-07-04 18:37:16 +08:00
1 parent d83bb22c58
commit 6e01164a56
4 files changed
+35 -5

No files matched your search

+2 -1
View File
@@ -1,7 +1,7 @@
use auth::{BrowserAuthenticator, auth_prelogin}; use auth::{BrowserAuthenticator, auth_prelogin};
use clap::Parser; use clap::Parser;
use gpapi::{ use gpapi::{
auth::{SamlAuthData, SamlAuthResult}, auth::{AuthenticationCancelled, SamlAuthData, SamlAuthResult},
clap::{Args, InfoLevelVerbosity, args::Os, handle_error}, clap::{Args, InfoLevelVerbosity, args::Os, handle_error},
gp_params::GpParams, gp_params::GpParams,
os_profile::{ClientOs, OsProfile}, os_profile::{ClientOs, OsProfile},
@@ -232,6 +232,7 @@ pub fn print_auth_result(auth_result: anyhow::Result<SamlAuthData>, host_id: Opt
}; };
SamlAuthResult::Success(auth_data) SamlAuthResult::Success(auth_data)
} }
Err(err) if err.downcast_ref::<AuthenticationCancelled>().is_some() => SamlAuthResult::Cancelled,
Err(err) => SamlAuthResult::Failure(format!("{}", err)), Err(err) => SamlAuthResult::Failure(format!("{}", err)),
}; };
+2 -2
View File
@@ -2,7 +2,7 @@ use std::{sync::Arc, time::Duration};
use anyhow::bail; use anyhow::bail;
use gpapi::{ use gpapi::{
auth::SamlAuthData, auth::{AuthenticationCancelled, SamlAuthData},
gp_params::GpParams, gp_params::GpParams,
os_profile::{WebviewUserAgent, WebviewUserAgentTransform}, os_profile::{WebviewUserAgent, WebviewUserAgentTransform},
utils::redact::redact_uri, utils::redact::redact_uri,
@@ -130,7 +130,7 @@ impl<'a> WebviewAuthenticator<'a> {
loop { loop {
match auth_messenger.subscribe().await? { match auth_messenger.subscribe().await? {
AuthEvent::Close => bail!("Authentication cancelled"), AuthEvent::Close => bail!(AuthenticationCancelled),
AuthEvent::RaiseWindow => self.raise_window(&auth_window), AuthEvent::RaiseWindow => self.raise_window(&auth_window),
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
AuthEvent::Error(AuthError::TlsError) => bail!(gpapi::error::PortalError::TlsError), AuthEvent::Error(AuthError::TlsError) => bail!(gpapi::error::PortalError::TlsError),
+29 -1
View File
@@ -1,4 +1,7 @@
use std::borrow::{Borrow, Cow}; use std::{
borrow::{Borrow, Cow},
fmt,
};
use anyhow::bail; use anyhow::bail;
use log::{info, warn}; use log::{info, warn};
@@ -25,13 +28,26 @@ pub struct SamlAuthData {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum SamlAuthResult { pub enum SamlAuthResult {
Success(SamlAuthData), Success(SamlAuthData),
Cancelled,
Failure(String), 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 { impl SamlAuthResult {
pub fn is_success(&self) -> bool { pub fn is_success(&self) -> bool {
match self { match self {
SamlAuthResult::Success(_) => true, SamlAuthResult::Success(_) => true,
SamlAuthResult::Cancelled => false,
SamlAuthResult::Failure(_) => false, SamlAuthResult::Failure(_) => false,
} }
} }
@@ -39,6 +55,7 @@ impl SamlAuthResult {
pub fn host_id(&self) -> Option<&str> { pub fn host_id(&self) -> Option<&str> {
match self { match self {
SamlAuthResult::Success(auth_data) => auth_data.host_id(), SamlAuthResult::Success(auth_data) => auth_data.host_id(),
SamlAuthResult::Cancelled => None,
SamlAuthResult::Failure(_) => None, SamlAuthResult::Failure(_) => None,
} }
} }
@@ -246,4 +263,15 @@ mod tests {
let value = serde_json::to_value(result).unwrap(); let value = serde_json::to_value(result).unwrap();
assert!(value["success"].get("hostId").is_none()); 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);
}
} }
+2 -1
View File
@@ -2,7 +2,7 @@ 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::{AuthenticationCancelled, SamlAuthData, SamlAuthResult};
#[derive(Debug, Serialize, Deserialize, Type, Clone)] #[derive(Debug, Serialize, Deserialize, Type, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@@ -250,6 +250,7 @@ impl TryFrom<SamlAuthResult> for Credential {
fn try_from(value: SamlAuthResult) -> anyhow::Result<Self> { fn try_from(value: SamlAuthResult) -> anyhow::Result<Self> {
match value { match value {
SamlAuthResult::Success(auth_data) => Ok(Self::from(auth_data)), SamlAuthResult::Success(auth_data) => Ok(Self::from(auth_data)),
SamlAuthResult::Cancelled => bail!(AuthenticationCancelled),
SamlAuthResult::Failure(err) => bail!(err), SamlAuthResult::Failure(err) => bail!(err),
} }
} }