Skip to content

feat: Add authentication middleware and integration documentation#36

Merged
elmorem merged 1 commit intomainfrom
feat/integrate-auth-with-gateway
Dec 11, 2025
Merged

feat: Add authentication middleware and integration documentation#36
elmorem merged 1 commit intomainfrom
feat/integrate-auth-with-gateway

Conversation

@elmorem
Copy link
Owner

@elmorem elmorem commented Dec 11, 2025

Summary

  • Adds AuthenticationMiddleware for automatic authentication enforcement across all routes
  • Creates generate_auth_config.py script to generate JWT secrets and API keys for development
  • Provides comprehensive authentication integration guide in docs/AUTHENTICATION.md
  • Includes .env.auth.example template for easy configuration

What's Included

Authentication Middleware

  • shared/auth/middleware.py: Automatic authentication enforcement
    • Validates JWT Bearer tokens and API keys
    • Configurable exempt paths (health, docs, metrics)
    • Returns 401 for invalid authentication
    • Attaches UserIdentity to request.state.user

Development Tools

  • scripts/generate_auth_config.py: Generates JWT secrets and API keys
    • Executable script for quick setup
    • Creates secure random secrets
    • Provides example configuration

Configuration

  • .env.auth.example: Complete configuration template
    • JWT settings (secret, algorithm, expiration)
    • API key settings
    • Authentication enforcement toggles
    • Exempt paths configuration

Documentation

  • docs/AUTHENTICATION.md: Comprehensive 400+ line guide
    • Quick start instructions
    • Two integration patterns (middleware vs dependencies)
    • JWT token creation examples
    • API key management
    • Permission system documentation
    • Security best practices
    • Troubleshooting guide

Integration Options

Option 1: Middleware (Recommended)

app.add_middleware(
    AuthenticationMiddleware,
    jwt_handler=jwt_handler,
    api_key_handler=api_key_handler,
    exempt_paths=["/health", "/docs", "/metrics"],
)

Option 2: Dependencies

@app.get("/sessions")
async def list_sessions(
    user: Annotated[UserIdentity, Depends(get_current_user)],
):
    return {"user_id": user.user_id}

Test Plan

  • Middleware created with proper authentication flow
  • Generate script creates valid JWT secrets
  • Generate script creates valid API keys
  • Configuration template includes all settings
  • Documentation covers all integration patterns
  • Code formatted with black
  • Type checking passed with mypy
  • All files committed and pushed

🤖 Generated with Claude Code

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>
@elmorem elmorem merged commit 7dc70c7 into main Dec 11, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant