use crate::config::app::AuthConfig;
use garrison::prelude::GarrisonConfig;
pub const DEFAULT_TOKEN_EXPIRATION_SECS: i64 = 3600;
pub fn map_auth_config_to_garrison(auth: &AuthConfig) -> GarrisonConfig {
let mut config = GarrisonConfig::default_config();
config.timeout = match (auth.token_expiration_seconds, auth.token_expiration_hours) {
(Some(secs), _) if secs > 0 => secs,
(_, Some(hours)) if hours > 0 => hours * 3600,
_ => DEFAULT_TOKEN_EXPIRATION_SECS,
};
if let Some(ref secret) = auth.jwt_secret {
config.jwt_secret = secret.clone().into();
}
config.token_style = "jwt".to_string();
config.throw_on_not_login = auth.enabled;
config.frontend_separation = true;
config.is_read_cookie = false;
config.is_read_header = true;
config
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::app::AuthConfig;
fn make_auth_config() -> AuthConfig {
AuthConfig {
enabled: true,
jwt_secret: Some("test-jwt-secret-at-least-32-chars!!".to_string()),
token_expiration_hours: Some(24),
token_expiration_seconds: None,
default_admin_username: Some("admin".to_string()),
default_admin_password: Some("SecurePass123!".to_string()),
csrf: crate::config::app::CsrfConfig::default(),
trusted_proxies: vec![],
}
}
#[test]
fn test_map_timeout_from_hours() {
let auth = make_auth_config();
let garrison = map_auth_config_to_garrison(&auth);
assert_eq!(garrison.timeout, 24 * 3600);
}
#[test]
fn test_map_timeout_seconds_override() {
let mut auth = make_auth_config();
auth.token_expiration_seconds = Some(5);
let garrison = map_auth_config_to_garrison(&auth);
assert_eq!(garrison.timeout, 5);
}
#[test]
fn test_map_timeout_seconds_non_positive_ignored() {
let mut auth = make_auth_config();
auth.token_expiration_seconds = Some(0);
let garrison = map_auth_config_to_garrison(&auth);
assert_eq!(garrison.timeout, 24 * 3600);
}
#[test]
fn test_map_timeout_default_is_one_hour_when_none() {
let mut auth = make_auth_config();
auth.token_expiration_hours = None;
let garrison = map_auth_config_to_garrison(&auth);
assert_eq!(garrison.timeout, 3600);
}
#[test]
fn test_map_timeout_zero_or_negative_falls_back_to_default() {
for bad in [Some(0), Some(-5)] {
let mut auth = make_auth_config();
auth.token_expiration_hours = bad;
let garrison = map_auth_config_to_garrison(&auth);
assert_eq!(garrison.timeout, 3600);
}
}
#[test]
fn test_map_jwt_secret() {
let auth = make_auth_config();
let garrison = map_auth_config_to_garrison(&auth);
assert_eq!(
garrison.jwt_secret.as_str(),
"test-jwt-secret-at-least-32-chars!!"
);
}
#[test]
fn test_map_token_style_is_jwt() {
let auth = make_auth_config();
let garrison = map_auth_config_to_garrison(&auth);
assert_eq!(garrison.token_style, "jwt");
}
#[test]
fn test_map_throw_on_notlogin_matches_enabled() {
let mut auth = make_auth_config();
auth.enabled = true;
let garrison = map_auth_config_to_garrison(&auth);
assert!(garrison.throw_on_not_login);
auth.enabled = false;
let garrison = map_auth_config_to_garrison(&auth);
assert!(!garrison.throw_on_not_login);
}
#[test]
fn test_map_frontend_separation_enabled() {
let auth = make_auth_config();
let garrison = map_auth_config_to_garrison(&auth);
assert!(garrison.frontend_separation);
assert!(!garrison.is_read_cookie);
assert!(garrison.is_read_header);
}
}