32 lines
1 KiB
Python
32 lines
1 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
from jose import JWTError, jwt
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.config import settings
|
|
from app.db.session import get_session
|
|
from app.models.user import User
|
|
|
|
bearer = HTTPBearer()
|
|
|
|
|
|
def current_user(
|
|
credentials: HTTPAuthorizationCredentials = Depends(bearer),
|
|
session: Session = Depends(get_session),
|
|
) -> User:
|
|
try:
|
|
payload = jwt.decode(
|
|
credentials.credentials,
|
|
settings.jwt_secret,
|
|
algorithms=[settings.jwt_algorithm],
|
|
)
|
|
user_id = payload.get("sub")
|
|
except JWTError as exc:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") from exc
|
|
user = session.get(User, user_id)
|
|
if user is None or not user.is_active:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Inactive user")
|
|
return user
|
|
|