-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
51 lines (40 loc) · 1.47 KB
/
auth.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from sqlalchemy.orm import Session
from app.config import app_config
from db import get_db
from models import User
# These should be in your environment variables
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
SECRET_KEY = app_config.SECRET_KEY
ALGORITHM = app_config.ALGORITHM
async def get_current_user(
token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)
) -> User:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
# Decode JWT token
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
email: str = payload.get("sub")
if email is None:
raise credentials_exception
except JWTError:
raise credentials_exception
# Get user from database
UserQ = User.__sqlmodel__
user = db.query(UserQ).filter(UserQ.email == email).first()
if user is None:
raise credentials_exception
return user
# Optional: Get current admin user
async def get_current_admin(current_user: User = Depends(get_current_user)) -> User:
if not current_user.is_admin:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
)
return current_user