mirror of
https://github.com/zylon-ai/private-gpt.git
synced 2025-12-22 20:12:55 +01:00
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
import random
|
|
import string
|
|
from datetime import datetime, timedelta
|
|
from typing import Dict, Any, Optional, Union
|
|
|
|
from jose import JWTError, jwt
|
|
from passlib.context import CryptContext
|
|
from private_gpt.users.core.config import settings
|
|
|
|
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 * 1 # 12 hrs # Default Value
|
|
REFRESH_TOKEN_EXPIRE_MINUTES = 60 * 24 * 7 # 7 days # Default Value
|
|
ALGORITHM = "HS256"
|
|
JWT_SECRET_KEY = settings.SECRET_KEY
|
|
JWT_REFRESH_SECRET_KEY = settings.REFRESH_KEY
|
|
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
|
return pwd_context.verify(plain_password, hashed_password)
|
|
|
|
|
|
def get_password_hash(password: str) -> str:
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
def create_access_token(subject: Union[str, Any], expires_delta: int = None) -> str:
|
|
if expires_delta is not None:
|
|
expires_delta = datetime.utcnow() + expires_delta
|
|
else:
|
|
expires_delta = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
|
|
to_encode = {"exp": expires_delta, **subject}
|
|
encoded_jwt = jwt.encode(to_encode, JWT_SECRET_KEY, ALGORITHM)
|
|
return encoded_jwt
|
|
|
|
|
|
def create_refresh_token(subject: Union[str, Any], expires_delta: int = None) -> str:
|
|
if expires_delta is not None:
|
|
expires_delta = datetime.utcnow() + expires_delta
|
|
else:
|
|
expires_delta = datetime.utcnow() + timedelta(minutes=REFRESH_TOKEN_EXPIRE_MINUTES)
|
|
|
|
to_encode = {"exp": expires_delta, **subject}
|
|
encoded_jwt = jwt.encode(to_encode, JWT_REFRESH_SECRET_KEY, ALGORITHM)
|
|
return encoded_jwt
|
|
|
|
def generate_random_password(length: int = 12) -> str:
|
|
"""
|
|
Generate a random password.
|
|
"""
|
|
characters = string.ascii_letters + string.digits
|
|
return ''.join(random.choice(characters) for i in range(length))
|
|
|
|
|
|
def verify_refresh_token(token: str) -> Optional[Dict[str, Any]]:
|
|
try:
|
|
payload = jwt.decode(token, JWT_REFRESH_SECRET_KEY,
|
|
algorithms=[ALGORITHM])
|
|
return payload
|
|
except JWTError:
|
|
return None
|
|
|