Skip to content

Commit eae7e2d

Browse files
committed
cyclic import issue
1 parent e0995f0 commit eae7e2d

File tree

21 files changed

+171
-89
lines changed

21 files changed

+171
-89
lines changed

supertokens_python/auth_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,14 @@ async def get_factors_set_up_for_user():
923923
async def get_mfa_requirements_for_auth():
924924
nonlocal mfa_info_prom
925925
if mfa_info_prom is None:
926+
from .recipe.multifactorauth.multi_factor_auth_claim import (
927+
MultiFactorAuthClaim,
928+
)
929+
926930
mfa_info_prom = await update_and_get_mfa_related_info_in_session(
927-
input_session=session, user_context=user_context
931+
MultiFactorAuthClaim,
932+
input_session=session,
933+
user_context=user_context,
928934
)
929935
return mfa_info_prom.mfa_requirements_for_auth
930936

supertokens_python/recipe/multifactorauth/api/implementation.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,22 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414
from __future__ import annotations
15+
import importlib
1516

16-
from typing import TYPE_CHECKING, Any, Dict, List, Union
17+
from typing import Any, Dict, List, Union, TYPE_CHECKING
1718

1819
from supertokens_python.recipe.session import SessionContainer
1920
from supertokens_python.recipe.multifactorauth.utils import (
2021
update_and_get_mfa_related_info_in_session,
2122
)
2223
from supertokens_python.recipe.multitenancy.asyncio import get_tenant
23-
from ..multi_factor_auth_claim import MultiFactorAuthClaim
2424
from supertokens_python.asyncio import get_user
2525
from supertokens_python.recipe.session.exceptions import (
2626
InvalidClaimsError,
2727
SuperTokensSessionError,
2828
UnauthorisedError,
2929
)
3030

31-
if TYPE_CHECKING:
32-
from supertokens_python.recipe.multifactorauth.interfaces import (
33-
APIInterface,
34-
APIOptions,
35-
)
36-
3731
from supertokens_python.types import GeneralErrorResponse
3832
from ..interfaces import (
3933
APIInterface,
@@ -42,6 +36,11 @@
4236
ResyncSessionAndFetchMFAInfoPUTOkResult,
4337
)
4438

39+
if TYPE_CHECKING:
40+
from ..multi_factor_auth_claim import (
41+
MultiFactorAuthClaimClass as MultiFactorAuthClaimType,
42+
)
43+
4544

4645
class APIImplementation(APIInterface):
4746
async def resync_session_and_fetch_mfa_info_put(
@@ -50,6 +49,11 @@ async def resync_session_and_fetch_mfa_info_put(
5049
session: SessionContainer,
5150
user_context: Dict[str, Any],
5251
) -> Union[ResyncSessionAndFetchMFAInfoPUTOkResult, GeneralErrorResponse]:
52+
53+
mfa = importlib.import_module("supertokens_python.recipe.multifactorauth")
54+
55+
MultiFactorAuthClaim: MultiFactorAuthClaimType = mfa.MultiFactorAuthClaim
56+
5357
session_user = await get_user(session.get_user_id(), user_context)
5458

5559
if session_user is None:
@@ -58,6 +62,7 @@ async def resync_session_and_fetch_mfa_info_put(
5862
)
5963

6064
mfa_info = await update_and_get_mfa_related_info_in_session(
65+
MultiFactorAuthClaim,
6166
input_session=session,
6267
user_context=user_context,
6368
)
@@ -144,7 +149,7 @@ async def get_mfa_requirements_for_auth():
144149
)
145150
return ResyncSessionAndFetchMFAInfoPUTOkResult(
146151
factors=NextFactors(
147-
next=next_factors,
152+
next_=next_factors,
148153
already_setup=factors_setup_for_user,
149154
allowed_to_setup=factors_allowed_to_setup,
150155
),

supertokens_python/recipe/multifactorauth/api/resync_session_and_fetch_mfa_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
async def handle_resync_session_and_fetch_mfa_info_api(
30-
tenant_id: str,
30+
_tenant_id: str,
3131
api_implementation: APIInterface,
3232
api_options: APIOptions,
3333
user_context: Dict[str, Any],

supertokens_python/recipe/multifactorauth/asyncio/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from ..types import (
2222
MFARequirementList,
2323
)
24-
from ..recipe import MultiFactorAuthRecipe
2524
from ..utils import update_and_get_mfa_related_info_in_session
2625
from supertokens_python.recipe.accountlinking.asyncio import get_user
2726

@@ -34,13 +33,17 @@ async def assert_allowed_to_setup_factor_else_throw_invalid_claim_error(
3433
if user_context is None:
3534
user_context = {}
3635

36+
from ..multi_factor_auth_claim import MultiFactorAuthClaim
37+
3738
mfa_info = await update_and_get_mfa_related_info_in_session(
39+
MultiFactorAuthClaim,
3840
input_session=session,
3941
user_context=user_context,
4042
)
4143
factors_set_up_for_user = await get_factors_setup_for_user(
4244
session.get_user_id(), user_context
4345
)
46+
from ..recipe import MultiFactorAuthRecipe
4447

4548
recipe = MultiFactorAuthRecipe.get_instance_or_throw_error()
4649

@@ -66,7 +69,10 @@ async def get_mfa_requirements_for_auth(
6669
if user_context is None:
6770
user_context = {}
6871

72+
from ..multi_factor_auth_claim import MultiFactorAuthClaim
73+
6974
mfa_info = await update_and_get_mfa_related_info_in_session(
75+
MultiFactorAuthClaim,
7076
input_session=session,
7177
user_context=user_context,
7278
)
@@ -81,6 +87,7 @@ async def mark_factor_as_complete_in_session(
8187
) -> None:
8288
if user_context is None:
8389
user_context = {}
90+
from ..recipe import MultiFactorAuthRecipe
8491

8592
recipe = MultiFactorAuthRecipe.get_instance_or_throw_error()
8693
await recipe.recipe_implementation.mark_factor_as_complete_in_session(
@@ -100,6 +107,7 @@ async def get_factors_setup_for_user(
100107
user = await get_user(user_id, user_context)
101108
if user is None:
102109
raise Exception("Unknown user id")
110+
from ..recipe import MultiFactorAuthRecipe
103111

104112
recipe = MultiFactorAuthRecipe.get_instance_or_throw_error()
105113
return await recipe.recipe_implementation.get_factors_setup_for_user(
@@ -114,6 +122,7 @@ async def get_required_secondary_factors_for_user(
114122
) -> List[str]:
115123
if user_context is None:
116124
user_context = {}
125+
from ..recipe import MultiFactorAuthRecipe
117126

118127
recipe = MultiFactorAuthRecipe.get_instance_or_throw_error()
119128
return await recipe.recipe_implementation.get_required_secondary_factors_for_user(
@@ -129,6 +138,7 @@ async def add_to_required_secondary_factors_for_user(
129138
) -> None:
130139
if user_context is None:
131140
user_context = {}
141+
from ..recipe import MultiFactorAuthRecipe
132142

133143
recipe = MultiFactorAuthRecipe.get_instance_or_throw_error()
134144
await recipe.recipe_implementation.add_to_required_secondary_factors_for_user(
@@ -145,13 +155,11 @@ async def remove_from_required_secondary_factors_for_user(
145155
) -> None:
146156
if user_context is None:
147157
user_context = {}
158+
from ..recipe import MultiFactorAuthRecipe
148159

149160
recipe = MultiFactorAuthRecipe.get_instance_or_throw_error()
150161
await recipe.recipe_implementation.remove_from_required_secondary_factors_for_user(
151162
user_id=user_id,
152163
factor_id=factor_id,
153164
user_context=user_context,
154165
)
155-
156-
157-
init = MultiFactorAuthRecipe.init

supertokens_python/recipe/multifactorauth/interfaces.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ async def resync_session_and_fetch_mfa_info_put(
123123

124124
class NextFactors:
125125
def __init__(
126-
self, next: List[str], already_setup: List[str], allowed_to_setup: List[str]
126+
self, next_: List[str], already_setup: List[str], allowed_to_setup: List[str]
127127
):
128-
self.next = next
128+
self.next_ = next_
129129
self.already_setup = already_setup
130130
self.allowed_to_setup = allowed_to_setup
131131

132132
def to_json(self) -> Dict[str, Any]:
133133
return {
134-
"next": self.next,
134+
"next": self.next_,
135135
"alreadySetup": self.already_setup,
136136
"allowedToSetup": self.allowed_to_setup,
137137
}

supertokens_python/recipe/multifactorauth/multi_factor_auth_claim.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved.
2+
#
3+
# This software is licensed under the Apache License, Version 2.0 (the
4+
# "License") as published by the Apache Software Foundation.
5+
#
6+
# You may not use this file except in compliance with the License. You may
7+
# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
115
from __future__ import annotations
216

317
from typing import Any, Dict, Optional, Set
@@ -15,7 +29,6 @@
1529
MFAClaimValue,
1630
MFARequirementList,
1731
)
18-
from .utils import update_and_get_mfa_related_info_in_session
1932

2033

2134
class HasCompletedRequirementListSCV(SessionClaimValidator):
@@ -29,14 +42,10 @@ def __init__(
2942
self.claim: MultiFactorAuthClaimClass = claim
3043
self.requirement_list = requirement_list
3144

32-
async def should_refetch(
45+
def should_refetch(
3346
self, payload: Dict[str, Any], user_context: Dict[str, Any]
3447
) -> bool:
35-
return (
36-
True
37-
if self.claim.key not in payload or not payload[self.claim.key]
38-
else False
39-
)
48+
return bool(self.claim.key not in payload or not payload[self.claim.key])
4049

4150
async def validate(
4251
self, payload: JSONObject, user_context: Dict[str, Any]
@@ -65,7 +74,7 @@ async def validate(
6574

6675
factor_ids = next_set_of_unsatisfied_factors.factor_ids
6776

68-
if next_set_of_unsatisfied_factors.type == "string":
77+
if next_set_of_unsatisfied_factors.type_ == "string":
6978
return ClaimValidationResult(
7079
is_valid=False,
7180
reason={
@@ -74,7 +83,7 @@ async def validate(
7483
},
7584
)
7685

77-
elif next_set_of_unsatisfied_factors.type == "oneOf":
86+
elif next_set_of_unsatisfied_factors.type_ == "oneOf":
7887
return ClaimValidationResult(
7988
is_valid=False,
8089
reason={
@@ -101,15 +110,11 @@ def __init__(
101110
super().__init__(id_)
102111
self.claim = claim
103112

104-
async def should_refetch(
113+
def should_refetch(
105114
self, payload: Dict[str, Any], user_context: Dict[str, Any]
106115
) -> bool:
107116
assert self.claim is not None
108-
return (
109-
True
110-
if self.claim.key not in payload or not payload[self.claim.key]
111-
else False
112-
)
117+
return bool(self.claim.key not in payload or not payload[self.claim.key])
113118

114119
async def validate(
115120
self, payload: JSONObject, user_context: Dict[str, Any]
@@ -161,13 +166,16 @@ def __init__(self, key: Optional[str] = None):
161166
key = key or "st-mfa"
162167

163168
async def fetch_value(
164-
user_id: str,
169+
_user_id: str,
165170
recipe_user_id: RecipeUserId,
166171
tenant_id: str,
167172
current_payload: Dict[str, Any],
168173
user_context: Dict[str, Any],
169174
) -> MFAClaimValue:
175+
from .utils import update_and_get_mfa_related_info_in_session
176+
170177
mfa_info = await update_and_get_mfa_related_info_in_session(
178+
self,
171179
input_session_recipe_user_id=recipe_user_id,
172180
input_tenant_id=tenant_id,
173181
input_access_token_payload=current_payload,
@@ -209,9 +217,11 @@ def get_next_set_of_unsatisfied_factors(
209217
)
210218

211219
if len(next_factors) > 0:
212-
return FactorIdsAndType(factor_ids=list(next_factors), type=factor_type)
220+
return FactorIdsAndType(
221+
factor_ids=list(next_factors), type_=factor_type
222+
)
213223

214-
return FactorIdsAndType(factor_ids=[], type="string")
224+
return FactorIdsAndType(factor_ids=[], type_="string")
215225

216226
def add_to_payload_(
217227
self,

supertokens_python/recipe/multifactorauth/recipe.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@
4747
GetEmailsForFactorOkResult,
4848
GetPhoneNumbersForFactorsOkResult,
4949
)
50-
from .utils import validate_and_normalise_user_input
51-
from .recipe_implementation import RecipeImplementation
52-
from .api.implementation import APIImplementation
5350
from .interfaces import APIOptions
5451

5552

@@ -79,10 +76,13 @@ def __init__(
7976
] = []
8077
self.is_get_mfa_requirements_for_auth_overridden: bool = False
8178

79+
from .utils import validate_and_normalise_user_input
80+
8281
self.config = validate_and_normalise_user_input(
8382
first_factors,
8483
override,
8584
)
85+
from .recipe_implementation import RecipeImplementation
8686

8787
recipe_implementation = RecipeImplementation(
8888
Querier.get_instance(recipe_id), self
@@ -92,6 +92,7 @@ def __init__(
9292
if self.config.override.functions is None
9393
else self.config.override.functions(recipe_implementation)
9494
)
95+
from .api.implementation import APIImplementation
9596

9697
api_implementation = APIImplementation()
9798
self.api_implementation = (

supertokens_python/recipe/multifactorauth/recipe_implementation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def __init__(
5858
self.factor_id = factor_id
5959
self.mfa_requirement_for_auth = mfa_requirement_for_auth
6060

61-
async def should_refetch(
61+
def should_refetch(
6262
self, payload: Dict[str, Any], user_context: Dict[str, Any]
6363
) -> bool:
64-
return True if self.claim.get_value_from_payload(payload) is None else False
64+
return self.claim.get_value_from_payload(payload) is None
6565

6666
async def validate(
6767
self, payload: JSONObject, user_context: Dict[str, Any]
@@ -174,6 +174,7 @@ async def mark_factor_as_complete_in_session(
174174
self, session: SessionContainer, factor_id: str, user_context: Dict[str, Any]
175175
):
176176
await update_and_get_mfa_related_info_in_session(
177+
MultiFactorAuthClaim,
177178
input_session=session,
178179
input_updated_factor_id=factor_id,
179180
user_context=user_context,

supertokens_python/recipe/multifactorauth/syncio/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from ..interfaces import (
2323
MFARequirementList,
2424
)
25-
from ..recipe import MultiFactorAuthRecipe
2625

2726

2827
def assert_allowed_to_setup_factor_else_throw_invalid_claim_error(
@@ -125,6 +124,3 @@ def remove_from_required_secondary_factors_for_user(
125124
)
126125

127126
return sync(async_func(user_id, factor_id, user_context))
128-
129-
130-
init = MultiFactorAuthRecipe.init

supertokens_python/recipe/multifactorauth/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ class FactorIdsAndType:
8585
def __init__(
8686
self,
8787
factor_ids: List[str],
88-
type: Union[Literal["string"], Literal["oneOf"], Literal["allOfInAnyOrder"]],
88+
type_: Union[Literal["string"], Literal["oneOf"], Literal["allOfInAnyOrder"]],
8989
):
9090
self.factor_ids = factor_ids
91-
self.type = type
91+
self.type_ = type_
9292

9393

9494
class GetFactorsSetupForUserFromOtherRecipesFunc:

0 commit comments

Comments
 (0)