39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
from fastapi import APIRouter, Depends, Response
|
|
from redis.asyncio import Redis
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.config import Settings
|
|
from app.domain.auth import create_session, revoke_session
|
|
from app.endpoints.dependencies import (
|
|
get_basic_credentials,
|
|
get_bearer_token,
|
|
get_db,
|
|
get_redis,
|
|
get_settings,
|
|
)
|
|
from app.schemas.session import AuthSession
|
|
|
|
router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
|
|
|
|
|
|
@router.post("/sessions", status_code=201)
|
|
async def login(
|
|
credentials: tuple[str, str] = Depends(get_basic_credentials),
|
|
session: AsyncSession = Depends(get_db),
|
|
redis: Redis = Depends(get_redis),
|
|
settings: Settings = Depends(get_settings),
|
|
) -> AuthSession:
|
|
username, password = credentials
|
|
return await create_session(
|
|
session, redis, username, password, settings.session_ttl_seconds
|
|
)
|
|
|
|
|
|
@router.delete("/sessions/current", status_code=204)
|
|
async def logout(
|
|
token: str = Depends(get_bearer_token),
|
|
redis: Redis = Depends(get_redis),
|
|
) -> Response:
|
|
await revoke_session(redis, token)
|
|
return Response(status_code=204)
|