mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-20 01:21:50 +03:00
327074c5d4
* feat(auth): backend authentification verification * feat(auth): added to all endpoints * feat(auth): added to all endpoints * feat(auth): redirect if not connected * chore(print): removed * feat(login): redirect * feat(icon): added * chore(yarn): removed lock * chore(gitignore): removed
26 lines
844 B
Python
26 lines
844 B
Python
from jose import jwt
|
|
from typing import Optional
|
|
from datetime import datetime, timedelta
|
|
from jose.exceptions import JWTError
|
|
import os
|
|
|
|
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:
|
|
print(f"JWTError: {str(e)}")
|
|
return None |