From fb0645594a91c41b44be993e06ee0d58b2e2f544 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 10 May 2026 10:55:57 +0800 Subject: [PATCH] fix: preserve SAML portal user auth cookie --- crates/gpapi/src/auth.rs | 7 +++- crates/gpapi/src/credential.rs | 63 ++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/crates/gpapi/src/auth.rs b/crates/gpapi/src/auth.rs index ebd7dd7..1db8357 100644 --- a/crates/gpapi/src/auth.rs +++ b/crates/gpapi/src/auth.rs @@ -122,6 +122,10 @@ impl SamlAuthData { self.prelogin_cookie.as_deref() } + pub fn portal_userauthcookie(&self) -> Option<&str> { + self.portal_userauthcookie.as_deref() + } + pub fn token(&self) -> Option<&str> { self.token.as_deref() } @@ -160,11 +164,12 @@ mod tests { #[test] fn auth_data_from_gpcallback_non_cas() { - let auth_data = "PGh0bWw+PCEtLSA8c2FtbC1hdXRoLXN0YXR1cz4xPC9zYW1sLWF1dGgtc3RhdHVzPjxwcmVsb2dpbi1jb29raWU+cHJlbG9naW4tY29va2llPC9wcmVsb2dpbi1jb29raWU+PHNhbWwtdXNlcm5hbWU+eHl6QGVtYWlsLmNvbTwvc2FtbC11c2VybmFtZT48c2FtbC1zbG8+bm88L3NhbWwtc2xvPjxzYW1sLVNlc3Npb25Ob3RPbk9yQWZ0ZXI+PC9zYW1sLVNlc3Npb25Ob3RPbk9yQWZ0ZXI+IC0tPjwvaHRtbD4="; + let auth_data = "PGh0bWw+PCEtLSA8c2FtbC1hdXRoLXN0YXR1cz4xPC9zYW1sLWF1dGgtc3RhdHVzPjxwcmVsb2dpbi1jb29raWU+cHJlbG9naW4tY29va2llPC9wcmVsb2dpbi1jb29raWU+PHBvcnRhbC11c2VyYXV0aGNvb2tpZT5wb3J0YWwtdXNlcmF1dGhjb29raWU8L3BvcnRhbC11c2VyYXV0aGNvb2tpZT48c2FtbC11c2VybmFtZT54eXpAZW1haWwuY29tPC9zYW1sLXVzZXJuYW1lPjxzYW1sLXNsbz5ubzwvc2FtbC1zbG8+IC0tPjwvaHRtbD4="; let auth_data = SamlAuthData::from_gpcallback(auth_data).unwrap(); assert_eq!(auth_data.username(), "xyz@email.com"); assert_eq!(auth_data.prelogin_cookie(), Some("prelogin-cookie")); + assert_eq!(auth_data.portal_userauthcookie(), Some("portal-userauthcookie")); } } diff --git a/crates/gpapi/src/credential.rs b/crates/gpapi/src/credential.rs index a745df1..b057af2 100644 --- a/crates/gpapi/src/credential.rs +++ b/crates/gpapi/src/credential.rs @@ -41,14 +41,21 @@ impl From<&CachedCredential> for PasswordCredential { pub struct PreloginCredential { username: String, prelogin_cookie: Option, + portal_userauthcookie: Option, token: Option, } impl PreloginCredential { - pub fn new(username: &str, prelogin_cookie: Option<&str>, token: Option<&str>) -> Self { + pub fn new( + username: &str, + prelogin_cookie: Option<&str>, + portal_userauthcookie: Option<&str>, + token: Option<&str>, + ) -> Self { Self { username: username.to_string(), prelogin_cookie: prelogin_cookie.map(|s| s.to_string()), + portal_userauthcookie: portal_userauthcookie.map(|s| s.to_string()), token: token.map(|s| s.to_string()), } } @@ -61,6 +68,10 @@ impl PreloginCredential { self.prelogin_cookie.as_deref() } + pub fn portal_userauthcookie(&self) -> Option<&str> { + self.portal_userauthcookie.as_deref() + } + pub fn token(&self) -> Option<&str> { self.token.as_deref() } @@ -70,9 +81,10 @@ impl From for PreloginCredential { fn from(value: SamlAuthData) -> Self { let username = value.username().to_string(); let prelogin_cookie = value.prelogin_cookie(); + let portal_userauthcookie = value.portal_userauthcookie(); let token = value.token(); - Self::new(&username, prelogin_cookie, token) + Self::new(&username, prelogin_cookie, portal_userauthcookie, token) } } @@ -186,7 +198,13 @@ impl Credential { let (passwd, prelogin_cookie, portal_userauthcookie, portal_prelogonuserauthcookie, token) = match self { Credential::Password(cred) => (Some(cred.password()), None, None, None, None), - Credential::Prelogin(cred) => (None, cred.prelogin_cookie(), None, None, cred.token()), + Credential::Prelogin(cred) => ( + None, + cred.prelogin_cookie(), + cred.portal_userauthcookie(), + None, + cred.token(), + ), Credential::AuthCookie(cred) => ( None, None, @@ -244,6 +262,45 @@ impl From for Credential { } } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn prelogin_credential_params_include_saml_cookies() { + let auth_data = SamlAuthData::new( + Some("alice@example.com".to_string()), + Some("prelogin-cookie".to_string()), + Some("portal-userauthcookie".to_string()), + ) + .unwrap(); + + let cred = Credential::from(auth_data); + let params = cred.to_params(); + + assert_eq!(params.get("user"), Some(&"alice@example.com")); + assert_eq!(params.get("passwd"), Some(&"")); + assert_eq!(params.get("prelogin-cookie"), Some(&"prelogin-cookie")); + assert_eq!(params.get("portal-userauthcookie"), Some(&"portal-userauthcookie")); + assert_eq!(params.get("portal-prelogonuserauthcookie"), Some(&"")); + } + + #[test] + fn prelogin_credential_params_keep_cas_token_without_cookies() { + let auth_data = + SamlAuthData::from_gpcallback("globalprotectcallback:cas-as=1&un=alice@example.com&token=cas-token").unwrap(); + + let cred = Credential::from(auth_data); + let params = cred.to_params(); + + assert_eq!(params.get("user"), Some(&"alice@example.com")); + assert_eq!(params.get("prelogin-cookie"), Some(&"")); + assert_eq!(params.get("portal-userauthcookie"), Some(&"")); + assert_eq!(params.get("portal-prelogonuserauthcookie"), Some(&"")); + assert_eq!(params.get("token"), Some(&"cas-token")); + } +} + impl From<&AuthCookieCredential> for Credential { fn from(value: &AuthCookieCredential) -> Self { Self::AuthCookie(value.clone())