-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependencies.py
More file actions
25 lines (23 loc) · 833 Bytes
/
dependencies.py
File metadata and controls
25 lines (23 loc) · 833 Bytes
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 fastapi import Depends, HTTPException
from main import SECRET_KEY, ALGORITHM
from models import db
from sqlalchemy.orm import sessionmaker, Session
from models import Usuario
from jose import jwt, JWTError
def pegar_sessao():
try:
Session = sessionmaker(bind=db)
session = Session()
yield session
finally:
session.close()
def verificar_token(token, session = Depends(pegar_sessao)):
try:
dic_info = jwt.decode(token, SECRET_KEY, ALGORITHM)
user_id = int(dic_info.get("sub"))
except JWTError:
raise HTTPException(status_code=401, detail="Acesso Negado, verifique a validade do token")
usuario = session.query(Usuario).filter(Usuario.id==user_id).first()
if not usuario:
raise HTTPException(status_code=401, detail="Acesso Inválido")
return usuario