feat: Add authentication middleware and integration documentation#36
Merged
feat: Add authentication middleware and integration documentation#36
Conversation
Provide authentication middleware for automatic enforcement, configuration examples, and comprehensive integration guide for enabling auth in services.
Changes:
- Add AuthenticationMiddleware for automatic auth enforcement
- Create generate_auth_config.py utility script
- Add .env.auth.example configuration template
- Add docs/AUTHENTICATION.md integration guide
Authentication Middleware (shared/auth/middleware.py):
- Automatic JWT and API key validation
- Configurable exempt paths (health, docs, metrics)
- Attaches UserIdentity to request.state.user
- Returns 401 for missing/invalid credentials
- Easy integration with FastAPI applications
Configuration Generator (scripts/generate_auth_config.py):
- Generate secure JWT secrets (32-byte entropy)
- Generate example API keys
- Provides setup instructions
- Helps developers get started quickly
Environment Template (.env.auth.example):
- All authentication settings documented
- Clear descriptions and defaults
- Security best practices included
- Example values for development
Integration Guide (docs/AUTHENTICATION.md):
- Quick start instructions
- Two integration options (middleware vs dependencies)
- JWT token creation examples
- API key management guide
- Permission system explanation
- Client authentication examples
- Service integration patterns
- Security best practices
- Troubleshooting guide
Middleware Usage:
```python
from shared.auth.middleware import AuthenticationMiddleware
app.add_middleware(
AuthenticationMiddleware,
jwt_handler=jwt_handler,
api_key_handler=api_key_handler,
exempt_paths=["/health", "/docs"],
)
```
Benefits:
- Zero-touch authentication for all routes
- Automatic 401 responses
- User identity in request.state
- Flexible exempt paths
- Works with both JWT and API keys
- Optional per-service
Security:
- Validates JWT signatures
- Checks token expiration
- Verifies API key hashes
- Prevents unauthorized access
- Logs authentication failures
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AuthenticationMiddlewarefor automatic authentication enforcement across all routesgenerate_auth_config.pyscript to generate JWT secrets and API keys for developmentdocs/AUTHENTICATION.md.env.auth.exampletemplate for easy configurationWhat's Included
Authentication Middleware
UserIdentitytorequest.state.userDevelopment Tools
Configuration
Documentation
Integration Options
Option 1: Middleware (Recommended)
Option 2: Dependencies
Test Plan
🤖 Generated with Claude Code