init
This commit is contained in:
29
auth/app/utils.py
Normal file
29
auth/app/utils.py
Normal file
@ -0,0 +1,29 @@
|
||||
from jose import jwt, JWTError
|
||||
from passlib.context import CryptContext
|
||||
from datetime import datetime, timedelta
|
||||
import os
|
||||
|
||||
# Настройки
|
||||
SECRET_KEY = os.getenv("SECRET_KEY", "secret")
|
||||
ALGORITHM = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 1440 # 24 часа
|
||||
|
||||
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(data: dict) -> str:
|
||||
to_encode = data.copy()
|
||||
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
to_encode.update({"exp": expire})
|
||||
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||
|
||||
def decode_token(token: str):
|
||||
try:
|
||||
return jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
except JWTError:
|
||||
return None
|
Reference in New Issue
Block a user