mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-16 18:52:12 +03:00
33f49ee289
* feat: user can create api keys * fix: linting on build * Update backend/routes/api_key_routes.py * chore: rename and refactor AuthBearer * chore: add types
36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
import os
|
|
from datetime import datetime, timedelta
|
|
from typing import Optional
|
|
|
|
from jose import jwt
|
|
from jose.exceptions import JWTError
|
|
|
|
SECRET_KEY = os.environ.get("JWT_SECRET_KEY")
|
|
ALGORITHM = "HS256"
|
|
|
|
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
|
to_encode = data.copy()
|
|
if expires_delta:
|
|
expire = datetime.utcnow() + expires_delta
|
|
else:
|
|
expire = datetime.utcnow() + timedelta(minutes=15)
|
|
to_encode.update({"exp": expire})
|
|
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
|
return encoded_jwt
|
|
|
|
def decode_access_token(token: str):
|
|
try:
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM], options={"verify_aud": False})
|
|
return payload
|
|
except JWTError as e:
|
|
return None
|
|
|
|
def verify_token(token: str):
|
|
payload = decode_access_token(token)
|
|
return payload is not None
|
|
|
|
def get_user_email_from_token(token: str):
|
|
payload = decode_access_token(token)
|
|
if payload:
|
|
return payload.get("email")
|
|
return "none" |