mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-18 22:16:18 -05:00
fix: handle authentication cancellation in auth results
This commit is contained in:
5 files changed
+36
-6
No files matched your search
@@ -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<SamlAuthData>, host_id: Opt
|
||||
};
|
||||
SamlAuthResult::Success(auth_data)
|
||||
}
|
||||
Err(err) if err.downcast_ref::<AuthenticationCancelled>().is_some() => SamlAuthResult::Cancelled,
|
||||
Err(err) => SamlAuthResult::Failure(format!("{}", err)),
|
||||
};
|
||||
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<SamlAuthResult> for Credential {
|
||||
fn try_from(value: SamlAuthResult) -> anyhow::Result<Self> {
|
||||
match value {
|
||||
SamlAuthResult::Success(auth_data) => Ok(Self::from(auth_data)),
|
||||
SamlAuthResult::Cancelled => bail!(AuthenticationCancelled),
|
||||
SamlAuthResult::Failure(err) => bail!(err),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,4 +181,4 @@ fn main() {
|
||||
.include("src/ffi")
|
||||
.include(&oc_dst.join("include"))
|
||||
.compile("vpn");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user