auth unit tests & ruff visual fix
This commit is contained in:
+12
-10
@@ -1,10 +1,12 @@
|
||||
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
|
||||
from uuid import uuid4
|
||||
import hashlib
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from uuid import uuid4
|
||||
|
||||
import bcrypt
|
||||
from jose import JWTError, jwt
|
||||
|
||||
from src.errors.http_errors.errors import Errors
|
||||
from src.models.configs_read.env import env_settings
|
||||
|
||||
'''Hash/Check hash'''
|
||||
class Hashes:
|
||||
@@ -32,7 +34,7 @@ class Jwt:
|
||||
def create_access_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),
|
||||
user_info.update({"exp": datetime.now(UTC)+timedelta(minutes=env_settings.ACCESS_TOKEN_EXPIRE_MINUTES),
|
||||
"token_type":"access"})
|
||||
return jwt.encode(user_info, env_settings.SECRET_KEY, env_settings.ALGORITHM)
|
||||
|
||||
@@ -41,7 +43,7 @@ class Jwt:
|
||||
|
||||
user_info=data.copy()
|
||||
jti=str(uuid4())
|
||||
user_info.update({"exp":datetime.now(timezone.utc)+timedelta(days=env_settings.REFRESH_TOKEN_EXPIRE_DAYS),
|
||||
user_info.update({"exp":datetime.now(UTC)+timedelta(days=env_settings.REFRESH_TOKEN_EXPIRE_DAYS),
|
||||
"token_type":"refresh",
|
||||
"jti":jti
|
||||
})
|
||||
@@ -53,9 +55,9 @@ class Jwt:
|
||||
def jwt_decode(self, token:str)->dict:
|
||||
|
||||
try:
|
||||
payload=jwt.decode(token, env_settings.SECRET_KEY, algorithms=[env_settings.ALGORITHM])
|
||||
payload=jwt.decode(token, env_settings.SECRET_KEY, algorithms=[env_settings.ALGORITHM], options={"require_exp": True} )
|
||||
|
||||
if (payload.get("sub")) is None:
|
||||
if not (payload.get("sub")) or not (payload.get("token_type")):
|
||||
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
||||
|
||||
except JWTError as e:
|
||||
|
||||
Reference in New Issue
Block a user