41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
# app/core/deps.py
|
|
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
from jose import JWTError, jwt
|
|
from sqlalchemy.orm import Session
|
|
from app.core.database import SessionLocal
|
|
from app.models.user import User
|
|
from app.core.security import SECRET_KEY, ALGORITHM
|
|
|
|
oauth2_scheme = HTTPBearer()
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
def get_current_user(
|
|
token: HTTPAuthorizationCredentials = Depends(oauth2_scheme),
|
|
db: Session = Depends(get_db)
|
|
) -> User:
|
|
credentials_exception = HTTPException(
|
|
status_code=401,
|
|
detail="Could not validate credentials",
|
|
)
|
|
|
|
try:
|
|
payload = jwt.decode(token.credentials, SECRET_KEY, algorithms=[ALGORITHM])
|
|
user_id: str = payload.get("sub")
|
|
if user_id is None:
|
|
raise credentials_exception
|
|
except JWTError:
|
|
raise credentials_exception
|
|
|
|
user = db.query(User).filter(User.id == int(user_id)).first()
|
|
if user is None:
|
|
raise credentials_exception
|
|
return user
|