Skip to main content

AuthConfig

Struct AuthConfig 

Source
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: Duration

Default token lifetime

§refresh_token_lifetime: Duration

Refresh token lifetime

§enable_multi_factor: bool

Whether multi-factor authentication is enabled

§issuer: String

JWT issuer for token validation

§audience: String

JWT audience for token validation

§secret: Option<String>

JWT secret key (optional - can be set via environment)

§storage: StorageConfig

Storage configuration

§rate_limiting: RateLimitConfig

Rate limiting configuration

§security: SecurityConfig

Security configuration

§cors: CorsConfig

CORS configuration used by all web framework integrations.

§audit: AuditConfig

Audit logging configuration

§enable_caching: bool

Whether framework-level caching helpers are enabled.

§max_failed_attempts: u32

Maximum failed authentication attempts before a client should be blocked.

§enable_rbac: bool

Whether RBAC helpers are enabled in the configuration model.

§enable_middleware: bool

Whether framework middleware helpers are enabled in the configuration model.

§method_configs: HashMap<String, Value>

Custom settings for different auth methods

§force_production_mode: bool

Force production validation regardless of environment variables. Used in tests that explicitly verify production-mode error handling.

Implementations§

Source§

impl AuthConfig

Source

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.

Source

pub fn from_env() -> Self

Build a configuration from common environment variables.

Reads the following environment variables (all optional):

VariableMaps to
JWT_SECRETsecret / security.secret_key
DATABASE_URLPostgreSQL storage (requires postgres-storage feature)
REDIS_URLRedis storage (requires redis-storage feature)
AUTH_ISSUERissuer
AUTH_AUDIENCEaudience

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();
Source

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.).

Source

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);
Source

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);
Source

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);
Source

pub fn issuer(self, issuer: impl Into<String>) -> Self

Set the JWT issuer.

Source

pub fn audience(self, audience: impl Into<String>) -> Self

Set the JWT audience.

Source

pub fn secret(self, secret: impl Into<String>) -> Self

Set the JWT secret key.

Source

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);
Source

pub fn enable_caching(self, enabled: bool) -> Self

Enable caching.

Source

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);
Source

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);
Source

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);
Source

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);
Source

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();
Source

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);
Source

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());
Source

pub fn security(self, config: SecurityConfig) -> Self

Set security configuration.

§Example
use auth_framework::config::{AuthConfig, SecurityConfig};

let config = AuthConfig::new().security(SecurityConfig::secure());
Source

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"]));
Source

pub fn audit(self, config: AuditConfig) -> Self

Set audit configuration.

§Example
use auth_framework::config::{AuthConfig, AuditConfig};

let config = AuthConfig::new().audit(AuditConfig::default());
Source

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();
Source

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 yet
Source

pub fn validate(&self) -> Result<()>

Validate the configuration.

§Example
use auth_framework::config::AuthConfig;

let config = AuthConfig::new();
assert!(config.validate().is_ok());

Trait Implementations§

Source§

impl Clone for AuthConfig

Source§

fn clone(&self) -> AuthConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AuthConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AuthConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for AuthConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for AuthConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for AuthConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,