base context for wrappers updated
This commit is contained in:
@@ -8,12 +8,14 @@ from ApiLibrary.common.line_number import get_line_number_for_error
|
||||
from ApiLibrary.date_time_actions.date_functions import DateTimeLocal
|
||||
from ApiLibrary.token.password_module import PasswordModule
|
||||
from ErrorHandlers import HTTPExceptionApi
|
||||
from Schemas.identity.identity import UsersTokens
|
||||
from Schemas.identity.identity import UsersTokens, People
|
||||
from Services.Redis import RedisActions, AccessToken
|
||||
from ApiValidations.Custom.token_objects import (
|
||||
EmployeeTokenObject,
|
||||
OccupantTokenObject,
|
||||
UserType,
|
||||
CompanyToken,
|
||||
OccupantToken,
|
||||
)
|
||||
from Schemas import (
|
||||
Users,
|
||||
@@ -37,6 +39,7 @@ if TYPE_CHECKING:
|
||||
T = TypeVar("T", EmployeeTokenObject, OccupantTokenObject)
|
||||
|
||||
|
||||
|
||||
class TokenService:
|
||||
"""Service class for handling authentication tokens and user sessions."""
|
||||
|
||||
@@ -128,22 +131,57 @@ class TokenService:
|
||||
timezone=user.local_timezone or "GMT+0",
|
||||
lang=user.lang or "tr",
|
||||
).model_dump()
|
||||
cls.set_object_to_redis(user, model_value)
|
||||
return {
|
||||
"user_type": UserType.occupant.name,
|
||||
"available_occupants": occupants_selection_dict,
|
||||
}
|
||||
if access_token := cls.set_object_to_redis(user, model_value):
|
||||
return {
|
||||
"access_token": access_token,
|
||||
"user_type": UserType.occupant.name,
|
||||
"available_occupants": occupants_selection_dict,
|
||||
}
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
lang="en",
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Creating Token failed...",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def set_object_to_redis(cls, user, model: Dict):
|
||||
accessObject = AccessToken(
|
||||
access_object = AccessToken(
|
||||
userUUID=user.uu_id,
|
||||
accessToken=cls._create_access_token(),
|
||||
)
|
||||
return RedisActions.set_json(
|
||||
list_keys=accessObject.to_list(),
|
||||
value=json.dumps(model),
|
||||
expires=Auth.TOKEN_EXPIRE_MINUTES_30.seconds,
|
||||
redis_action = RedisActions.set_json(
|
||||
list_keys=access_object.to_list(),
|
||||
value=model,
|
||||
expires={"seconds": int(Auth.TOKEN_EXPIRE_MINUTES_30.seconds)},
|
||||
)
|
||||
if redis_action.status:
|
||||
return access_object.accessToken
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
lang="en",
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Saving Token failed...",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def update_object_to_redis(cls, access_token: str, user_uu_id: str, model: Dict):
|
||||
access_object = AccessToken(
|
||||
userUUID=user_uu_id,
|
||||
accessToken=access_token,
|
||||
)
|
||||
redis_action = RedisActions.set_json(
|
||||
list_keys=access_object.to_list(),
|
||||
value=model,
|
||||
expires={"seconds": int(Auth.TOKEN_EXPIRE_MINUTES_30.seconds)},
|
||||
)
|
||||
if redis_action.status:
|
||||
return access_object.accessToken
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
lang="en",
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Saving Token failed...",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@@ -194,15 +232,17 @@ class TokenService:
|
||||
"company_address": company_address,
|
||||
}
|
||||
)
|
||||
|
||||
person = People.filter_one(
|
||||
People.id == user.person_id, db=db_session
|
||||
).data
|
||||
model_value = EmployeeTokenObject(
|
||||
domain=domain,
|
||||
user_type=UserType.employee.value,
|
||||
user_uu_id=str(user.uu_id),
|
||||
credentials=user.credentials(),
|
||||
user_id=user.id,
|
||||
person_id=user.person_id,
|
||||
person_uu_id=str(user.person.uu_id),
|
||||
person_id=person.id,
|
||||
person_uu_id=str(person.uu_id),
|
||||
request=dict(request.headers),
|
||||
companies_uu_id_list=companies_uu_id_list,
|
||||
companies_id_list=companies_id_list,
|
||||
@@ -211,8 +251,9 @@ class TokenService:
|
||||
timezone=user.local_timezone or "GMT+0",
|
||||
lang=user.lang or "tr",
|
||||
).model_dump()
|
||||
if cls.set_object_to_redis(user, model_value):
|
||||
if access_token := cls.set_object_to_redis(user, model_value):
|
||||
return {
|
||||
"access_token": access_token,
|
||||
"user_type": UserType.employee.name,
|
||||
"companies_list": companies_list,
|
||||
}
|
||||
@@ -228,7 +269,8 @@ class TokenService:
|
||||
"""Remove all tokens for a user with specific domain."""
|
||||
redis_rows = cls._get_user_tokens(user)
|
||||
for redis_row in redis_rows.all:
|
||||
if redis_row.get("domain") == domain:
|
||||
print('redis_row', redis_row.data)
|
||||
if redis_row.data.get("domain") == domain:
|
||||
RedisActions.delete_key(redis_row.key)
|
||||
|
||||
@classmethod
|
||||
@@ -291,6 +333,36 @@ class TokenService:
|
||||
"user": user.get_dict(),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def update_token_at_redis(
|
||||
cls, request: "Request", add_payload: Union[CompanyToken, OccupantToken]
|
||||
) -> Dict[str, Any]:
|
||||
"""Update token at Redis."""
|
||||
access_token = cls.get_access_token_from_request(request=request)
|
||||
token_object = cls.get_object_via_access_key(access_token=access_token)
|
||||
if isinstance(token_object, EmployeeTokenObject) and isinstance(add_payload, CompanyToken):
|
||||
token_object.selected_company = add_payload
|
||||
cls.update_object_to_redis(
|
||||
access_token=access_token,
|
||||
user_uu_id=token_object.user_uu_id,
|
||||
model=token_object.model_dump()
|
||||
)
|
||||
return token_object.selected_company.model_dump()
|
||||
elif isinstance(token_object, OccupantTokenObject) and isinstance(add_payload, OccupantToken):
|
||||
token_object.selected_occupant = add_payload
|
||||
cls.update_object_to_redis(
|
||||
access_token=access_token,
|
||||
user_uu_id=token_object.user_uu_id,
|
||||
model=token_object.model_dump()
|
||||
)
|
||||
return token_object.selected_occupant.model_dump()
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
lang="en",
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Token not found",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def raise_error_if_request_has_no_token(cls, request: "Request") -> None:
|
||||
"""Validate request has required token headers."""
|
||||
@@ -330,7 +402,6 @@ class TokenService:
|
||||
redis_object["selected_company"] = None
|
||||
if not redis_object.get("selected_occupant"):
|
||||
redis_object["selected_occupant"] = None
|
||||
|
||||
if redis_object.get("user_type") == UserType.employee.value:
|
||||
return EmployeeTokenObject(**redis_object)
|
||||
elif redis_object.get("user_type") == UserType.occupant.value:
|
||||
@@ -348,17 +419,17 @@ class TokenService:
|
||||
"""Get token object using access key."""
|
||||
access_token_obj = AccessToken(accessToken=access_token)
|
||||
redis_response = RedisActions.get_json(list_keys=access_token_obj.to_list())
|
||||
|
||||
if redis_object := redis_response.first.data:
|
||||
access_token_obj.userUUID = redis_object.get("user_uu_id")
|
||||
return cls._process_redis_object(redis_object)
|
||||
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
lang="en",
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Invalid access token",
|
||||
)
|
||||
if not redis_response.status:
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
lang="en",
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Access token token is not found or unable to retrieve",
|
||||
)
|
||||
if redis_object := redis_response.first:
|
||||
redis_object_dict = redis_object.data
|
||||
access_token_obj.userUUID = redis_object_dict.get("user_uu_id")
|
||||
return cls._process_redis_object(redis_object_dict)
|
||||
|
||||
@classmethod
|
||||
def get_object_via_user_uu_id(cls, user_id: str) -> T:
|
||||
|
||||
Reference in New Issue
Block a user