Overview
Infrastructure services provide concrete implementations of service interfaces defined in the application layer. These handle cross-cutting concerns like security, token management, and external integrations.Service Categories
- Security Services: Password hashing, encryption
- Token Services: JWT creation and verification
- External Services: Email, SMS, cloud storage
- Caching Services: Redis, in-memory caching
JWT Token Service
Location
src/features/auth/infrastructure/services/security/jwt_handler.py:8
Implementation
Creating Access Tokens
jwt_handler.py:23-43
Key Features:
- Default 15-minute expiration (900 seconds)
- Standard JWT claims:
exp,iat,nbf - Custom
typeclaim to distinguish token types - Optional issuer and audience validation
Creating Refresh Tokens
jwt_handler.py:45-64
Key Features:
- Default 30-day expiration (2,592,000 seconds)
- Marked with
type: refreshto prevent misuse as access token - Same security standards as access tokens
Token Verification
jwt_handler.py:66-84
Verification includes:
- Signature validation
- Expiration check
- Issuer validation (if configured)
- Audience validation (if configured)
Token Utilities
Decode Without Verification
jwt_handler.py:86-96
Useful for inspecting expired tokens or debugging.
Check Expiration
jwt_handler.py:98-105
Get Expiry Date
jwt_handler.py:107-112
Password Hashing Service
Location
src/features/auth/infrastructure/services/security/password_hasher.py:7
Implementation
password_hasher.py:10-19
Argon2 Configuration
- Winner of Password Hashing Competition (2015)
- Resistant to GPU cracking
- Memory-hard algorithm
- Configurable resource usage
Hashing Passwords
password_hasher.py:21-38
Returns:
- Hashed password string
- Algorithm identifier (for future migrations)
Verifying Passwords
password_hasher.py:40-69
Key Features:
- Auto-detects algorithm from hash format
- Supports bcrypt (
$2b$prefix) - Supports Argon2 (
$argon2prefix) - Graceful error handling
Algorithm Migration
password_hasher.py:71-79
Use this to migrate from bcrypt to Argon2:
Service Configuration
Dependency Injection
Environment Variables
Best Practices
Security
-
Secret Key Management
- Never commit secrets to version control
- Use environment variables or secret managers
- Rotate keys periodically
-
Token Expiration
- Short-lived access tokens (15 minutes)
- Long-lived refresh tokens (30 days)
- Implement token revocation
-
Password Hashing
- Always use Argon2 or bcrypt
- Never use MD5, SHA1, or plain SHA256
- Configure appropriate cost factors
Error Handling
Testing
Related Documentation
- Repositories - Data access layer
- Models - Database models
- Use Cases - Application services that use infrastructure services
- JWT Configuration - Security best practices