feat: add bearer schema and get_current_user function

This commit is contained in:
Vladislav Syrochkin 2025-06-09 11:51:13 +05:00
parent a31758192d
commit c1d315d6e9
2 changed files with 13 additions and 2 deletions

View File

@ -1,7 +1,11 @@
from fastapi.security import HTTPBearer
from pydantic import BaseModel, ConfigDict
from pydantic.alias_generators import to_camel
bearer_schema = HTTPBearer() # схема для авторизации в swagger
class Base(BaseModel):
model_config = ConfigDict(
from_attributes=True,

View File

@ -1,3 +1,4 @@
from fastapi import Request, HTTPException
from typing import Optional
from sqlalchemy.ext.asyncio import AsyncConnection
from api.db.logic.auth import get_user
@ -9,11 +10,17 @@ from api.db.tables.account import AccountStatus
from api.utils.hasher import Hasher
async def get_current_user(request: Request) -> Optional[User]:
if not hasattr(request.state, "current_user"):
return HTTPException(status_code=401, detail="Unauthorized")
return request.state.current_user
async def authenticate_user(connection: AsyncConnection, username: str, password: str) -> Optional[User]:
sql_user, sql_password = await get_user(connection, username)
if not sql_user or sql_user.status != AccountStatus.ACTIVE :
return None
if not sql_user or sql_user.status != AccountStatus.ACTIVE:
return None
hasher = Hasher()
if not hasher.verify_data(password, sql_password.key_value):
return None