Skip to content

Commit

Permalink
fixes all cyclic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Sep 30, 2024
1 parent eae7e2d commit 0cfec6f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
3 changes: 2 additions & 1 deletion supertokens_python/auth_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -54,14 +51,18 @@ 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:
raise UnauthorisedError(
"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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# under the License.

from __future__ import annotations
import importlib

from typing import Any, Dict, Optional, Set

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions supertokens_python/recipe/multifactorauth/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
)
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions supertokens_python/recipe/multitenancy/api/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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":
Expand Down
7 changes: 5 additions & 2 deletions supertokens_python/supertokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0cfec6f

Please sign in to comment.