martian7777
feat: implement backend core with ORM models, authentication, and AI-driven telemetry diagnostics
e5be436 | """Common FastAPI dependencies: DB session and current authenticated user.""" | |
| from __future__ import annotations | |
| import uuid | |
| from typing import Annotated | |
| from fastapi import Depends | |
| from fastapi.security import OAuth2PasswordBearer | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.core.config import settings | |
| from app.core.database import get_db | |
| from app.core.exceptions import AuthenticationError | |
| from app.core.security import decode_access_token | |
| from app.models.user import User | |
| from app.repositories.user_repo import UserRepository | |
| oauth2_scheme = OAuth2PasswordBearer( | |
| tokenUrl=f"{settings.api_v1_prefix}/auth/login", | |
| auto_error=False, | |
| ) | |
| DbSession = Annotated[AsyncSession, Depends(get_db)] | |
| async def get_current_user( | |
| session: DbSession, | |
| token: Annotated[str | None, Depends(oauth2_scheme)], | |
| ) -> User: | |
| if not token: | |
| raise AuthenticationError("Not authenticated") | |
| subject = decode_access_token(token) | |
| try: | |
| user_id = uuid.UUID(subject) | |
| except ValueError as exc: | |
| raise AuthenticationError("Invalid token subject") from exc | |
| user = await UserRepository(session).get(user_id) | |
| if user is None: | |
| raise AuthenticationError("User not found") | |
| if not user.is_active: | |
| raise AuthenticationError("Inactive user") | |
| return user | |
| CurrentUser = Annotated[User, Depends(get_current_user)] | |