cognee/cognee/modules/users/authentication/get_client_auth_backend.py
maozhen 4a61e774d5 ```
fix(auth): add error handling for JWT lifetime configuration

- Add try-catch block to handle invalid JWT_LIFETIME_SECONDS environment variable
- Default to 360 seconds when environment variable is not a valid integer
- Apply same fix to both API and client authentication backendsdocs(docker): add security warning for CORS configuration

- Add comment warning about default CORS_ALLOWED_ORIGINS setting
- Emphasize need to override wildcard with specific domains in production
```
2025-12-29 14:39:13 +08:00

34 lines
893 B
Python

import os
from functools import lru_cache
from fastapi_users import models
from fastapi_users.authentication import (
JWTStrategy,
AuthenticationBackend,
)
from .default import default_transport
@lru_cache
def get_client_auth_backend():
transport = default_transport
def get_jwt_strategy() -> JWTStrategy[models.UP, models.ID]:
from .default.default_jwt_strategy import DefaultJWTStrategy
secret = os.getenv("FASTAPI_USERS_JWT_SECRET", "super_secret")
try:
lifetime_seconds = int(os.getenv("JWT_LIFETIME_SECONDS", "3600"))
except ValueError:
lifetime_seconds = 3600
return DefaultJWTStrategy(secret, lifetime_seconds=lifetime_seconds)
auth_backend = AuthenticationBackend(
name=transport.name,
transport=transport,
get_strategy=get_jwt_strategy,
)
return auth_backend