-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdependencies.py
More file actions
35 lines (28 loc) · 1.17 KB
/
dependencies.py
File metadata and controls
35 lines (28 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# dependencies.py
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import jwt, JWTError
from sqlalchemy.orm import Session
import models, db_postgres
import os
# Secret key & algorithm (make sure SECRET_KEY matches what's used in users.py)
SECRET_KEY = os.getenv("JWT_SECRET", "fallback-secret")
ALGORITHM = "HS256"
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/users/login")
def authenticate_user(token: str, db: Session):
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
user_id: str = payload.get("sub")
if not user_id:
raise HTTPException(status_code=401, detail="Invalid token: 'sub' missing")
user = db.query(models.User).filter(models.User.id == user_id).first()
if not user:
raise HTTPException(status_code=401, detail="User not found")
return user
except JWTError as e:
raise HTTPException(status_code=401, detail=f"Token error: {str(e)}")
def get_current_user(
token: str = Depends(oauth2_scheme),
db: Session = Depends(db_postgres.get_db)
) -> models.User:
return authenticate_user(token, db)