32 lines
913 B
Python
32 lines
913 B
Python
from datetime import timedelta, datetime
|
|
from passlib.context import CryptContext
|
|
from jose import jwt
|
|
|
|
SECRET_KEY = "6ed23d121dd868fd6c0bf3fce00140ccc196f162f842fda574901052f2127e09"
|
|
ACCESS_TOKEN_EXPIRE_MINUTES = 15
|
|
AlGORITHM = "HS256"
|
|
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
|
|
def verify_password(plain_password, hashed_password):
|
|
return pwd_context.verify(plain_password, hashed_password)
|
|
|
|
|
|
def get_password_hash(password):
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
def create_token(data: dict):
|
|
to_encode = data.copy()
|
|
expires_delta = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
expire = datetime.now() + expires_delta
|
|
to_encode.update({"exp": expire})
|
|
encode_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=AlGORITHM)
|
|
return encode_jwt
|
|
|
|
|
|
def decode_token(token):
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[AlGORITHM])
|
|
return payload
|