-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoauth.py
25 lines (19 loc) · 816 Bytes
/
oauth.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
from sqlalchemy.orm import Session
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
import token_key
from typing import Annotated
from model.users_model import User
from database.db import get_db
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login")
def get_current_user(token: Annotated[str, Depends(oauth2_scheme)], db: Session = Depends(get_db)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
token_data = token_key.verify_token(token, credentials_exception)
user = db.query(User).filter(User.email == token_data.email).first()
if not user:
raise credentials_exception
return user