mirror of
https://github.com/zylon-ai/private-gpt.git
synced 2025-12-22 23:22:57 +01:00
Added users model for authentication with roles and user roles
This commit is contained in:
parent
0a89d76cc5
commit
b67b66cd44
53 changed files with 1955 additions and 43 deletions
44
private_gpt/users/core/security.py
Normal file
44
private_gpt/users/core/security.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
from passlib.context import CryptContext
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Union, Any
|
||||
from jose import jwt
|
||||
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 30 # 30 minutes
|
||||
REFRESH_TOKEN_EXPIRE_MINUTES = 60 * 24 * 7 # 7 days
|
||||
ALGORITHM = "HS256"
|
||||
# JWT_SECRET_KEY = os.environ['JWT_SECRET_KEY'] # should be kept secret
|
||||
# JWT_REFRESH_SECRET_KEY = os.environ['JWT_REFRESH_SECRET_KEY'] # should be kept secret
|
||||
JWT_SECRET_KEY = "QUICKGPT"
|
||||
JWT_REFRESH_SECRET_KEY = "QUICKGPT_REFRESH"
|
||||
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue