Skip to content

Commit 0e48151

Browse files
committed
fix: updated for tenantId in core
1 parent f4eb3e1 commit 0e48151

File tree

18 files changed

+43
-117
lines changed

18 files changed

+43
-117
lines changed

supertokens_python/recipe/multitenancy/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,18 @@
2626
from supertokens_python.supertokens import AppInfo
2727

2828
from ...recipe_module import RecipeModule
29-
from .interfaces import TypeGetTenantIdForUserId, TypeGetAllowedDomainsForTenantId
29+
from .interfaces import TypeGetAllowedDomainsForTenantId
3030
from .utils import InputErrorHandlers, InputOverrideConfig
3131

3232

3333
def init(
34-
get_tenant_id_for_user_id: Union[TypeGetTenantIdForUserId, None] = None,
3534
get_allowed_domains_for_tenant_id: Union[
3635
TypeGetAllowedDomainsForTenantId, None
3736
] = None,
3837
error_handlers: Union[InputErrorHandlers, None] = None,
3938
override: Union[InputOverrideConfig, None] = None,
4039
) -> Callable[[AppInfo], RecipeModule]:
4140
return recipe.MultitenancyRecipe.init(
42-
get_tenant_id_for_user_id,
4341
get_allowed_domains_for_tenant_id,
4442
error_handlers,
4543
override,

supertokens_python/recipe/multitenancy/interfaces.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,21 +254,7 @@ async def login_methods_get(
254254
pass
255255

256256

257-
class TenantIdOkResult:
258-
def __init__(self, tenant_id: str):
259-
self.tenant_id = tenant_id
260-
261-
262-
class UnknownUserIdError(Exception):
263-
pass
264-
265-
266-
TypeGetTenantIdForUserId = Callable[
267-
[str, Dict[str, Any]],
268-
Awaitable[Union[TenantIdOkResult, UnknownUserIdError]],
269-
]
270-
271257
TypeGetAllowedDomainsForTenantId = Callable[
272-
[str, Dict[str, Any]],
258+
[Union[str, None], Dict[str, Any]],
273259
Awaitable[List[str]],
274260
]

supertokens_python/recipe/multitenancy/recipe.py

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
from .interfaces import (
3030
APIInterface,
3131
APIOptions,
32-
TypeGetTenantIdForUserId,
3332
TypeGetAllowedDomainsForTenantId,
34-
TenantIdOkResult,
35-
UnknownUserIdError,
3633
)
3734

3835
from .recipe_implementation import RecipeImplementation
@@ -78,7 +75,6 @@ def __init__(
7875
self,
7976
recipe_id: str,
8077
app_info: AppInfo,
81-
get_tenant_id_for_user_id: Optional[TypeGetTenantIdForUserId] = None,
8278
get_allowed_domains_for_tenant_id: Optional[
8379
TypeGetAllowedDomainsForTenantId
8480
] = None,
@@ -87,7 +83,6 @@ def __init__(
8783
) -> None:
8884
super().__init__(recipe_id, app_info)
8985
self.config = validate_and_normalise_user_input(
90-
get_tenant_id_for_user_id,
9186
get_allowed_domains_for_tenant_id,
9287
error_handlers,
9388
override,
@@ -109,10 +104,6 @@ def __init__(
109104
else self.config.override.apis(api_implementation)
110105
)
111106

112-
self.get_tenant_id_for_user_id_funcs_from_other_recipes: List[
113-
TypeGetTenantIdForUserId
114-
] = []
115-
116107
self.static_third_party_providers: List[ProviderInput] = []
117108

118109
def is_error_from_this_recipe_based_on_instance(self, err: Exception) -> bool:
@@ -169,7 +160,6 @@ def get_all_cors_headers(self) -> List[str]:
169160

170161
@staticmethod
171162
def init(
172-
get_tenant_id_for_user_id: Union[TypeGetTenantIdForUserId, None] = None,
173163
get_allowed_domains_for_tenant_id: Union[
174164
TypeGetAllowedDomainsForTenantId, None
175165
] = None,
@@ -181,7 +171,6 @@ def func(app_info: AppInfo):
181171
MultitenancyRecipe.__instance = MultitenancyRecipe(
182172
MultitenancyRecipe.recipe_id,
183173
app_info,
184-
get_tenant_id_for_user_id,
185174
get_allowed_domains_for_tenant_id,
186175
error_handlers,
187176
override,
@@ -219,24 +208,6 @@ def reset():
219208
raise_general_exception("calling testing function in non testing env")
220209
MultitenancyRecipe.__instance = None
221210

222-
async def get_tenant_id_for_user_id(
223-
self, user_id: str, user_context: Dict[str, Any]
224-
) -> Union[TenantIdOkResult, UnknownUserIdError]:
225-
if self.config.get_tenant_id_for_user_id is not None:
226-
res = await self.config.get_tenant_id_for_user_id(user_id, user_context)
227-
if not isinstance(res, UnknownUserIdError):
228-
return res
229-
230-
for f in self.get_tenant_id_for_user_id_funcs_from_other_recipes:
231-
res = await f(user_id, user_context)
232-
if not isinstance(res, UnknownUserIdError):
233-
return res
234-
235-
return UnknownUserIdError()
236-
237-
def add_get_tenant_id_for_user_id_func(self, f: TypeGetTenantIdForUserId):
238-
self.get_tenant_id_for_user_id_funcs_from_other_recipes.append(f)
239-
240211

241212
class APIImplementation(APIInterface):
242213
async def login_methods_get(
@@ -286,23 +257,19 @@ class AllowedDomainsClaimClass(PrimitiveArrayClaim[List[str]]):
286257
def __init__(self):
287258
async def fetch_value(user_id: str, user_context: Dict[str, Any]) -> List[str]:
288259
recipe = MultitenancyRecipe.get_instance()
289-
tenant_ids_res = await recipe.get_tenant_id_for_user_id(
290-
user_id, user_context
260+
tenant_id = (
261+
None # TODO fetch value will be passed with tenant_id as well later
291262
)
292263

293-
if isinstance(tenant_ids_res, TenantIdOkResult):
294-
for tenant_id in tenant_ids_res.tenant_id:
295-
if recipe.config.get_allowed_domains_for_tenant_id is None:
296-
return (
297-
[]
298-
) # User did not provide a function to get allowed domains, but is using a validator. So we don't allow any domains by default
299-
300-
domains_res = await recipe.config.get_allowed_domains_for_tenant_id(
301-
tenant_id, user_context
302-
)
303-
return domains_res
264+
if recipe.config.get_allowed_domains_for_tenant_id is None:
265+
return (
266+
[]
267+
) # User did not provide a function to get allowed domains, but is using a validator. So we don't allow any domains by default
304268

305-
raise Exception("UNKNOWN_USER_ID")
269+
domains_res = await recipe.config.get_allowed_domains_for_tenant_id(
270+
tenant_id, user_context
271+
)
272+
return domains_res
306273

307274
super().__init__(
308275
key="st-tenant-domains",

supertokens_python/recipe/multitenancy/utils.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
if TYPE_CHECKING:
2626
from typing import Union
2727
from .interfaces import (
28-
TypeGetTenantIdForUserId,
2928
TypeGetAllowedDomainsForTenantId,
3029
RecipeInterface,
3130
APIInterface,
@@ -125,19 +124,16 @@ def __init__(
125124
class MultitenancyConfig:
126125
def __init__(
127126
self,
128-
get_tenant_id_for_user_id: Union[TypeGetTenantIdForUserId, None],
129127
get_allowed_domains_for_tenant_id: Optional[TypeGetAllowedDomainsForTenantId],
130128
error_handlers: ErrorHandlers,
131129
override: OverrideConfig,
132130
):
133-
self.get_tenant_id_for_user_id = get_tenant_id_for_user_id
134131
self.get_allowed_domains_for_tenant_id = get_allowed_domains_for_tenant_id
135132
self.error_handlers = error_handlers
136133
self.override = override
137134

138135

139136
def validate_and_normalise_user_input(
140-
get_tenant_id_for_user_id: Optional[TypeGetTenantIdForUserId],
141137
get_allowed_domains_for_tenant_id: Optional[TypeGetAllowedDomainsForTenantId],
142138
error_handlers: Union[ErrorHandlers, None] = None,
143139
override: Union[InputOverrideConfig, None] = None,
@@ -155,7 +151,6 @@ def validate_and_normalise_user_input(
155151
override = InputOverrideConfig()
156152

157153
return MultitenancyConfig(
158-
get_tenant_id_for_user_id,
159154
get_allowed_domains_for_tenant_id,
160155
error_handlers,
161156
OverrideConfig(override.functions, override.apis),

supertokens_python/recipe/thirdparty/providers/custom.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
get_actual_client_id_from_development_client_id,
1717
is_using_oauth_development_client_id,
1818
)
19-
from supertokens_python.recipe.multitenancy.constants import DEFAULT_TENANT_ID
2019

2120
from ..types import RawUserInfoFromProvider, UserInfo, UserInfoEmail
2221
from ..provider import (
@@ -191,7 +190,6 @@ def _normalize_input(self):
191190
async def default_generate_fake_email(
192191
third_party_user_id: str, _: Dict[str, Any]
193192
) -> str:
194-
third_party_user_id = third_party_user_id.replace("|", ".")
195193
return f"{third_party_user_id}@{self.input_config.third_party_id}.fakeemail.com"
196194

197195
self.input_config.generate_fake_email = default_generate_fake_email
@@ -361,12 +359,6 @@ async def get_user_info(
361359
self.config, raw_user_info_from_provider
362360
)
363361

364-
if (
365-
self.config.tenant_id is not None
366-
and self.config.tenant_id != DEFAULT_TENANT_ID
367-
):
368-
user_info_result.third_party_user_id += "|" + self.config.tenant_id
369-
370362
return UserInfo(
371363
third_party_user_id=user_info_result.third_party_user_id,
372364
email=user_info_result.email,

supertokens_python/recipe/thirdparty/recipe.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818

1919
from supertokens_python.normalised_url_path import NormalisedURLPath
2020
from supertokens_python.querier import Querier
21-
from supertokens_python.recipe.multitenancy.interfaces import (
22-
TenantIdOkResult,
23-
UnknownUserIdError as MTUnknownUserIdError,
24-
)
2521
from supertokens_python.recipe_module import APIHandled, RecipeModule
2622

2723
from .api.implementation import APIImplementation
@@ -92,9 +88,6 @@ def callback():
9288
mt_recipe = MultitenancyRecipe.get_instance_optional()
9389
if mt_recipe:
9490
mt_recipe.static_third_party_providers = self.providers
95-
mt_recipe.add_get_tenant_id_for_user_id_func(
96-
self.get_tenant_id_for_user_id
97-
)
9891

9992
PostSTInitCallbacks.add_post_init_callback(callback)
10093

@@ -211,14 +204,3 @@ async def get_email_for_user_id(self, user_id: str, user_context: Dict[str, Any]
211204
return GetEmailForUserIdOkResult(user_info.email)
212205

213206
return UnknownUserIdError()
214-
215-
async def get_tenant_id_for_user_id(
216-
self, user_id: str, user_context: Dict[str, Any]
217-
):
218-
user_info = await self.recipe_implementation.get_user_by_id(
219-
user_id, user_context
220-
)
221-
if user_info is None:
222-
return MTUnknownUserIdError()
223-
224-
return TenantIdOkResult(user_info.tenant_ids)

supertokens_python/recipe/thirdparty/recipe_implementation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async def get_user_by_id(
5757
response["user"]["thirdParty"]["userId"],
5858
response["user"]["thirdParty"]["id"],
5959
),
60+
response["user"].get("tenantId"),
6061
)
6162
return None
6263

@@ -79,6 +80,7 @@ async def get_users_by_email(
7980
ThirdPartyInfo(
8081
user["thirdParty"]["userId"], user["thirdParty"]["id"]
8182
),
83+
response["user"].get("tenantId"),
8284
)
8385
)
8486
return users
@@ -105,6 +107,7 @@ async def get_user_by_thirdparty_info(
105107
response["user"]["thirdParty"]["userId"],
106108
response["user"]["thirdParty"]["id"],
107109
),
110+
response["user"].get("tenantId"),
108111
)
109112
return None
110113

@@ -134,6 +137,7 @@ async def sign_in_up(
134137
response["user"]["thirdParty"]["userId"],
135138
response["user"]["thirdParty"]["id"],
136139
),
140+
response["user"].get("tenantId"),
137141
),
138142
response["createdNewUser"],
139143
oauth_tokens,
@@ -164,6 +168,7 @@ async def manually_create_or_update_user(
164168
response["user"]["thirdParty"]["userId"],
165169
response["user"]["thirdParty"]["id"],
166170
),
171+
response["user"].get("tenantId"),
167172
),
168173
response["createdNewUser"],
169174
)

supertokens_python/recipe/thirdparty/types.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,13 @@ def __init__(
3737
email: str,
3838
time_joined: int,
3939
third_party_info: ThirdPartyInfo,
40+
tenant_id: Union[str, None],
4041
):
4142
self.user_id: str = user_id
4243
self.email: str = email
4344
self.time_joined: int = time_joined
4445
self.third_party_info: ThirdPartyInfo = third_party_info
45-
self.tenant_id: Union[str, None] = None
46-
47-
self._update_tenant_ids()
48-
49-
def _update_tenant_ids(self):
50-
if "|" in self.third_party_info.user_id:
51-
self.tenant_id = self.third_party_info.user_id.split("|")[1]
46+
self.tenant_id = tenant_id
5247

5348

5449
class UserInfoEmail:

supertokens_python/recipe/thirdpartyemailpassword/api/thirdparty_api_implementation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ async def sign_in_up_post(
7474
result.user.email,
7575
result.user.time_joined,
7676
result.user.third_party_info,
77+
result.user.tenant_id,
7778
),
7879
result.created_new_user,
7980
result.session,

supertokens_python/recipe/thirdpartyemailpassword/recipeimplementation/third_party_recipe_implementation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ async def get_user_by_id(
4444
email=user.email,
4545
time_joined=user.time_joined,
4646
third_party_info=user.third_party_info,
47+
tenant_id=user.tenant_id,
4748
)
4849

4950
async def get_users_by_email(
@@ -60,6 +61,7 @@ async def get_users_by_email(
6061
email=user.email,
6162
time_joined=user.time_joined,
6263
third_party_info=user.third_party_info,
64+
tenant_id=user.tenant_id,
6365
)
6466
)
6567

@@ -82,6 +84,7 @@ async def get_user_by_thirdparty_info(
8284
email=user.email,
8385
time_joined=user.time_joined,
8486
third_party_info=user.third_party_info,
87+
tenant_id=user.tenant_id,
8588
)
8689

8790
async def sign_in_up(
@@ -111,6 +114,7 @@ async def sign_in_up(
111114
result.user.email,
112115
result.user.time_joined,
113116
result.user.third_party_info,
117+
result.user.tenant_id,
114118
),
115119
result.created_new_user,
116120
oauth_tokens,
@@ -142,6 +146,7 @@ async def manually_create_or_update_user(
142146
result.user.email,
143147
result.user.time_joined,
144148
result.user.third_party_info,
149+
result.user.tenant_id,
145150
),
146151
result.created_new_user,
147152
)

0 commit comments

Comments
 (0)