Description
Implement a JWT authentication system to protect sensitive API routes, following the project’s layered architecture and established patterns.
Architecture and Principles to Follow
- Layered Architecture: Maintain a clear separation between layers (core, models, schemas, repositories, service, routes)
- Repository Pattern: Preserve the existing data-access pattern
- Dependency Injection: Use the dependency system already implemented
- JOSE Library: Use
python-jose for handling JWT tokens
- Consistency: Follow the same naming conventions and module structure already used
Tasks to Implement
1. Configuration & Environment
2. Data Models (models/)
3. Validation Schemas (schemas/)
4. Security & JWT (core/security/)
5. Authentication Service (service/)
6. Authentication Dependencies (dependencies.py)
7. Protected Routes (routes/v1/)
8. Exception Handling (exceptions/)
9. API Documentation
Proposed Authentication Flow
- Login → Generate JWT token
- Protected Routes → Require token in header:
Authorization: Bearer <token>
- Validation → Middleware checks token and extracts user
- Authorization → Dependencies verify role-based permissions
Suggested File Structure
api_certify/
├── core/security/
│ ├── hash_manager.py (existing)
│ └── jwt_manager.py (new)
├── models/
│ └── jwt_model.py (new)
├── schemas/
│ └── token_schemas.py (new)
└── dependencies.py (updated)
Important Considerations
- Maintain compatibility with the existing password hashing system (bcrypt)
- Follow the existing response pattern (
SuccessResponse, ErrorResponse)
- Keep exception handling centralized
- Ensure tokens do not store sensitive data
- Implement logout with blacklist if needed
Required Validations
- Token expiration
- Signature validation
- User status checks
- Role-based access control
- Proper error messages
Recommended Tests
- Token generation and validation
- Accessing protected routes
- Handling expired/invalid tokens
- Role-based access restrictions
Note: This implementation must integrate seamlessly with the existing email/password authentication, adding the JWT layer exclusively for route protection.
Description
Implement a JWT authentication system to protect sensitive API routes, following the project’s layered architecture and established patterns.
Architecture and Principles to Follow
python-josefor handling JWT tokensTasks to Implement
1. Configuration & Environment
Add environment variables to
.env.example:JWT_SECRET_KEY: Secret key for token signingJWT_ALGORITHM: Encryption algorithm (e.g., HS256)JWT_EXPIRATION_MINUTES: Token expiration time2. Data Models (models/)
Create
models/jwt_model.pycontaining:Token: Model for access tokenTokenData: Model for decoded token dataTokenResponse: Model for login response3. Validation Schemas (schemas/)
Create
schemas/token_schemas.pywith Pydantic schemas for:4. Security & JWT (core/security/)
Create
core/security/jwt_manager.pycontaining:JWTManager: Class for managing JWT tokenspython-jose5. Authentication Service (service/)
Update
service/auth_service.py:generate_token()method toAuthService6. Authentication Dependencies (dependencies.py)
Create dependencies for:
get_current_user: Extract user from JWT tokenget_current_active_user: Validate active user7. Protected Routes (routes/v1/)
Update
routes/v1/certificate_routes.py:Create a refresh token endpoint if applicable
8. Exception Handling (exceptions/)
Add specific exceptions for:
9. API Documentation
Update Swagger auto-docs:
Proposed Authentication Flow
Authorization: Bearer <token>Suggested File Structure
Important Considerations
SuccessResponse,ErrorResponse)Required Validations
Recommended Tests
Note: This implementation must integrate seamlessly with the existing email/password authentication, adding the JWT layer exclusively for route protection.