From 0cfec6f26a73c71e712f10d4c6584bb5eabc6ac4 Mon Sep 17 00:00:00 2001 From: rishabhpoddar Date: Mon, 30 Sep 2024 16:13:13 +0530 Subject: [PATCH] fixes all cyclic imports --- supertokens_python/auth_utils.py | 3 ++- .../recipe/multifactorauth/api/implementation.py | 9 +++++---- .../recipe/multifactorauth/multi_factor_auth_claim.py | 7 +++++-- supertokens_python/recipe/multifactorauth/recipe.py | 10 +++++++--- .../recipe/multifactorauth/recipe_implementation.py | 8 ++++++-- .../recipe/multitenancy/api/implementation.py | 9 +++++++-- supertokens_python/supertokens.py | 7 +++++-- 7 files changed, 37 insertions(+), 16 deletions(-) diff --git a/supertokens_python/auth_utils.py b/supertokens_python/auth_utils.py index ff3315ba..08d77cb4 100644 --- a/supertokens_python/auth_utils.py +++ b/supertokens_python/auth_utils.py @@ -912,12 +912,13 @@ async def filter_out_invalid_second_factors_or_throw_if_all_are_invalid( factors_set_up_for_user_prom: Optional[List[str]] = None mfa_info_prom = None - async def get_factors_set_up_for_user(): + async def get_factors_set_up_for_user() -> List[str]: nonlocal factors_set_up_for_user_prom if factors_set_up_for_user_prom is None: factors_set_up_for_user_prom = await mfa_instance.recipe_implementation.get_factors_setup_for_user( user=session_user, user_context=user_context ) + assert factors_set_up_for_user_prom is not None return factors_set_up_for_user_prom async def get_mfa_requirements_for_auth(): diff --git a/supertokens_python/recipe/multifactorauth/api/implementation.py b/supertokens_python/recipe/multifactorauth/api/implementation.py index d6fe15e9..dea5fe91 100644 --- a/supertokens_python/recipe/multifactorauth/api/implementation.py +++ b/supertokens_python/recipe/multifactorauth/api/implementation.py @@ -17,9 +17,6 @@ from typing import Any, Dict, List, Union, TYPE_CHECKING from supertokens_python.recipe.session import SessionContainer -from supertokens_python.recipe.multifactorauth.utils import ( - update_and_get_mfa_related_info_in_session, -) from supertokens_python.recipe.multitenancy.asyncio import get_tenant from supertokens_python.asyncio import get_user from supertokens_python.recipe.session.exceptions import ( @@ -54,6 +51,10 @@ async def resync_session_and_fetch_mfa_info_put( MultiFactorAuthClaim: MultiFactorAuthClaimType = mfa.MultiFactorAuthClaim + module = importlib.import_module( + "supertokens_python.recipe.multifactorauth.utils" + ) + session_user = await get_user(session.get_user_id(), user_context) if session_user is None: @@ -61,7 +62,7 @@ async def resync_session_and_fetch_mfa_info_put( "Session user not found", ) - mfa_info = await update_and_get_mfa_related_info_in_session( + mfa_info = await module.update_and_get_mfa_related_info_in_session( MultiFactorAuthClaim, input_session=session, user_context=user_context, diff --git a/supertokens_python/recipe/multifactorauth/multi_factor_auth_claim.py b/supertokens_python/recipe/multifactorauth/multi_factor_auth_claim.py index 4b9eeef2..2780bd5f 100644 --- a/supertokens_python/recipe/multifactorauth/multi_factor_auth_claim.py +++ b/supertokens_python/recipe/multifactorauth/multi_factor_auth_claim.py @@ -13,6 +13,7 @@ # under the License. from __future__ import annotations +import importlib from typing import Any, Dict, Optional, Set @@ -172,9 +173,11 @@ async def fetch_value( current_payload: Dict[str, Any], user_context: Dict[str, Any], ) -> MFAClaimValue: - from .utils import update_and_get_mfa_related_info_in_session + module = importlib.import_module( + "supertokens_python.recipe.multifactorauth.utils" + ) - mfa_info = await update_and_get_mfa_related_info_in_session( + mfa_info = await module.update_and_get_mfa_related_info_in_session( self, input_session_recipe_user_id=recipe_user_id, input_tenant_id=tenant_id, diff --git a/supertokens_python/recipe/multifactorauth/recipe.py b/supertokens_python/recipe/multifactorauth/recipe.py index f60c30c3..13982676 100644 --- a/supertokens_python/recipe/multifactorauth/recipe.py +++ b/supertokens_python/recipe/multifactorauth/recipe.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. from __future__ import annotations +import importlib from os import environ from typing import Any, Dict, Optional, List, Union @@ -31,7 +32,6 @@ MultiFactorAuthClaim, ) from supertokens_python.recipe.multitenancy.interfaces import TenantConfig -from supertokens_python.recipe.multitenancy.recipe import MultitenancyRecipe from supertokens_python.recipe.session.recipe import SessionRecipe from supertokens_python.recipe_module import APIHandled, RecipeModule from supertokens_python.supertokens import AppInfo @@ -76,9 +76,11 @@ def __init__( ] = [] self.is_get_mfa_requirements_for_auth_overridden: bool = False - from .utils import validate_and_normalise_user_input + module = importlib.import_module( + "supertokens_python.recipe.multifactorauth.utils" + ) - self.config = validate_and_normalise_user_input( + self.config = module.validate_and_normalise_user_input( first_factors, override, ) @@ -102,6 +104,8 @@ def __init__( ) def callback(): + from supertokens_python.recipe.multitenancy.recipe import MultitenancyRecipe + mt_recipe = MultitenancyRecipe.get_instance() mt_recipe.static_first_factors = self.config.first_factors diff --git a/supertokens_python/recipe/multifactorauth/recipe_implementation.py b/supertokens_python/recipe/multifactorauth/recipe_implementation.py index 9ae8f4a9..f2cd77d4 100644 --- a/supertokens_python/recipe/multifactorauth/recipe_implementation.py +++ b/supertokens_python/recipe/multifactorauth/recipe_implementation.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. from __future__ import annotations +import importlib from typing import TYPE_CHECKING, Any, Awaitable, Dict, Set, Callable, List @@ -35,7 +36,6 @@ from supertokens_python.recipe.session import SessionContainer from supertokens_python.types import User -from .utils import update_and_get_mfa_related_info_in_session from .interfaces import RecipeInterface if TYPE_CHECKING: @@ -173,7 +173,11 @@ async def assert_allowed_to_setup_factor_else_throw_invalid_claim_error( async def mark_factor_as_complete_in_session( self, session: SessionContainer, factor_id: str, user_context: Dict[str, Any] ): - await update_and_get_mfa_related_info_in_session( + module = importlib.import_module( + "supertokens_python.recipe.multifactorauth.utils" + ) + + await module.update_and_get_mfa_related_info_in_session( MultiFactorAuthClaim, input_session=session, input_updated_factor_id=factor_id, diff --git a/supertokens_python/recipe/multitenancy/api/implementation.py b/supertokens_python/recipe/multitenancy/api/implementation.py index 27a58624..565773e7 100644 --- a/supertokens_python/recipe/multitenancy/api/implementation.py +++ b/supertokens_python/recipe/multitenancy/api/implementation.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +import importlib from typing import Any, Dict, Optional, Union, List from ..constants import DEFAULT_TENANT_ID @@ -35,7 +36,9 @@ async def login_methods_get( api_options: APIOptions, user_context: Dict[str, Any], ) -> Union[LoginMethodsGetOkResult, GeneralErrorResponse]: - from ...multifactorauth.utils import is_valid_first_factor + module = importlib.import_module( + "supertokens_python.recipe.multifactorauth.utils" + ) from supertokens_python.recipe.thirdparty.providers.config_utils import ( merge_providers_from_core_and_static, find_and_create_provider_instance, @@ -91,7 +94,9 @@ async def login_methods_get( valid_first_factors: List[str] = [] for factor_id in first_factors: - valid_res = await is_valid_first_factor(tenant_id, factor_id, user_context) + valid_res = await module.is_valid_first_factor( + tenant_id, factor_id, user_context + ) if valid_res == "OK": valid_first_factors.append(factor_id) if valid_res == "TENANT_NOT_FOUND_ERROR": diff --git a/supertokens_python/supertokens.py b/supertokens_python/supertokens.py index 6aae8aa9..51ec74b8 100644 --- a/supertokens_python/supertokens.py +++ b/supertokens_python/supertokens.py @@ -13,6 +13,7 @@ # under the License. from __future__ import annotations +import importlib from os import environ from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, Union, Tuple @@ -276,9 +277,11 @@ def make_recipe(recipe: Callable[[AppInfo], RecipeModule]) -> RecipeModule: self.recipe_modules: List[RecipeModule] = list(map(make_recipe, recipe_list)) if not multitenancy_found: - from supertokens_python.recipe.multitenancy.recipe import MultitenancyRecipe + module = importlib.import_module( + "supertokens_python.recipe.multitenancy.recipe" + ) - self.recipe_modules.append(MultitenancyRecipe.init()(self.app_info)) + self.recipe_modules.append(module.init()(self.app_info)) if totp_found and not multi_factor_auth_found: raise Exception("Please initialize the MultiFactorAuth recipe to use TOTP.") if not user_metadata_found: