#![doc(html_root_url = "https://docs.rs/garrison/latest")]
#![allow(rustdoc::invalid_rust_codeblocks)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
pub mod constants;
pub mod core;
pub mod stp;
pub mod annotation;
pub mod router;
pub mod dao;
pub mod limiteron;
pub mod strategy;
pub mod session;
#[cfg(feature = "data-erasure")]
pub mod compliance;
#[cfg(feature = "three-tier-cache")]
pub mod cache;
pub mod state;
pub mod config;
pub mod health;
pub mod cli;
pub mod context;
pub mod json;
pub mod exception;
pub mod manager;
pub mod plugin;
#[cfg(any(
feature = "metrics-prometheus",
feature = "otlp",
feature = "tracing-log"
))]
pub mod observability;
#[cfg(feature = "grpc")]
pub mod grpc;
pub mod i18n;
#[cfg(feature = "garrison-testing")]
pub mod testing;
#[cfg(feature = "web-actix")]
pub mod web_actix;
#[cfg(feature = "web-warp")]
pub mod web_warp;
#[cfg(any(
feature = "web-axum",
feature = "firewall-waf",
feature = "web-cors",
feature = "web-csrf",
feature = "web-security-headers",
feature = "firewall-waf"
))]
pub mod web;
#[cfg(feature = "listener")]
pub mod listener;
#[cfg(feature = "credit-metering")]
pub mod credit;
#[cfg(any(
feature = "secure-totp",
feature = "secure-sign",
feature = "protocol-httpbasic",
feature = "protocol-httpdigest",
feature = "secure-confusable",
feature = "secure-masking",
feature = "secure-xss",
feature = "secure-sanitize",
feature = "secure-ct-eq",
feature = "sms-rate-limit",
feature = "email-verification",
feature = "field-encryption",
))]
pub mod secure;
pub mod account;
pub mod protocol;
#[cfg(any(feature = "backend-embedded", feature = "backend-remote"))]
pub mod backend;
pub mod abac;
#[cfg(feature = "auth-server")]
pub mod server;
#[cfg(feature = "oauth2-server")]
pub mod oauth2_server;
pub mod prelude;
pub mod error;
pub use prelude::*;
pub use context::tenant::{
HeaderTenantResolver, SubdomainTenantResolver, TenantContext, TenantResolver, TenantSource,
TENANT,
};
#[cfg(feature = "protocol-jwt")]
pub use context::tenant::ClaimTenantResolver;
pub use context::GarrisonPrincipal;
pub use state::TokenState;
pub use state::UserStatus;
pub use dao::repository::role_hierarchy::RoleHierarchyRecord;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::role_hierarchy::RoleHierarchyService;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::sqlite::DbnexusUserRepository;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::sqlite::DbnexusRoleRepository;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::sqlite::DbnexusPermissionRepository;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::sqlite::DbnexusUserRoleRepository;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::sqlite::DbnexusRolePermissionRepository;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::sqlite::DbnexusAuthMethodRepository;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::sqlite::DbnexusSessionRepository;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::sqlite::DbnexusLoginLogRepository;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub use dao::repository::sqlite::DbnexusUserExtRepository;
#[cfg(all(
feature = "listener",
any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql")
))]
pub use dao::GarrisonEventTx;
#[cfg(feature = "protocol-jwt")]
pub use protocol::jwt::refresh::RefreshTokenRecord;
#[cfg(feature = "protocol-jwt")]
pub use protocol::jwt::refresh::RefreshTokenReuseSubtype;
#[cfg(all(feature = "protocol-jwt", feature = "db-sqlite"))]
pub use protocol::jwt::refresh::RefreshTokenRotation;
#[cfg(feature = "audit-log")]
pub use listener::audit::AuditConfig;
#[cfg(all(feature = "audit-log", feature = "db-sqlite"))]
pub use listener::audit::AuditEntry;
#[cfg(all(feature = "audit-log", feature = "db-sqlite"))]
pub use listener::audit::AuditQuery;
#[cfg(all(feature = "audit-log", feature = "db-sqlite"))]
pub use listener::audit::AuditLogListener;
#[cfg(feature = "audit-log")]
pub use listener::audit_chain::{AuditChainEntry, AuditEventChain};
pub use protocol::social::SocialLoginProvider;
pub use protocol::social::SocialUserInfo;
pub use protocol::social::provider_names;
pub use protocol::social::registry::SocialLoginService;
#[cfg(feature = "social-wechat")]
pub use protocol::social::wechat::WechatProvider;
#[cfg(feature = "social-wechat")]
pub use protocol::social::wechat::WechatMiniAppProvider;
#[cfg(feature = "social-alipay")]
pub use protocol::social::alipay::AlipayProvider;
#[cfg(all(
any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"),
any(feature = "social-wechat", feature = "social-alipay")
))]
pub use protocol::social::SocialBindingService;
#[cfg(feature = "keycloak-oidc")]
pub use protocol::oauth2::keycloak::KeycloakConfig;
#[cfg(feature = "keycloak-oidc")]
pub use protocol::oauth2::keycloak::KeycloakProvider;
#[cfg(feature = "keycloak-oidc")]
pub use protocol::oauth2::keycloak::KeycloakClaims;
#[cfg(feature = "keycloak-oidc")]
pub use protocol::oauth2::keycloak::KeycloakTokenSet;
#[cfg(feature = "keycloak-oidc")]
pub use protocol::oauth2::keycloak::RealmAccess;
#[cfg(feature = "firewall")]
pub use strategy::firewall::GarrisonFirewallStrategy;
#[cfg(feature = "firewall")]
pub use strategy::firewall::FirewallContext;
#[cfg(feature = "firewall")]
pub use strategy::firewall::StrategyRegistration;
#[cfg(feature = "firewall-bruteforce")]
pub use strategy::firewall::{BruteForceConfig, BruteForceStrategy};
#[cfg(feature = "firewall-ratelimit")]
pub use strategy::firewall::{RateLimitConfig, RateLimitScope, RateLimitStrategy};
#[cfg(feature = "firewall-anomalous")]
pub use strategy::firewall::{AnomalousConfig, AnomalousLoginStrategy};
#[cfg(feature = "firewall-ddos")]
pub use strategy::firewall::{DDoSConfig, DDoSStrategy};
#[cfg(feature = "firewall-geoip")]
pub use strategy::firewall::{GeoIPConfig, GeoIPStrategy};
#[cfg(any(feature = "firewall-anomalous", feature = "firewall-geoip"))]
pub use strategy::firewall::{CountryLookup, GeoCoord, GeoLookup};
#[cfg(feature = "annotation-macros")]
pub use garrison_macros::{
check_abac, check_access_token, check_api_key, check_client_token, check_disable, check_login,
check_login_forge, check_mfa, check_permission, check_permission_forge, check_role,
check_role_forge, check_temp_token,
};
#[cfg(test)]
mod msrv_guard {
fn toml_str<'a>(value: &'a toml::Value, path: &[&str]) -> Option<&'a str> {
let mut cur = value;
for key in path {
cur = cur.get(*key)?;
}
cur.as_str()
}
fn cargo_toml() -> toml::Value {
toml::from_str(include_str!("../Cargo.toml")).expect("根 Cargo.toml 应为合法 TOML")
}
fn declared_msrv() -> String {
toml_str(&cargo_toml(), &["package", "rust-version"])
.expect("根 Cargo.toml 的 [package] 应声明 rust-version")
.to_string()
}
fn extract_values<'a>(text: &'a str, prefix: &str, delimiter: &str) -> Vec<&'a str> {
let mut out = Vec::new();
let mut rest = text;
while let Some(idx) = rest.find(prefix) {
let tail = &rest[idx + prefix.len()..];
let Some(end) = tail.find(delimiter) else {
break;
};
out.push(&tail[..end]);
rest = &tail[end..];
}
out
}
#[test]
fn msrv_single_source_of_truth_manifest_and_clippy() {
let msrv = declared_msrv();
assert_eq!(
toml_str(&cargo_toml(), &["workspace", "package", "rust-version"]),
Some(msrv.as_str()),
"[workspace.package].rust-version 必须与 [package].rust-version 一致(macros/examples 经此继承)"
);
let clippy: toml::Value =
toml::from_str(include_str!("../clippy.toml")).expect("clippy.toml 应为合法 TOML");
assert_eq!(
toml_str(&clippy, &["msrv"]),
Some(msrv.as_str()),
"clippy.toml 的 msrv 必须与 Cargo.toml 的 rust-version 一致,否则 lint 按错误 MSRV 判定 API 可用性"
);
}
#[test]
fn msrv_single_source_of_truth_ci_and_badge() {
let msrv = declared_msrv();
let ci = include_str!("../.github/workflows/ci.yml");
let matrix = format!("rust: [{msrv}, stable, beta]");
assert!(
ci.contains(&matrix),
"ci.yml 测试矩阵的 MSRV 腿未钉在声明值(期望 `{matrix}`):该腿以声明值编译锁定依赖图,声明与依赖面不一致时先在此暴露"
);
let gates = extract_values(ci, "matrix.rust == '", "'");
assert!(
!gates.is_empty(),
"ci.yml 未找到 MSRV 腿门控步骤(`matrix.rust == '<版本>'`)"
);
let stale: Vec<&str> = gates.iter().copied().filter(|v| *v != msrv).collect();
assert!(
stale.is_empty(),
"ci.yml 的 MSRV 腿门控步骤仍钉在旧值 {stale:?}(声明值为 {msrv})"
);
for (name, text) in [
("README.md", include_str!("../README.md")),
("README_EN.md", include_str!("../README_EN.md")),
] {
let badges = extract_values(text, "badge/rust-", "%2B");
assert_eq!(
badges,
vec![msrv.as_str()],
"{name} 的 Rust 徽章必须唯一且等于 Cargo.toml 的 rust-version(实际 {badges:?},声明值 {msrv})"
);
}
}
}