mirror of
https://github.com/reconurge/flowsint.git
synced 2026-05-03 18:09:52 -05:00
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from jose import JWTError, jwt
|
|
from sqlalchemy.orm import Session
|
|
from flowsint_core.core.auth import ALGORITHM
|
|
from flowsint_core.core.postgre_db import get_db
|
|
from flowsint_core.core.models import Profile
|
|
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
# Remplace avec ton URL de BDD
|
|
AUTH_SECRET = os.getenv("AUTH_SECRET")
|
|
|
|
load_dotenv()
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
|
|
|
|
def get_current_user(
|
|
token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)
|
|
) -> Profile:
|
|
credentials_exception = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Could not validate credentials",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
try:
|
|
payload = jwt.decode(token, AUTH_SECRET, algorithms=[ALGORITHM])
|
|
email: str = payload.get("sub")
|
|
if email is None:
|
|
raise credentials_exception
|
|
except JWTError:
|
|
raise credentials_exception
|
|
user = db.query(Profile).filter(Profile.email == email).first()
|
|
if user is None:
|
|
raise credentials_exception
|
|
return user
|