33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPBearer
|
|
from jose import jwt, JWTError
|
|
from app.core.config import settings
|
|
from app.db.session import get_session
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from app.models.user import User
|
|
from sqlalchemy import select
|
|
|
|
bearer = HTTPBearer()
|
|
|
|
async def get_db() -> AsyncSession:
|
|
async for s in get_session():
|
|
yield s
|
|
|
|
async def get_current_user(token=Depends(bearer), db: AsyncSession = Depends(get_db)) -> User:
|
|
try:
|
|
payload = jwt.decode(token.credentials, settings.SECRET_KEY, algorithms=["HS256"])
|
|
email: str = payload.get("sub")
|
|
except JWTError:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
|
|
res = await db.execute(select(User).where(User.email == email))
|
|
user = res.scalar_one_or_none()
|
|
if not user:
|
|
raise HTTPException(status_code=401, detail="User not found")
|
|
return user
|
|
|
|
def require_role(*roles: str):
|
|
async def _dep(user: User = Depends(get_current_user)):
|
|
if user.role not in roles:
|
|
raise HTTPException(status_code=403, detail="Insufficient role")
|
|
return user
|
|
return _dep |