pub struct AuthConfig {Show 17 fields
pub token_lifetime: Duration,
pub refresh_token_lifetime: Duration,
pub enable_multi_factor: bool,
pub issuer: String,
pub audience: String,
pub secret: Option<String>,
pub storage: StorageConfig,
pub rate_limiting: RateLimitConfig,
pub security: SecurityConfig,
pub cors: CorsConfig,
pub audit: AuditConfig,
pub enable_caching: bool,
pub max_failed_attempts: u32,
pub enable_rbac: bool,
pub enable_middleware: bool,
pub method_configs: HashMap<String, Value>,
pub force_production_mode: bool,
}Expand description
Main configuration for the authentication framework.
Fields§
§token_lifetime: DurationDefault token lifetime
refresh_token_lifetime: DurationRefresh token lifetime
enable_multi_factor: boolWhether multi-factor authentication is enabled
issuer: StringJWT issuer for token validation
audience: StringJWT audience for token validation
secret: Option<String>JWT secret key (optional - can be set via environment)
storage: StorageConfigStorage configuration
rate_limiting: RateLimitConfigRate limiting configuration
security: SecurityConfigSecurity configuration
cors: CorsConfigCORS configuration used by all web framework integrations.
audit: AuditConfigAudit logging configuration
enable_caching: boolWhether framework-level caching helpers are enabled.
max_failed_attempts: u32Maximum failed authentication attempts before a client should be blocked.
enable_rbac: boolWhether RBAC helpers are enabled in the configuration model.
enable_middleware: boolWhether framework middleware helpers are enabled in the configuration model.
method_configs: HashMap<String, Value>Custom settings for different auth methods
force_production_mode: boolForce production validation regardless of environment variables. Used in tests that explicitly verify production-mode error handling.
Implementations§
Source§impl AuthConfig
impl AuthConfig
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new configuration with default values.
AuthConfig supports two construction styles:
§Fluent setter chain (simple cases)
use auth_framework::config::AuthConfig;
use std::time::Duration;
let config = AuthConfig::new()
.token_lifetime(Duration::from_secs(3600))
.secret("my-secret-key-at-least-32-chars-long!!");§Full builder (complex / multi-backend setups)
use auth_framework::prelude::*;
let auth = AuthFramework::builder()
.with_jwt().secret("...").issuer("myapp").done()
.with_storage().memory().done()
.security_preset(SecurityPreset::HighSecurity)
.build().await?;See [AuthFramework::builder] and [AuthFramework::quick_start] for
the full builder APIs.
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Build a configuration from common environment variables.
Reads the following environment variables (all optional):
| Variable | Maps to |
|---|---|
JWT_SECRET | secret / security.secret_key |
DATABASE_URL | PostgreSQL storage (requires postgres-storage feature) |
REDIS_URL | Redis storage (requires redis-storage feature) |
AUTH_ISSUER | issuer |
AUTH_AUDIENCE | audience |
Missing variables are silently ignored and fall back to defaults.
§Example
use auth_framework::config::AuthConfig;
// In tests or CI you can set the env vars beforehand:
// std::env::set_var("JWT_SECRET", "my-long-secret-key-for-jwt-signing!!");
let config = AuthConfig::from_env();Sourcepub fn builder() -> AuthBuilder
pub fn builder() -> AuthBuilder
Start the full AuthBuilder workflow.
This is a convenience alias for [AuthFramework::builder()] — use it
when you want to configure storage, security presets, and sub-builders
from a single fluent chain.
§Example
use auth_framework::prelude::*;
let auth = AuthConfig::builder()
.with_jwt().secret("...").done()
.with_storage().memory().done()
.build().await?;For more organized configuration, consider AuthConfigBuilder which
groups settings by concern (tokens, security, storage, features, etc.).
Sourcepub fn token_lifetime(self, lifetime: Duration) -> Self
pub fn token_lifetime(self, lifetime: Duration) -> Self
Set the token lifetime.
§Example
use auth_framework::config::AuthConfig;
use std::time::Duration;
let config = AuthConfig::new().token_lifetime(Duration::from_secs(1800));
assert_eq!(config.token_lifetime.as_secs(), 1800);Sourcepub fn refresh_token_lifetime(self, lifetime: Duration) -> Self
pub fn refresh_token_lifetime(self, lifetime: Duration) -> Self
Set the refresh token lifetime.
§Example
use auth_framework::config::AuthConfig;
use std::time::Duration;
let config = AuthConfig::new().refresh_token_lifetime(Duration::from_secs(86400));
assert_eq!(config.refresh_token_lifetime.as_secs(), 86400);Sourcepub fn enable_multi_factor(self, enabled: bool) -> Self
pub fn enable_multi_factor(self, enabled: bool) -> Self
Enable or disable multi-factor authentication.
§Example
use auth_framework::config::AuthConfig;
let config = AuthConfig::new().enable_multi_factor(true);
assert!(config.enable_multi_factor);Sourcepub fn require_mfa(self, required: bool) -> Self
pub fn require_mfa(self, required: bool) -> Self
Require MFA for all users.
§Example
use auth_framework::config::AuthConfig;
let config = AuthConfig::new().require_mfa(true);
assert!(config.enable_multi_factor);Sourcepub fn enable_caching(self, enabled: bool) -> Self
pub fn enable_caching(self, enabled: bool) -> Self
Enable caching.
Sourcepub fn max_failed_attempts(self, max: u32) -> Self
pub fn max_failed_attempts(self, max: u32) -> Self
Set maximum failed attempts.
§Example
use auth_framework::config::AuthConfig;
let config = AuthConfig::new().max_failed_attempts(10);
assert_eq!(config.max_failed_attempts, 10);Sourcepub fn enable_rbac(self, enabled: bool) -> Self
pub fn enable_rbac(self, enabled: bool) -> Self
Enable RBAC.
§Example
use auth_framework::config::AuthConfig;
let config = AuthConfig::new().enable_rbac(true);
assert!(config.enable_rbac);Sourcepub fn enable_security_audit(self, enabled: bool) -> Self
pub fn enable_security_audit(self, enabled: bool) -> Self
Enable security audit.
§Example
use auth_framework::config::AuthConfig;
let config = AuthConfig::new().enable_security_audit(true);
assert!(config.audit.enabled);Sourcepub fn enable_middleware(self, enabled: bool) -> Self
pub fn enable_middleware(self, enabled: bool) -> Self
Enable middleware.
§Example
use auth_framework::config::AuthConfig;
let config = AuthConfig::new().enable_middleware(true);
assert!(config.enable_middleware);Sourcepub fn force_production_mode(self) -> Self
pub fn force_production_mode(self) -> Self
Force production-mode validation, bypassing test-environment detection.
Used exclusively in tests that verify production-specific error handling without
polluting the process-wide environment with ENVIRONMENT=production.
§Example
use auth_framework::config::AuthConfig;
let config = AuthConfig::new().force_production_mode();Sourcepub fn storage(self, storage: StorageConfig) -> Self
pub fn storage(self, storage: StorageConfig) -> Self
Set the storage configuration.
§Example
use auth_framework::config::{AuthConfig, StorageConfig};
let config = AuthConfig::new().storage(StorageConfig::Memory);Sourcepub fn rate_limiting(self, config: RateLimitConfig) -> Self
pub fn rate_limiting(self, config: RateLimitConfig) -> Self
Set rate limiting configuration.
§Example
use auth_framework::config::{AuthConfig, RateLimitConfig};
let config = AuthConfig::new().rate_limiting(RateLimitConfig::default());Sourcepub fn security(self, config: SecurityConfig) -> Self
pub fn security(self, config: SecurityConfig) -> Self
Set security configuration.
§Example
use auth_framework::config::{AuthConfig, SecurityConfig};
let config = AuthConfig::new().security(SecurityConfig::secure());Sourcepub fn cors(self, config: CorsConfig) -> Self
pub fn cors(self, config: CorsConfig) -> Self
Set CORS configuration.
§Example
use auth_framework::config::{AuthConfig, CorsConfig};
let config = AuthConfig::new()
.cors(CorsConfig::for_origins(["https://app.example.com"]));Sourcepub fn audit(self, config: AuditConfig) -> Self
pub fn audit(self, config: AuditConfig) -> Self
Set audit configuration.
§Example
use auth_framework::config::{AuthConfig, AuditConfig};
let config = AuthConfig::new().audit(AuditConfig::default());Sourcepub fn method_config(
self,
method_name: impl Into<String>,
config: impl Serialize,
) -> Result<Self>
pub fn method_config( self, method_name: impl Into<String>, config: impl Serialize, ) -> Result<Self>
Add configuration for a specific auth method.
§Example
use auth_framework::config::AuthConfig;
let config = AuthConfig::new()
.method_config("oauth2", serde_json::json!({
"client_id": "my-client",
"client_secret": "my-secret"
}))
.unwrap();Sourcepub fn get_method_config<T>(&self, method_name: &str) -> Result<Option<T>>where
T: for<'de> Deserialize<'de>,
pub fn get_method_config<T>(&self, method_name: &str) -> Result<Option<T>>where
T: for<'de> Deserialize<'de>,
Get configuration for a specific auth method.
§Example
use auth_framework::config::AuthConfig;
let config = AuthConfig::new();
let oauth: Option<serde_json::Value> = config.get_method_config("oauth2").unwrap();
assert!(oauth.is_none()); // no oauth2 config set yetTrait Implementations§
Source§impl Clone for AuthConfig
impl Clone for AuthConfig
Source§fn clone(&self) -> AuthConfig
fn clone(&self) -> AuthConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AuthConfig
impl Debug for AuthConfig
Source§impl Default for AuthConfig
impl Default for AuthConfig
Source§impl<'de> Deserialize<'de> for AuthConfig
impl<'de> Deserialize<'de> for AuthConfig
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for AuthConfig
impl Display for AuthConfig
Auto Trait Implementations§
impl Freeze for AuthConfig
impl RefUnwindSafe for AuthConfig
impl Send for AuthConfig
impl Sync for AuthConfig
impl Unpin for AuthConfig
impl UnsafeUnpin for AuthConfig
impl UnwindSafe for AuthConfig
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.