refactor: align GP profile and packaging flow

Main changes:

- Refactor connect/auth flows around a single OS profile and host identity model, including gateway fallback and cookie-on-stdin handling.

- Align request parameters, HIP host-id propagation, SAML webview user-agent behavior, and portal default-browser handling with observed official client behavior.

- Add opt-in cookie cache support and Microsoft Defender HIP reporting while keeping host-id validation explicit.

- Add BSD build, packaging, release artifact, and provenance support with FreeBSD/OpenBSD path and dependency cleanup.

- Update NixOS install documentation and dependency compatibility fixes.
This commit is contained in:
Kevin Yue committed 2026-06-19 21:23:29 +08:00
1 parent f98e0336dc
commit 888dc93643
92 files changed
+9847 -2868

No files matched your search

+139 -38
View File
@@ -1,5 +1,3 @@
use std::collections::HashMap;
use anyhow::bail;
use serde::{Deserialize, Serialize};
use specta::Type;
@@ -41,14 +39,21 @@ impl From<&CachedCredential> for PasswordCredential {
pub struct PreloginCredential {
username: String,
prelogin_cookie: Option<String>,
portal_userauthcookie: Option<String>,
token: Option<String>,
}
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 +66,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 +79,10 @@ impl From<SamlAuthData> 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)
}
}
@@ -82,6 +92,8 @@ pub struct AuthCookieCredential {
username: String,
user_auth_cookie: String,
prelogon_user_auth_cookie: String,
#[serde(skip)]
password: Option<String>,
}
impl AuthCookieCredential {
@@ -90,9 +102,16 @@ impl AuthCookieCredential {
username: username.to_string(),
user_auth_cookie: user_auth_cookie.to_string(),
prelogon_user_auth_cookie: prelogon_user_auth_cookie.to_string(),
password: None,
}
}
pub fn with_password(mut self, password: impl Into<String>) -> Self {
let password = password.into();
self.password = (!password.trim().is_empty()).then_some(password);
self
}
pub fn username(&self) -> &str {
&self.username
}
@@ -104,6 +123,19 @@ impl AuthCookieCredential {
pub fn prelogon_user_auth_cookie(&self) -> &str {
&self.prelogon_user_auth_cookie
}
pub fn password(&self) -> Option<&str> {
self.password.as_deref()
}
pub fn can_authenticate_gateway(&self) -> bool {
is_gateway_auth_cookie(&self.user_auth_cookie) || is_gateway_auth_cookie(&self.prelogon_user_auth_cookie)
}
}
fn is_gateway_auth_cookie(value: &str) -> bool {
let value = value.trim();
!value.is_empty() && value != "empty"
}
#[derive(Debug, Serialize, Deserialize, Type, Clone)]
@@ -135,6 +167,13 @@ impl CachedCredential {
self.auth_cookie.as_ref()
}
pub fn can_authenticate_gateway(&self) -> bool {
self
.auth_cookie
.as_ref()
.is_some_and(AuthCookieCredential::can_authenticate_gateway)
}
pub fn set_auth_cookie(&mut self, auth_cookie: AuthCookieCredential) {
self.auth_cookie = Some(auth_cookie);
}
@@ -180,42 +219,20 @@ impl Credential {
}
}
pub fn to_params(&self) -> HashMap<&str, &str> {
let mut params = HashMap::new();
params.insert("user", self.username());
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::AuthCookie(cred) => (
None,
None,
Some(cred.user_auth_cookie()),
Some(cred.prelogon_user_auth_cookie()),
None,
),
Credential::Cached(cred) => (
cred.password(),
None,
cred.auth_cookie.as_ref().map(|c| c.user_auth_cookie()),
cred.auth_cookie.as_ref().map(|c| c.prelogon_user_auth_cookie()),
None,
),
};
params.insert("passwd", passwd.unwrap_or_default());
params.insert("prelogin-cookie", prelogin_cookie.unwrap_or_default());
params.insert("portal-userauthcookie", portal_userauthcookie.unwrap_or_default());
params.insert(
"portal-prelogonuserauthcookie",
portal_prelogonuserauthcookie.unwrap_or_default(),
);
if let Some(token) = token {
params.insert("token", token);
pub fn password(&self) -> Option<&str> {
match self {
Credential::Password(cred) => Some(cred.password()),
Credential::Cached(cred) => cred.password(),
Credential::Prelogin(_) | Credential::AuthCookie(_) => None,
}
}
params
pub fn can_authenticate_gateway(&self) -> bool {
match self {
Credential::AuthCookie(cred) => cred.can_authenticate_gateway(),
Credential::Cached(cred) => cred.can_authenticate_gateway(),
Credential::Password(_) | Credential::Prelogin(_) => true,
}
}
}
@@ -255,3 +272,87 @@ impl From<&CachedCredential> for Credential {
Self::Cached(value.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prelogin_credential_keeps_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 Credential::Prelogin(prelogin) = cred else {
panic!("expected prelogin credential");
};
assert_eq!(prelogin.username(), "[email protected]");
assert_eq!(prelogin.prelogin_cookie(), Some("prelogin-cookie"));
assert_eq!(prelogin.portal_userauthcookie(), Some("portal-userauthcookie"));
assert_eq!(prelogin.token(), None);
}
#[test]
fn prelogin_credential_keeps_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 Credential::Prelogin(prelogin) = cred else {
panic!("expected prelogin credential");
};
assert_eq!(prelogin.username(), "[email protected]");
assert_eq!(prelogin.prelogin_cookie(), None);
assert_eq!(prelogin.portal_userauthcookie(), None);
assert_eq!(prelogin.token(), Some("cas-token"));
}
#[test]
fn auth_cookie_runtime_password_is_not_serialized() {
let value = serde_json::to_value(
AuthCookieCredential::new("alice", "user-cookie", "prelogon-cookie").with_password("secret"),
)
.unwrap();
assert_eq!(value.get("password"), None);
}
#[test]
fn auth_cookie_with_empty_sentinels_cannot_authenticate_gateway() {
let cred = AuthCookieCredential::new("alice", "empty", "empty");
assert!(!cred.can_authenticate_gateway());
}
#[test]
fn auth_cookie_with_blank_values_cannot_authenticate_gateway() {
let cred = AuthCookieCredential::new("alice", "", " ");
assert!(!cred.can_authenticate_gateway());
}
#[test]
fn auth_cookie_with_real_cookie_can_authenticate_gateway() {
let cred = AuthCookieCredential::new("alice", "empty", "portal-prelogon-cookie");
assert!(cred.can_authenticate_gateway());
}
#[test]
fn cached_credential_without_real_auth_cookie_cannot_authenticate_gateway() {
let cred = CachedCredential::new(
"alice".to_string(),
None,
Some(AuthCookieCredential::new("alice", "empty", "empty")),
);
assert!(!cred.can_authenticate_gateway());
assert!(!Credential::from(&cred).can_authenticate_gateway());
}
}