auth-framework 0.5.0-rc19

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
# AuthFramework v0.5.0-rc19 - Production Deployment Guide


## Prerequisites


### System Requirements


- **OS**: Linux (Ubuntu 20.04+, CentOS 8+, or equivalent)
- **Memory**: Minimum 4GB RAM (8GB+ recommended)
- **CPU**: Minimum 2 cores (4+ cores recommended)
- **Storage**: 20GB+ available disk space
- **Network**: HTTPS-capable with valid SSL certificates

### Required Software


- Docker 20.10+
- Docker Compose v2.0+
- Git
- OpenSSL (for certificate generation)

## Quick Start (5 Minutes)


### 1. Clone Repository


```bash
git clone https://github.com/ciresnave/auth-framework.git
cd auth-framework
```

### 2. Generate Secrets


```bash
# Create secrets directory

mkdir -p secrets

# Generate secure passwords and keys

openssl rand -base64 32 > secrets/db_password.txt
openssl rand -base64 32 > secrets/redis_password.txt
openssl rand -base64 64 > secrets/jwt_secret.txt
openssl rand -base64 32 > secrets/grafana_password.txt

# Set proper permissions

chmod 600 secrets/*.txt
```

### 3. Configure Environment


```bash
# Copy example environment file

cp .env.example .env

# Edit configuration

vim .env
```

**.env Configuration:**

```bash
# Domain configuration

DOMAIN=auth.yourdomain.com
ADMIN_EMAIL=admin@yourdomain.com

# Security settings

REQUIRE_MFA=true
SESSION_TIMEOUT=3600
RATE_LIMIT_ENABLED=true

# Performance settings

DB_MAX_CONNECTIONS=100
REDIS_MAX_MEMORY=256mb
APP_WORKER_THREADS=4

# SSL/TLS

SSL_CERT_PATH=/etc/nginx/ssl/fullchain.pem
SSL_KEY_PATH=/etc/nginx/ssl/privkey.pem
```

### 4. SSL Certificate Setup


#### Option A: Let's Encrypt (Recommended)


```bash
# Install certbot

sudo apt update && sudo apt install certbot

# Generate certificate

sudo certbot certonly --standalone \
  -d auth.yourdomain.com \
  --email admin@yourdomain.com \
  --agree-tos --non-interactive

# Copy certificates

sudo cp /etc/letsencrypt/live/auth.yourdomain.com/fullchain.pem nginx/ssl/
sudo cp /etc/letsencrypt/live/auth.yourdomain.com/privkey.pem nginx/ssl/
sudo chown $USER:$USER nginx/ssl/*.pem
```

#### Option B: Self-Signed (Development Only)


```bash
# Generate self-signed certificate

openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout nginx/ssl/privkey.pem \
  -out nginx/ssl/fullchain.pem \
  -subj "/C=US/ST=State/L=City/O=Organization/CN=auth.yourdomain.com"
```

### 5. Deploy


```bash
# Production deployment

docker-compose -f docker-compose.production.yml up -d

# Verify deployment

docker-compose -f docker-compose.production.yml ps
```

### 6. Verify Installation


```bash
# Check health

curl -k https://auth.yourdomain.com/health

# Check logs

docker-compose -f docker-compose.production.yml logs -f auth-server
```

## Detailed Configuration


### Database Configuration


#### PostgreSQL Optimization


Create `postgres/postgresql.conf`:

```conf
# Memory settings

shared_buffers = 256MB
effective_cache_size = 1GB
work_mem = 4MB
maintenance_work_mem = 64MB

# Connection settings

max_connections = 100
listen_addresses = '*'

# Performance settings

random_page_cost = 1.1
effective_io_concurrency = 200
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
```

#### Database Backup


```bash
# Automated backup script

#!/bin/bash

BACKUP_DIR="/backups/postgres"
DATE=$(date +%Y%m%d_%H%M%S)

docker exec postgres pg_dump -U auth_user auth_framework | \
  gzip > "$BACKUP_DIR/auth_framework_$DATE.sql.gz"

# Keep only last 7 days

find "$BACKUP_DIR" -name "*.sql.gz" -mtime +7 -delete
```

### Redis Configuration


Create `redis/redis.conf`:

```conf
# Security

bind 127.0.0.1
protected-mode yes
requirepass-file /run/secrets/redis_password

# Persistence

appendonly yes
appendfsync everysec
save 900 1
save 300 10
save 60 10000

# Memory management

maxmemory 256mb
maxmemory-policy allkeys-lru

# Performance

tcp-keepalive 300
timeout 0
```

### Application Configuration


Create `config/production.toml`:

```toml
[server]
host = "0.0.0.0"
port = 8080
worker_threads = 4
max_connections = 100

[database]
url = "postgresql://auth_user@postgres:5432/auth_framework"
max_connections = 20
min_connections = 5
connect_timeout = 30

[redis]
url = "redis://redis:6379"
pool_size = 10
timeout = 5

[jwt]
algorithm = "RS256"
access_token_ttl = 3600
refresh_token_ttl = 86400
issuer = "https://auth.yourdomain.com"
audience = "authframework-api"

[security]
min_password_length = 12
require_password_complexity = true
password_hash_algorithm = "Argon2"
secure_cookies = true
csrf_protection = true
session_timeout = 3600

[rate_limiting]
enabled = true
requests_per_minute = 100
burst_size = 20

[logging]
level = "info"
format = "json"
destination = "stdout"

[monitoring]
metrics_enabled = true
metrics_path = "/metrics"
health_check_path = "/health"
```

## Security Hardening


### Firewall Configuration


```bash
# Configure UFW firewall

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
```

### System Security


```bash
# Disable unnecessary services

sudo systemctl disable apache2 2>/dev/null || true
sudo systemctl disable nginx 2>/dev/null || true

# Update system

sudo apt update && sudo apt upgrade -y

# Install security updates automatically

echo 'Unattended-Upgrade::Automatic-Reboot "false";' | \
  sudo tee -a /etc/apt/apt.conf.d/50unattended-upgrades
```

### Container Security


```bash
# Run Docker security benchmark

docker run --rm --net host --pid host --userns host --cap-add audit_control \
  -e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \
  -v /etc:/etc:ro \
  -v /var/lib:/var/lib:ro \
  -v /var/run/docker.sock:/var/run/docker.sock:ro \
  --label docker_bench_security \
  docker/docker-bench-security
```

## Monitoring and Observability


### Grafana Dashboard Setup


1. Access Grafana: `https://auth.yourdomain.com:3000`
2. Login with admin credentials from `secrets/grafana_password.txt`
3. Import dashboards:
   - AuthFramework Application Metrics
   - Infrastructure Monitoring
   - Security Events

### Log Management


```bash
# Configure log rotation

sudo tee /etc/logrotate.d/authframework << EOF
/var/log/authframework/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    copytruncate
}
EOF
```

### Alerting Setup


```yaml
# Add to monitoring/alert_rules.yml

groups:
  - name: authframework
    rules:
      - alert: HighErrorRate
        expr: rate(http_requests_total{status=~"5.."}[5m]) > 0.1
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: High error rate detected

      - alert: DatabaseConnectionHigh
        expr: postgres_stat_activity_count > 80
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: High database connection usage
```

## Performance Tuning

### Application Performance

```bash
# Optimize for production
export RUST_LOG=auth_framework=info
export TOKIO_WORKER_THREADS=4
export DATABASE_POOL_SIZE=20
export REDIS_POOL_SIZE=10
```

### Database Performance


```sql
-- Create optimized indexes
CREATE INDEX CONCURRENTLY idx_users_username ON users(username);
CREATE INDEX CONCURRENTLY idx_tokens_user_id ON tokens(user_id);
CREATE INDEX CONCURRENTLY idx_sessions_expires_at ON sessions(expires_at);

-- Analyze tables
ANALYZE users;
ANALYZE tokens;
ANALYZE sessions;
```

### Nginx Performance


```nginx
# Add to nginx.conf

worker_processes auto;
worker_connections 1024;
worker_rlimit_nofile 2048;

# Enable caching

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=auth_cache:10m
                 max_size=100m inactive=60m use_temp_path=off;

# In server block

proxy_cache auth_cache;
proxy_cache_valid 200 5m;
proxy_cache_valid 404 1m;
```

## Scaling and High Availability


### Horizontal Scaling


```bash
# Scale application servers

docker-compose -f docker-compose.production.yml up -d --scale auth-server=3

# Load balancer configuration update automatically

```

### Database Clustering


```yaml
# PostgreSQL cluster with replication

version: '3.8'
services:
  postgres-primary:
    image: postgres:15
    environment:
      POSTGRES_REPLICATION_MODE: master
      POSTGRES_REPLICATION_USER: replicator
      POSTGRES_REPLICATION_PASSWORD: replica_password

  postgres-replica:
    image: postgres:15
    environment:
      POSTGRES_REPLICATION_MODE: slave
      POSTGRES_MASTER_HOST: postgres-primary
      POSTGRES_REPLICATION_USER: replicator
      POSTGRES_REPLICATION_PASSWORD: replica_password
```

### Redis Clustering


```yaml
# Redis Sentinel for high availability

redis-sentinel:
  image: redis:7-alpine
  command: redis-sentinel /etc/redis/sentinel.conf
  volumes:
    - ./redis/sentinel.conf:/etc/redis/sentinel.conf
```

## Backup and Disaster Recovery


### Automated Backup Script


```bash
#!/bin/bash

# /opt/authframework/backup.sh


BACKUP_DIR="/backups/authframework"
DATE=$(date +%Y%m%d_%H%M%S)

# Database backup

docker exec postgres pg_dump -U auth_user auth_framework | \
  gzip > "$BACKUP_DIR/db_$DATE.sql.gz"

# Redis backup

docker exec redis redis-cli --rdb - | \
  gzip > "$BACKUP_DIR/redis_$DATE.rdb.gz"

# Configuration backup

tar -czf "$BACKUP_DIR/config_$DATE.tar.gz" \
  config/ secrets/ nginx/ monitoring/

# Upload to S3 (optional)

if [ "$BACKUP_TO_S3" = "true" ]; then
  aws s3 sync "$BACKUP_DIR" "s3://$S3_BACKUP_BUCKET/authframework/"
fi

# Cleanup old backups

find "$BACKUP_DIR" -type f -mtime +30 -delete
```

### Disaster Recovery Plan


1. **Service Restoration**:
   - Deploy from git repository
   - Restore database from latest backup
   - Update DNS if necessary

2. **Data Recovery**:
   - PostgreSQL point-in-time recovery
   - Redis data restoration
   - Configuration restoration

3. **Testing**:
   - Monthly disaster recovery drills
   - Backup integrity verification

## Troubleshooting


### Common Issues


#### Connection Errors


```bash
# Check container connectivity

docker network ls
docker exec auth-server ping postgres
docker exec auth-server ping redis
```

#### Performance Issues


```bash
# Monitor resource usage

docker stats
docker-compose logs auth-server | grep -i "slow\|timeout\|error"
```

#### SSL Certificate Issues


```bash
# Verify certificate

openssl x509 -in nginx/ssl/fullchain.pem -text -noout
openssl verify nginx/ssl/fullchain.pem

# Test SSL configuration

openssl s_client -connect auth.yourdomain.com:443 -servername auth.yourdomain.com
```

### Debug Mode


```bash
# Enable debug logging

docker-compose -f docker-compose.production.yml \
  exec auth-server sh -c 'RUST_LOG=debug /usr/local/bin/auth-framework-cli server'
```

## Maintenance


### Regular Maintenance Tasks


```bash
# Weekly maintenance script

#!/bin/bash


# Update containers

docker-compose -f docker-compose.production.yml pull
docker-compose -f docker-compose.production.yml up -d

# Database maintenance

docker exec postgres vacuumdb -U auth_user auth_framework
docker exec postgres reindexdb -U auth_user auth_framework

# Log rotation

docker-compose -f docker-compose.production.yml logs --no-color | \
  tail -n 10000 > /var/log/authframework/application.log

# Security updates

apt update && apt upgrade -y
```

### Certificate Renewal


```bash
# Automated Let's Encrypt renewal

0 3 * * * certbot renew --quiet && \
  cp /etc/letsencrypt/live/auth.yourdomain.com/*.pem nginx/ssl/ && \
  docker-compose -f docker-compose.production.yml restart nginx
```

## Support and Resources


### Documentation


- **API Documentation**: `/docs/api/`
- **Architecture Guide**: `/docs/architecture/`
- **Security Guide**: `/docs/security/`

### Community


- **GitHub Issues**: <https://github.com/ciresnave/auth-framework/issues>
- **Discord Community**: <https://discord.gg/authframework>
- **Stack Overflow**: Tag `authframework`

### Professional Support


- **Enterprise Support**: <enterprise@authframework.com>
- **Consulting Services**: <consulting@authframework.com>
- **Training**: <training@authframework.com>

---

**Deployment Status**: Production Ready ✅
**Last Updated**: January 2024
**Version**: 0.4.0