Access tokens 0.1.0
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
from jose import JWTError, jwt
|
||||
import bcrypt
|
||||
from src.errors.http_errors.errors import Errors
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from src.models.configs_read.env import env_settings
|
||||
'''Hash/Check hash'''
|
||||
class Hashes:
|
||||
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def plain_to_hash(self, plain_password:str)->str:
|
||||
return bcrypt.hashpw(plain_password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
|
||||
|
||||
def verify_password(self, plain_password:str, hashed_password:str)->bool:
|
||||
return bcrypt.checkpw(plain_password.encode("utf-8"), hashed_password.encode("utf-8"))
|
||||
|
||||
|
||||
'''jwt'''
|
||||
class Jwt:
|
||||
def __init__(self) -> None:
|
||||
self.error=Errors()
|
||||
|
||||
def create_token(self, data:dict)->str:
|
||||
|
||||
user_info=data.copy()
|
||||
user_info.update({"exp": datetime.now(timezone.utc)+timedelta(minutes=env_settings.ACCESS_TOKEN_EXPIRE_MINUTES)})
|
||||
|
||||
return jwt.encode(user_info, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
||||
|
||||
|
||||
def jwt_decode(self, token:str)->dict:
|
||||
|
||||
try:
|
||||
|
||||
payload=jwt.decode(token, env_settings.SECRET_KEY, algorithms=[env_settings.ALGORITHM])
|
||||
|
||||
if (payload.get("sub")) is None:
|
||||
raise self.error.credentials_error(detail="Sub block is missing")
|
||||
|
||||
except JWTError as e:
|
||||
|
||||
raise self.error.credentials_error(detail="JWTerror") from e
|
||||
|
||||
return payload
|
||||
|
||||
Reference in New Issue
Block a user