cognee/cognee/modules/users/authentication/get_api_auth_backend.py
2026-01-07 16:26:36 +01:00

32 lines
841 B
Python

import os
from functools import lru_cache
from fastapi_users import models
from fastapi_users.authentication import (
JWTStrategy,
AuthenticationBackend,
)
from .api_bearer import api_bearer_transport, APIJWTStrategy
@lru_cache
def get_api_auth_backend():
transport = api_bearer_transport
def get_jwt_strategy() -> JWTStrategy[models.UP, models.ID]:
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 APIJWTStrategy(secret, lifetime_seconds=lifetime_seconds)
auth_backend = AuthenticationBackend(
name=transport.name,
transport=transport,
get_strategy=get_jwt_strategy,
)
return auth_backend