1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*!
# Auth Framework
A comprehensive authentication and authorization framework for Rust applications.
This crate provides a unified interface for various authentication methods,
token management, permission checking, and secure credential handling with
a focus on distributed systems.
## API Orientation
- Use [`AuthFramework`] as the default entry point for most applications.
- Use [`ModularAuthFramework`] only when you explicitly want manager-level
composition and lifecycle control.
- Use [`prelude`] when you want ergonomic imports for application code.
- Use [`AppConfigBuilder`] for simple application-owned configuration values.
- Use [`LayeredConfigBuilder`] and [`ConfigManager`] when you need layered
configuration from files and environment variables.
## Features
- Multiple authentication methods (OAuth, API keys, JWT, etc.)
- Token issuance, validation, and refresh with RSA and HMAC signing
- RSA key format support: PKCS#1 and PKCS#8 formats auto-detected
- Role-based access control integration
- Permission checking and enforcement
- Secure credential storage
- Authentication middleware for web frameworks
- Distributed authentication with cross-node validation
- Single sign-on capabilities
- Multi-factor authentication support
- Audit logging of authentication events
- Rate limiting and brute force protection
- Session management
- Password hashing and validation
- Customizable authentication flows
## Quick Start
```rust,no_run
use auth_framework::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Build configuration. JWT secret must be at least 32 characters.
let config = AuthConfig::new()
.token_lifetime(std::time::Duration::from_secs(3600))
.secret(std::env::var("JWT_SECRET")
.unwrap_or_else(|_| "replace-with-a-32-char-random-secret!!".to_string()));
let mut auth = AuthFramework::new(config);
auth.initialize().await?;
// Register a user.
let user_id = auth.users().register("alice", "alice@example.com", "s3cr3t!").await?;
// Issue a token via the grouped token accessor.
let token = auth.tokens().create(&user_id, &["read"], "jwt", None).await?;
// Validate and authorize.
if auth.tokens().validate(&token).await? {
if auth.authorization().check(&token, "read", "documents").await? {
println!("Alice may read documents.");
}
}
Ok(())
}
```
See [`prelude`] for the full set of re-exported types, and the accessor groups
[`AuthFramework::users`], [`AuthFramework::sessions`], [`AuthFramework::tokens`],
[`AuthFramework::authorization`], [`AuthFramework::mfa`], [`AuthFramework::monitoring`],
[`AuthFramework::audit`], and [`AuthFramework::admin`] for organized entry points
into each capability area.
## Security Considerations
- Always use HTTPS in production
- Use strong, unique secrets for token signing
- Enable rate limiting to prevent brute force attacks
- Regularly rotate secrets and keys
- Monitor authentication events for suspicious activity
- Follow the principle of least privilege for permissions
See the [Security Policy](https://github.com/ciresnave/auth-framework/blob/main/SECURITY.md)
for comprehensive security guidelines.
*/
// REST API Server
// Admin interface (conditional on admin-binary feature)
// ────────────────────────────────────────────────────────────────────────────
// Core framework modules
// ────────────────────────────────────────────────────────────────────────────
/// Primary authentication framework — start here.
///
/// Contains [`AuthFramework`], the main entry point for most applications.
/// Access grouped operations via [`AuthFramework::users`], [`AuthFramework::tokens`],
/// [`AuthFramework::sessions`], etc.
/// Advanced component-oriented framework.
///
/// Use [`ModularAuthFramework`](auth_modular::AuthFramework) only when you need
/// direct access to individual manager instances (user, session, MFA) for custom
/// composition. Most applications should use [`auth::AuthFramework`] instead.
/// Grouped operation facades over [`AuthFramework`].
///
/// Light reference wrappers (e.g. [`UserOperations`], [`TokenOperations`]) returned
/// by the accessor methods on `AuthFramework`. Not usually imported directly —
/// use `auth.users()`, `auth.tokens()`, etc.
/// Supporting authentication data types.
///
/// Credentials, metadata, and MFA primitives used as inputs to the core
/// framework. Import specific types rather than the module wildcard.
/// Domain-specific newtypes (`Roles`, `Scopes`, `Permissions`, etc.).
/// Error types and the crate-wide [`Result`](errors::Result) alias.
/// Authentication method implementations (JWT, OAuth2, API keys, passwords, SAML).
/// Permission and role definitions for access control.
/// Token creation, validation, rotation, and JWKS support.
// ────────────────────────────────────────────────────────────────────────────
// Configuration
// ────────────────────────────────────────────────────────────────────────────
/// Configuration types and management.
///
/// - [`AuthConfig`](config::AuthConfig) — main config struct (use [`AuthConfig::new()`]
/// for fluent setters or [`AuthConfig::builder()`] for the full builder).
/// - [`ConfigManager`](config::ConfigManager) — layered config from files + env.
/// - [`AppConfig`](config::AppConfig) — simple app-owned config values.
// ────────────────────────────────────────────────────────────────────────────
// Storage & persistence
// ────────────────────────────────────────────────────────────────────────────
/// Storage backends and the [`AuthStorage`](storage::AuthStorage) trait.
///
/// See the trait documentation for available backends (Memory, PostgreSQL,
/// MySQL, Redis, SQLite, Encrypted) and guidance on writing custom backends.
// ────────────────────────────────────────────────────────────────────────────
// Security
// ────────────────────────────────────────────────────────────────────────────
/// Audit logging of authentication and authorization events.
/// Role-based and attribute-based access control (RBAC/ABAC).
/// Security utilities: rate limiting, DoS protection, IP blocking, and JWT hardening.
// ────────────────────────────────────────────────────────────────────────────
// Session & distributed state
// ────────────────────────────────────────────────────────────────────────────
/// Session lifecycle, device fingerprinting, and risk scoring.
/// Distributed authentication: cross-node token validation and cluster coordination.
// ────────────────────────────────────────────────────────────────────────────
// Server-side protocol implementations
// ────────────────────────────────────────────────────────────────────────────
/// Server-side OAuth 2.0 / OIDC / FAPI protocol implementations.
// oauth2_server and oauth2_enhanced_storage now live under server::oauth.
pub use oauth2_enhanced_storage;
pub use oauth2_server;
/// OAuth 2.0 client type definitions (RFC 6749 §2.1).
// ────────────────────────────────────────────────────────────────────────────
// Integrations, providers & transport
// ────────────────────────────────────────────────────────────────────────────
/// OAuth 2.0 provider configuration and PKCE helpers.
/// Helpers for extracting user profiles from tokens and provider responses.
/// Multi-tenant support for native multi-tenant deployments.
/// User context and session enrichment.
// ────────────────────────────────────────────────────────────────────────────
// Monitoring, analytics & operations
// ────────────────────────────────────────────────────────────────────────────
/// Monitoring, health checks, and performance metrics.
/// Analytics collection and reporting.
/// Deployment, scaling, and infrastructure management.
/// Threat intelligence feeds and IP reputation services.
// ────────────────────────────────────────────────────────────────────────────
// Migration & maintenance
// ────────────────────────────────────────────────────────────────────────────
/// Schema migration utilities for role-system v1.0 integration.
/// SQL migration scripts for database backends.
/// Backup, restore, and reset utilities.
// ────────────────────────────────────────────────────────────────────────────
// Developer tools
// ────────────────────────────────────────────────────────────────────────────
/// Ergonomic builders and prelude for better developer experience.
/// Convenience re-exports for common types — `use auth_framework::prelude::*`.
/// Internal utility functions.
/// Test helpers and mock implementations for downstream testing.
/// Protocol-level types shared across OAuth, OIDC, and SAML flows.
/// Command-line interface utilities.
// ────────────────────────────────────────────────────────────────────────────
// Feature-gated optional modules
// ────────────────────────────────────────────────────────────────────────────
// SDK generation for multiple languages
// ────────────────────────────────────────────────────────────────────────────
// Web framework integrations
// ────────────────────────────────────────────────────────────────────────────
/// Ready-made middleware and extractors for popular web frameworks.
///
/// Enable the appropriate feature flag to pull in the integration you need:
///
/// | Feature | Module |
/// |---------|--------|
/// | `axum-integration` | [`integrations::axum`] |
/// | `actix-integration` | [`integrations::actix_web`] |
/// | `warp-integration` | [`integrations::warp`] |
// ────────────────────────────────────────────────────────────────────────────
// Re-exports — public API surface
// ────────────────────────────────────────────────────────────────────────────
// Re-exports - Main modular auth framework components
pub use crate;
/// Deprecated alias — use [`UserInfo`] directly.
pub type CoreUserInfo = UserInfo;
pub use crateAuthFramework as ModularAuthFramework;
pub use crate;
pub use Credential;
pub use ConfigBuilder as AppConfigBuilder;
pub use ;
pub use ;
pub use ;
pub use ;
// REST API Server exports
pub use ;
// SAML support (feature-gated)
pub use saml;
// PKCE support functions
pub use generate_pkce;
pub use ;
pub use ;
pub use ;
pub use AuthToken;
// WS-Security 1.1 and WS-Trust — enterprise XML security protocols.
// Hidden from root docs; access via `auth_framework::protocols::ws_security` / `ws_trust`.
pub use ;
pub use RequestSecurityToken;
// Server-side OIDC types.
//
// Note: the OIDC spec defines its own `UserInfo` struct (the /userinfo endpoint
// response). It is re-exported here as `OidcUserInfo` to avoid collision with
// the framework-level [`UserInfo`] (the internal user record).
pub use ;
// Phase 2: Logout & Security Ecosystem specifications (advanced OIDC logout protocols).
// Hidden from root docs; access via `auth_framework::server::oidc::oidc_backchannel_logout`
// and `auth_framework::server::oidc::oidc_frontchannel_logout`.
pub use ;
pub use ;
pub use ;
// OAuth2 server types and configurations
pub use ;
// Server configuration types — ClientType and ClientConfig come from the canonical `client` module.
pub use ;
pub use ;
/// Deprecated alias for [`ClientRegistrationRequest`].
pub type ServerClientRegistrationRequest =
ClientRegistrationRequest;
// Advanced server modules and RFC implementations.
// Hidden from top-level docs/autocomplete to avoid cluttering the onboarding path;
// access via `auth_framework::server::*` for advanced use.
pub use DpopManager;
pub use MetadataProvider;
pub use OAuth2Server as ServerOAuth2Server;
pub use PARManager;
pub use PrivateKeyJwtManager;
pub use TokenIntrospectionService;
// Security and authentication module re-exports
pub use ;
/// Deprecated alias for `authentication::mfa::MfaManager`. Use `auth_modular` MFA operations instead.
pub use MfaManager as LegacyMfaManager;
pub use ;
pub use ;
pub use ;
pub use SecureMfaService;
pub use ;
pub use ;
/// Deprecated alias for [`SessionManager`]. Use `SessionManager` directly.
pub use SessionManager as LegacySessionManager;
pub use ;
pub use RateLimiter;
// Multi-tenant support
pub use ;
// Monitoring and metrics
pub use ;
// Session coordination stats from auth module
pub use SessionCoordinationStats;
// Re-export testing utilities when available
pub use ; // Removed helpers temporarily
// Re-export test infrastructure for bulletproof testing
pub use ;