fix: preserve SAML portal user auth cookie

This commit is contained in:
Kevin Yue committed 2026-05-10 10:55:57 +08:00
1 parent 7d63836e35
commit fb0645594a
2 files changed
+66 -4

No files matched your search

+6 -1
View File
@@ -122,6 +122,10 @@ impl SamlAuthData {
self.prelogin_cookie.as_deref() self.prelogin_cookie.as_deref()
} }
pub fn portal_userauthcookie(&self) -> Option<&str> {
self.portal_userauthcookie.as_deref()
}
pub fn token(&self) -> Option<&str> { pub fn token(&self) -> Option<&str> {
self.token.as_deref() self.token.as_deref()
} }
@@ -160,11 +164,12 @@ mod tests {
#[test] #[test]
fn auth_data_from_gpcallback_non_cas() { 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(); let auth_data = SamlAuthData::from_gpcallback(auth_data).unwrap();
assert_eq!(auth_data.username(), "[email protected]"); assert_eq!(auth_data.username(), "[email protected]");
assert_eq!(auth_data.prelogin_cookie(), Some("prelogin-cookie")); assert_eq!(auth_data.prelogin_cookie(), Some("prelogin-cookie"));
assert_eq!(auth_data.portal_userauthcookie(), Some("portal-userauthcookie"));
} }
} }
+60 -3
View File
@@ -41,14 +41,21 @@ impl From<&CachedCredential> for PasswordCredential {
pub struct PreloginCredential { pub struct PreloginCredential {
username: String, username: String,
prelogin_cookie: Option<String>, prelogin_cookie: Option<String>,
portal_userauthcookie: Option<String>,
token: Option<String>, token: Option<String>,
} }
impl PreloginCredential { 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 { Self {
username: username.to_string(), username: username.to_string(),
prelogin_cookie: prelogin_cookie.map(|s| s.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()), token: token.map(|s| s.to_string()),
} }
} }
@@ -61,6 +68,10 @@ impl PreloginCredential {
self.prelogin_cookie.as_deref() self.prelogin_cookie.as_deref()
} }
pub fn portal_userauthcookie(&self) -> Option<&str> {
self.portal_userauthcookie.as_deref()
}
pub fn token(&self) -> Option<&str> { pub fn token(&self) -> Option<&str> {
self.token.as_deref() self.token.as_deref()
} }
@@ -70,9 +81,10 @@ impl From<SamlAuthData> for PreloginCredential {
fn from(value: SamlAuthData) -> Self { fn from(value: SamlAuthData) -> Self {
let username = value.username().to_string(); let username = value.username().to_string();
let prelogin_cookie = value.prelogin_cookie(); let prelogin_cookie = value.prelogin_cookie();
let portal_userauthcookie = value.portal_userauthcookie();
let token = value.token(); 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 { let (passwd, prelogin_cookie, portal_userauthcookie, portal_prelogonuserauthcookie, token) = match self {
Credential::Password(cred) => (Some(cred.password()), None, None, None, None), 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) => ( Credential::AuthCookie(cred) => (
None, None,
None, None,
@@ -244,6 +262,45 @@ impl From<PasswordCredential> for Credential {
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prelogin_credential_params_include_saml_cookies() {
let auth_data = SamlAuthData::new(
Some("[email protected]".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(&"[email protected]"));
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&[email protected]&token=cas-token").unwrap();
let cred = Credential::from(auth_data);
let params = cred.to_params();
assert_eq!(params.get("user"), Some(&"[email protected]"));
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 { impl From<&AuthCookieCredential> for Credential {
fn from(value: &AuthCookieCredential) -> Self { fn from(value: &AuthCookieCredential) -> Self {
Self::AuthCookie(value.clone()) Self::AuthCookie(value.clone())