Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions backend/dwf/api/deps.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
from collections.abc import AsyncGenerator
from fastapi import Request

from fastapi import Depends, HTTPException, Request, status
from fastapi.security import OAuth2PasswordBearer
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select

from dwf.infrastructure.database.models import User
from dwf.services.auth_service import AuthService
from dwf.settings import Settings

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")


async def get_db(request: Request) -> AsyncGenerator[AsyncSession, None]:
session_factory = request.app.state.session_factory
session = session_factory()

try:
yield session
# await session.commit()
# except Exception:
# await session.rollback()
# raise
finally:
await session.close()
await session.close()


async def get_current_user(
token: str = Depends(oauth2_scheme),
db: AsyncSession = Depends(get_db),
settings: Settings = Depends(),
) -> User:
auth_service = AuthService(db, settings)
try:
payload = auth_service.decode_token(token)
except Exception:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token"
)

if payload.get("type") != "access":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token type"
)

result = await db.execute(select(User).where(User.id == payload["sub"]))
user = result.scalar_one_or_none()
if user is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found"
)

return user


async def get_current_active_user(
current_user: User = Depends(get_current_user),
) -> User:
if not current_user.is_active:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user"
)
return current_user
Loading