auth-framework 0.4.2

A comprehensive, production-ready authentication and authorization framework for Rust applications
Documentation
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# Performance Optimization Guide


## Introduction


This guide provides comprehensive strategies for optimizing AuthFramework performance in production environments. It covers caching strategies, connection management, database optimization, and monitoring techniques to ensure your authentication service delivers optimal performance.

## Table of Contents


1. [Performance Fundamentals]#performance-fundamentals
2. [Caching Strategies]#caching-strategies
3. [Database Optimization]#database-optimization
4. [Connection Management]#connection-management
5. [Token Optimization]#token-optimization
6. [Memory Management]#memory-management
7. [Network Optimization]#network-optimization
8. [Monitoring and Profiling]#monitoring-and-profiling
9. [Load Testing]#load-testing
10. [Production Tuning]#production-tuning

## Performance Fundamentals


### Key Performance Metrics


AuthFramework tracks several critical performance metrics:

- **Authentication Latency**: Time to authenticate user credentials
- **Token Validation Latency**: Time to validate JWT tokens
- **Throughput**: Requests per second the service can handle
- **Memory Usage**: RAM consumption patterns
- **CPU Utilization**: Processing overhead
- **Database Query Performance**: SQL query execution times

### Performance Targets


**Production Performance Targets**:

```yaml
authentication:
  p50_latency: "<50ms"
  p95_latency: "<200ms"
  p99_latency: "<500ms"

token_validation:
  p50_latency: "<10ms"
  p95_latency: "<25ms"
  p99_latency: "<100ms"

throughput:
  authentication: ">1000 req/s"
  token_validation: ">5000 req/s"

resources:
  memory_usage: "<2GB per instance"
  cpu_utilization: "<70% average"
```

### Benchmarking Setup


```rust
use auth_framework::{AuthClient, PerformanceConfig};
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn benchmark_authentication(c: &mut Criterion) {
    let rt = tokio::runtime::Runtime::new().unwrap();
    let auth_client = rt.block_on(async {
        AuthClient::builder()
            .base_url("http://localhost:8080")
            .performance_config(PerformanceConfig {
                connection_pool_size: 50,
                request_timeout: Duration::from_secs(30),
                keep_alive_timeout: Duration::from_secs(90),
            })
            .build()
    });

    let credentials = LoginRequest {
        username: "test@example.com".to_string(),
        password: "test_password".to_string(),
        remember_me: false,
    };

    c.bench_function("authenticate_user", |b| {
        b.to_async(&rt).iter(|| async {
            black_box(auth_client.authenticate(black_box(&credentials)).await)
        })
    });
}

criterion_group!(benches, benchmark_authentication);
criterion_main!(benches);
```

## Caching Strategies


### 1. Multi-Layer Caching Architecture


```rust
use auth_framework::{CacheLayer, CacheConfig};

pub struct MultiLayerCache {
    l1_cache: MemoryCache,      // In-memory cache
    l2_cache: RedisCache,       // Distributed cache
    l3_cache: DatabaseCache,    // Database query cache
}

impl MultiLayerCache {
    pub async fn get_user_permissions(&self, user_id: &str) -> Result<UserPermissions, CacheError> {
        let cache_key = format!("permissions:{}", user_id);

        // L1: Check in-memory cache first
        if let Some(permissions) = self.l1_cache.get(&cache_key).await? {
            return Ok(permissions);
        }

        // L2: Check Redis cache
        if let Some(permissions) = self.l2_cache.get(&cache_key).await? {
            // Store in L1 cache for faster future access
            self.l1_cache.set(&cache_key, &permissions, Duration::from_secs(300)).await?;
            return Ok(permissions);
        }

        // L3: Fetch from database and cache at all levels
        let permissions = self.fetch_from_database(user_id).await?;

        // Cache at all levels with appropriate TTLs
        self.l3_cache.set(&cache_key, &permissions, Duration::from_secs(3600)).await?;
        self.l2_cache.set(&cache_key, &permissions, Duration::from_secs(1800)).await?;
        self.l1_cache.set(&cache_key, &permissions, Duration::from_secs(300)).await?;

        Ok(permissions)
    }
}
```

### 2. Smart Token Caching


```rust
use auth_framework::{TokenCache, TokenCachePolicy};

pub struct SmartTokenCache {
    cache: TokenCache,
    policy: TokenCachePolicy,
}

impl SmartTokenCache {
    pub async fn validate_token_cached(&self, token: &str) -> Result<TokenValidationResult, AuthError> {
        // Generate cache key from token hash (never cache actual token)
        let token_hash = blake3::hash(token.as_bytes());
        let cache_key = format!("token_validation:{}", token_hash);

        // Check cache first
        if let Some(cached_result) = self.cache.get(&cache_key).await? {
            // Verify cached result is still valid
            if self.is_cache_entry_valid(&cached_result) {
                return Ok(cached_result.result);
            }
        }

        // Validate token with auth service
        let result = self.validate_token_with_service(token).await?;

        // Cache successful validations with smart TTL
        if let TokenValidationResult::Valid { expires_at, .. } = &result {
            let cache_ttl = self.policy.calculate_cache_ttl(*expires_at);

            let cache_entry = CachedTokenValidation {
                result: result.clone(),
                cached_at: Utc::now(),
                expires_at: *expires_at,
            };

            self.cache.set(&cache_key, &cache_entry, cache_ttl).await?;
        }

        Ok(result)
    }
}
```

### 3. Session Caching Optimization


```rust
use auth_framework::{SessionCache, SessionOptimizer};

pub struct OptimizedSessionManager {
    session_cache: SessionCache,
    session_optimizer: SessionOptimizer,
}

impl OptimizedSessionManager {
    pub async fn get_session_optimized(&self, session_id: &str) -> Result<Session, SessionError> {
        // Use read-through cache pattern
        self.session_cache.get_or_fetch(session_id, |id| async {
            self.fetch_session_from_store(id).await
        }).await
    }

    pub async fn create_session_optimized(&self, user_id: &str) -> Result<Session, SessionError> {
        let session = Session {
            id: generate_session_id(),
            user_id: user_id.to_string(),
            created_at: Utc::now(),
            expires_at: Utc::now() + Duration::from_hours(24),
            last_activity: Utc::now(),
        };

        // Write to cache and database concurrently
        let (cache_result, db_result) = tokio::join!(
            self.session_cache.set(&session.id, &session, Duration::from_hours(24)),
            self.store_session_in_database(&session)
        );

        cache_result?;
        db_result?;

        Ok(session)
    }
}
```

## Database Optimization


### 1. Connection Pool Optimization


```rust
use auth_framework::{DatabaseConfig, ConnectionPool};
use sqlx::PgPool;

pub async fn create_optimized_db_pool() -> Result<PgPool, DatabaseError> {
    let pool = PgPool::connect_with(
        PgConnectOptions::new()
            .host(&std::env::var("DATABASE_HOST")?)
            .port(5432)
            .database("auth_framework")
            .username("auth_user")
            .password(&std::env::var("DATABASE_PASSWORD")?)
            // Optimize connection settings
            .options([
                ("application_name", "auth_framework"),
                ("statement_timeout", "30s"),
                ("lock_timeout", "10s"),
                ("idle_in_transaction_session_timeout", "60s"),
            ])
    )
    // Pool size optimization
    .max_connections(50)                    // Based on server capacity
    .min_connections(10)                    // Always keep minimum connections
    .acquire_timeout(Duration::from_secs(30))  // Connection acquisition timeout
    .idle_timeout(Duration::from_secs(600))     // Close idle connections after 10 minutes
    .max_lifetime(Duration::from_secs(1800))    // Recreate connections every 30 minutes
    .build()
    .await?;

    Ok(pool)
}
```

### 2. Query Optimization


```rust
use auth_framework::{QueryOptimizer, PreparedStatements};

pub struct OptimizedUserRepository {
    db_pool: PgPool,
    prepared_statements: PreparedStatements,
}

impl OptimizedUserRepository {
    pub async fn get_user_with_permissions_optimized(&self, user_id: &str) -> Result<UserWithPermissions, DatabaseError> {
        // Use prepared statement for better performance
        let query = r#"
            SELECT
                u.id, u.username, u.email, u.created_at, u.last_login,
                array_agg(DISTINCT p.permission_name) as permissions,
                array_agg(DISTINCT r.role_name) as roles
            FROM users u
            LEFT JOIN user_roles ur ON u.id = ur.user_id
            LEFT JOIN roles r ON ur.role_id = r.id
            LEFT JOIN role_permissions rp ON r.id = rp.role_id
            LEFT JOIN permissions p ON rp.permission_id = p.id
            WHERE u.id = $1 AND u.active = true
            GROUP BY u.id, u.username, u.email, u.created_at, u.last_login
        "#;

        let row = sqlx::query(query)
            .bind(user_id)
            .fetch_optional(&self.db_pool)
            .await?;

        match row {
            Some(row) => Ok(UserWithPermissions {
                id: row.get("id"),
                username: row.get("username"),
                email: row.get("email"),
                permissions: row.get::<Vec<String>, _>("permissions")
                    .into_iter()
                    .filter(|p| !p.is_empty())
                    .collect(),
                roles: row.get::<Vec<String>, _>("roles")
                    .into_iter()
                    .filter(|r| !r.is_empty())
                    .collect(),
                created_at: row.get("created_at"),
                last_login: row.get("last_login"),
            }),
            None => Err(DatabaseError::UserNotFound),
        }
    }

    pub async fn batch_validate_tokens(&self, token_hashes: Vec<String>) -> Result<Vec<TokenValidationInfo>, DatabaseError> {
        // Use batch query for multiple token validations
        let query = r#"
            SELECT token_hash, user_id, expires_at, revoked
            FROM tokens
            WHERE token_hash = ANY($1) AND expires_at > NOW()
        "#;

        let rows = sqlx::query(query)
            .bind(&token_hashes)
            .fetch_all(&self.db_pool)
            .await?;

        let results = rows.into_iter()
            .map(|row| TokenValidationInfo {
                token_hash: row.get("token_hash"),
                user_id: row.get("user_id"),
                expires_at: row.get("expires_at"),
                revoked: row.get("revoked"),
            })
            .collect();

        Ok(results)
    }
}
```

### 3. Database Index Optimization


```sql
-- Essential indexes for AuthFramework performance

-- Users table indexes
CREATE INDEX CONCURRENTLY idx_users_username ON users (username) WHERE active = true;
CREATE INDEX CONCURRENTLY idx_users_email ON users (email) WHERE active = true;
CREATE INDEX CONCURRENTLY idx_users_last_login ON users (last_login DESC) WHERE active = true;

-- Tokens table indexes
CREATE INDEX CONCURRENTLY idx_tokens_hash ON tokens (token_hash);
CREATE INDEX CONCURRENTLY idx_tokens_user_expires ON tokens (user_id, expires_at DESC);
CREATE INDEX CONCURRENTLY idx_tokens_expires_cleanup ON tokens (expires_at) WHERE revoked = false;

-- Sessions table indexes
CREATE INDEX CONCURRENTLY idx_sessions_user_id ON sessions (user_id, created_at DESC);
CREATE INDEX CONCURRENTLY idx_sessions_expires ON sessions (expires_at) WHERE active = true;
CREATE INDEX CONCURRENTLY idx_sessions_last_activity ON sessions (last_activity DESC) WHERE active = true;

-- User roles and permissions indexes
CREATE INDEX CONCURRENTLY idx_user_roles_user_id ON user_roles (user_id);
CREATE INDEX CONCURRENTLY idx_user_roles_role_id ON user_roles (role_id);
CREATE INDEX CONCURRENTLY idx_role_permissions_role_id ON role_permissions (role_id);

-- Audit log indexes (for performance monitoring)
CREATE INDEX CONCURRENTLY idx_audit_log_user_timestamp ON audit_log (user_id, timestamp DESC);
CREATE INDEX CONCURRENTLY idx_audit_log_event_type_timestamp ON audit_log (event_type, timestamp DESC);
```

## Connection Management


### 1. HTTP Client Optimization


```rust
use auth_framework::{HttpClientConfig, ConnectionOptimizer};

pub fn create_optimized_http_client() -> Result<reqwest::Client, ClientError> {
    reqwest::Client::builder()
        // Connection pool optimization
        .pool_max_idle_per_host(20)
        .pool_idle_timeout(Duration::from_secs(90))
        .tcp_keepalive(Duration::from_secs(60))

        // Timeout configuration
        .timeout(Duration::from_secs(30))
        .connect_timeout(Duration::from_secs(10))

        // Performance settings
        .tcp_nodelay(true)
        .http2_prior_knowledge()
        .http2_adaptive_window(true)

        // Security settings
        .tls_sni(true)
        .min_tls_version(tls::Version::TLSv1_2)

        .build()
        .map_err(ClientError::from)
}
```

### 2. Redis Connection Optimization


```rust
use auth_framework::{RedisConfig, RedisOptimizer};

pub async fn create_optimized_redis_client() -> Result<redis::Client, RedisError> {
    let redis_url = std::env::var("REDIS_URL")?;

    let client = redis::Client::open(redis::ConnectionInfo {
        addr: redis_url.parse()?,
        redis: redis::RedisConnectionInfo {
            db: 0,
            username: None,
            password: std::env::var("REDIS_PASSWORD").ok(),
        },
    })?;

    // Configure connection pool
    let pool_config = redis::PoolConfig::builder()
        .max_size(30)
        .min_idle(5)
        .connection_timeout(Duration::from_secs(10))
        .idle_timeout(Some(Duration::from_secs(300)))
        .build();

    Ok(client)
}

pub struct RedisOperationOptimizer {
    client: redis::Client,
}

impl RedisOperationOptimizer {
    pub async fn batch_get(&self, keys: Vec<String>) -> Result<Vec<Option<String>>, RedisError> {
        let mut conn = self.client.get_async_connection().await?;

        // Use pipeline for batch operations
        let mut pipe = redis::pipe();
        for key in &keys {
            pipe.get(key);
        }

        let results: Vec<Option<String>> = pipe.query_async(&mut conn).await?;
        Ok(results)
    }

    pub async fn batch_set(&self, key_values: Vec<(String, String, Duration)>) -> Result<(), RedisError> {
        let mut conn = self.client.get_async_connection().await?;

        // Use pipeline for batch operations
        let mut pipe = redis::pipe();
        for (key, value, ttl) in key_values {
            pipe.set_ex(key, value, ttl.as_secs());
        }

        pipe.query_async(&mut conn).await?;
        Ok(())
    }
}
```

## Token Optimization


### 1. JWT Performance Optimization


```rust
use auth_framework::{JwtOptimizer, TokenConfig};

pub struct OptimizedJwtManager {
    encoding_key: EncodingKey,
    decoding_key: DecodingKey,
    validation: Validation,
}

impl OptimizedJwtManager {
    pub fn new() -> Result<Self, JwtError> {
        // Use RS256 for better security and performance in distributed systems
        let private_key = std::fs::read("private_key.pem")?;
        let public_key = std::fs::read("public_key.pem")?;

        let encoding_key = EncodingKey::from_rsa_pem(&private_key)?;
        let decoding_key = DecodingKey::from_rsa_pem(&public_key)?;

        let mut validation = Validation::new(Algorithm::RS256);
        validation.set_audience(&["auth-framework"]);
        validation.set_issuer(&["auth.yourdomain.com"]);

        Ok(Self {
            encoding_key,
            decoding_key,
            validation,
        })
    }

    pub fn create_optimized_token(&self, claims: &TokenClaims) -> Result<String, JwtError> {
        // Minimize token size by using short claim names
        let optimized_claims = OptimizedClaims {
            sub: claims.user_id.clone(),    // subject
            iat: claims.issued_at.timestamp(),   // issued at
            exp: claims.expires_at.timestamp(),  // expires at
            aud: "auth-framework".to_string(),   // audience
            iss: "auth.yourdomain.com".to_string(), // issuer
            perms: claims.permissions.clone(),   // permissions (custom)
        };

        jsonwebtoken::encode(
            &Header::new(Algorithm::RS256),
            &optimized_claims,
            &self.encoding_key,
        )
        .map_err(JwtError::from)
    }

    pub fn validate_token_fast(&self, token: &str) -> Result<TokenClaims, JwtError> {
        let token_data = jsonwebtoken::decode::<OptimizedClaims>(
            token,
            &self.decoding_key,
            &self.validation,
        )?;

        Ok(TokenClaims {
            user_id: token_data.claims.sub,
            permissions: token_data.claims.perms,
            issued_at: DateTime::from_timestamp(token_data.claims.iat, 0)
                .ok_or(JwtError::InvalidTimestamp)?,
            expires_at: DateTime::from_timestamp(token_data.claims.exp, 0)
                .ok_or(JwtError::InvalidTimestamp)?,
        })
    }
}
```

### 2. Token Compression


```rust
use auth_framework::{TokenCompressor, CompressionAlgorithm};

pub struct CompressedTokenManager {
    jwt_manager: OptimizedJwtManager,
    compressor: TokenCompressor,
}

impl CompressedTokenManager {
    pub fn create_compressed_token(&self, claims: &TokenClaims) -> Result<String, TokenError> {
        // Create standard JWT
        let jwt_token = self.jwt_manager.create_optimized_token(claims)?;

        // Compress for large payloads
        if jwt_token.len() > 1024 {
            let compressed = self.compressor.compress(&jwt_token)?;
            let encoded = base64::encode_config(compressed, base64::URL_SAFE_NO_PAD);
            Ok(format!("compressed:{}", encoded))
        } else {
            Ok(jwt_token)
        }
    }

    pub fn validate_compressed_token(&self, token: &str) -> Result<TokenClaims, TokenError> {
        if let Some(compressed_data) = token.strip_prefix("compressed:") {
            // Decompress token
            let compressed_bytes = base64::decode_config(compressed_data, base64::URL_SAFE_NO_PAD)?;
            let decompressed = self.compressor.decompress(&compressed_bytes)?;
            let jwt_token = String::from_utf8(decompressed)?;

            self.jwt_manager.validate_token_fast(&jwt_token)
        } else {
            // Standard JWT validation
            self.jwt_manager.validate_token_fast(token)
        }
    }
}
```

## Memory Management


### 1. Memory Pool Optimization


```rust
use auth_framework::{MemoryPool, PooledBuffer};

pub struct MemoryOptimizedAuthService {
    buffer_pool: MemoryPool<Vec<u8>>,
    string_pool: MemoryPool<String>,
}

impl MemoryOptimizedAuthService {
    pub fn new() -> Self {
        Self {
            buffer_pool: MemoryPool::new(
                || Vec::with_capacity(4096),  // Pre-allocate 4KB buffers
                |mut buf| {
                    buf.clear();
                    buf.shrink_to(4096);  // Don't let buffers grow too large
                    buf
                }
            ),
            string_pool: MemoryPool::new(
                || String::with_capacity(512),
                |mut s| {
                    s.clear();
                    s.shrink_to(512);
                    s
                }
            ),
        }
    }

    pub async fn process_auth_request(&self, request: &[u8]) -> Result<Vec<u8>, AuthError> {
        // Get buffer from pool
        let mut buffer = self.buffer_pool.get().await;

        // Process request using pooled buffer
        buffer.extend_from_slice(request);
        let processed = self.process_buffer(&buffer).await?;

        // Buffer is automatically returned to pool when dropped
        Ok(processed)
    }
}
```

### 2. Zero-Copy Operations


```rust
use auth_framework::{ZeroCopyParser, ByteSliceExt};

pub struct ZeroCopyAuthProcessor {
    parser: ZeroCopyParser,
}

impl ZeroCopyAuthProcessor {
    pub fn parse_auth_header(&self, header_bytes: &[u8]) -> Result<AuthInfo, ParseError> {
        // Parse without copying data
        let (scheme, token) = self.parser.split_auth_header(header_bytes)?;

        match scheme {
            b"Bearer" => {
                // Validate token without copying
                let token_info = self.parse_jwt_zero_copy(token)?;
                Ok(AuthInfo::Bearer(token_info))
            }
            b"Basic" => {
                // Decode basic auth without intermediate allocations
                let decoded = self.decode_basic_auth_zero_copy(token)?;
                Ok(AuthInfo::Basic(decoded))
            }
            _ => Err(ParseError::UnsupportedScheme),
        }
    }

    fn parse_jwt_zero_copy(&self, token_bytes: &[u8]) -> Result<TokenInfo, ParseError> {
        // Parse JWT header and payload without string allocations
        let parts = token_bytes.split_by(b'.');
        if parts.len() != 3 {
            return Err(ParseError::InvalidJwtFormat);
        }

        let header = self.decode_base64_zero_copy(parts[0])?;
        let payload = self.decode_base64_zero_copy(parts[1])?;

        // Parse JSON without string allocation where possible
        let token_info = self.parse_jwt_payload(&payload)?;
        Ok(token_info)
    }
}
```

---

*AuthFramework v0.4.0 - Performance Optimization Guide*