Skip to content

Commit 32d342f

Browse files
committed
refactor: Add tenant_id in functions and update tests accordingly
1 parent 78059b2 commit 32d342f

37 files changed

+121
-51
lines changed

supertokens_python/recipe/emailpassword/api/implementation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ async def sign_in_post(
170170
user = result.user
171171
session = await create_new_session(
172172
api_options.request,
173+
user.tenant_id,
173174
user.user_id,
174175
access_token_payload={},
175176
session_data_in_database={},
@@ -209,6 +210,7 @@ async def sign_up_post(
209210
user = result.user
210211
session = await create_new_session(
211212
api_options.request,
213+
user.tenant_id,
212214
user.user_id,
213215
access_token_payload={},
214216
session_data_in_database={},

supertokens_python/recipe/passwordless/api/implementation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ async def consume_code_post(
289289

290290
session = await create_new_session(
291291
api_options.request,
292+
user.tenant_id,
292293
user.user_id,
293294
{},
294295
{},

supertokens_python/recipe/session/asyncio/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
async def create_new_session(
5050
request: Any,
51+
tenant_id: str,
5152
user_id: str,
5253
access_token_payload: Union[Dict[str, Any], None] = None,
5354
session_data_in_database: Union[Dict[str, Any], None] = None,
@@ -66,6 +67,7 @@ async def create_new_session(
6667

6768
return await create_new_session_in_request(
6869
request,
70+
tenant_id,
6971
user_context,
7072
recipe_instance,
7173
access_token_payload,
@@ -77,6 +79,7 @@ async def create_new_session(
7779

7880

7981
async def create_new_session_without_request_response(
82+
tenant_id: str,
8083
user_id: str,
8184
access_token_payload: Union[Dict[str, Any], None] = None,
8285
session_data_in_database: Union[Dict[str, Any], None] = None,
@@ -106,6 +109,7 @@ async def create_new_session_without_request_response(
106109
final_access_token_payload = {**final_access_token_payload, **update}
107110

108111
return await SessionRecipe.get_instance().recipe_implementation.create_new_session(
112+
tenant_id,
109113
user_id,
110114
final_access_token_payload,
111115
session_data_in_database,

supertokens_python/recipe/session/interfaces.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def __init__(
6969
expiry: int,
7070
custom_claims_in_access_token_payload: Dict[str, Any],
7171
time_created: int,
72+
tenant_id: str,
7273
):
7374
self.session_handle: str = session_handle
7475
self.user_id: str = user_id
@@ -78,6 +79,7 @@ def __init__(
7879
str, Any
7980
] = custom_claims_in_access_token_payload
8081
self.time_created: int = time_created
82+
self.tenant_id: str = tenant_id
8183

8284

8385
class ReqResInfo:
@@ -133,6 +135,7 @@ def __init__(self):
133135
@abstractmethod
134136
async def create_new_session(
135137
self,
138+
tenant_id: str,
136139
user_id: str,
137140
access_token_payload: Optional[Dict[str, Any]],
138141
session_data_in_database: Optional[Dict[str, Any]],
@@ -383,6 +386,7 @@ def __init__(
383386
user_data_in_access_token: Optional[Dict[str, Any]],
384387
req_res_info: Optional[ReqResInfo],
385388
access_token_updated: bool,
389+
tenant_id: str,
386390
):
387391
self.recipe_implementation = recipe_implementation
388392
self.config = config
@@ -395,6 +399,7 @@ def __init__(
395399
self.user_data_in_access_token = user_data_in_access_token
396400
self.req_res_info: Optional[ReqResInfo] = req_res_info
397401
self.access_token_updated = access_token_updated
402+
self.tenant_id = tenant_id
398403

399404
self.response_mutators: List[ResponseMutator] = []
400405

supertokens_python/recipe/session/recipe_implementation.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def __init__(self, querier: Querier, config: SessionConfig, app_info: AppInfo):
6060

6161
async def create_new_session(
6262
self,
63+
tenant_id: str,
6364
user_id: str,
6465
access_token_payload: Optional[Dict[str, Any]],
6566
session_data_in_database: Optional[Dict[str, Any]],
@@ -95,6 +96,7 @@ async def create_new_session(
9596
payload,
9697
None,
9798
True,
99+
tenant_id,
98100
)
99101

100102
return new_session
@@ -262,6 +264,7 @@ async def get_session(
262264
payload,
263265
None,
264266
access_token_updated,
267+
response.session.tenantId,
265268
)
266269

267270
return session
@@ -384,7 +387,7 @@ async def fetch_and_set_claim(
384387
return False
385388

386389
access_token_payload_update = await claim.build(
387-
session_info.user_id, tenant_id, user_context
390+
session_info.user_id, session_info.tenant_id, user_context
388391
)
389392
return await self.merge_into_access_token_payload(
390393
session_handle, access_token_payload_update, user_context

supertokens_python/recipe/session/session_class.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
14-
from typing import Any, Dict, List, TypeVar, Union
14+
from typing import Any, Dict, List, TypeVar, Union, Optional
1515

1616
from supertokens_python.recipe.session.exceptions import (
1717
raise_invalid_claims_exception,
@@ -133,6 +133,9 @@ async def update_session_data_in_database(
133133
def get_user_id(self, user_context: Union[Dict[str, Any], None] = None) -> str:
134134
return self.user_id
135135

136+
def get_tenant_id(self, user_context: Optional[Dict[str, Any]] = None) -> str:
137+
return self.tenant_id
138+
136139
def get_access_token_payload(
137140
self, user_context: Union[Dict[str, Any], None] = None
138141
) -> Dict[str, Any]:
@@ -220,7 +223,9 @@ async def fetch_and_set_claim(
220223
if user_context is None:
221224
user_context = {}
222225

223-
update = await claim.build(self.get_user_id(), tenant_id, user_context)
226+
update = await claim.build(
227+
self.get_user_id(), self.get_tenant_id(), user_context
228+
)
224229
return await self.merge_into_access_token_payload(update, user_context)
225230

226231
async def set_claim_value(

supertokens_python/recipe/session/session_functions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ def __init__(
6666
userId: str,
6767
userDataInJWT: Dict[str, Any],
6868
expiryTime: int,
69+
tenantId: str,
6970
) -> None:
7071
self.handle = handle
7172
self.userId = userId
7273
self.userDataInJWT = userDataInJWT
7374
self.expiryTime = expiryTime
75+
self.tenantId = tenantId
7476

7577

7678
class GetSessionAPIResponseAccessToken:
@@ -254,6 +256,7 @@ async def get_session(
254256
access_token_info["userId"],
255257
access_token_info["userData"],
256258
access_token_info["expiryTime"],
259+
access_token_info["tenantId"],
257260
)
258261
)
259262

@@ -292,6 +295,7 @@ async def get_session(
292295
"expiryTime"
293296
] # if the token didn't pass validation, but we got here, it means it was a v2 token that we didn't have the key cached for.
294297
), # This will throw error if others are none and 'expiryTime' key doesn't exist in the payload
298+
response["session"]["tenantId"],
295299
),
296300
GetSessionAPIResponseAccessToken(
297301
response["accessToken"]["token"],
@@ -455,5 +459,6 @@ async def get_session_information(
455459
response["expiry"],
456460
response["userDataInJWT"],
457461
response["timeCreated"],
462+
response["tenantId"],
458463
)
459464
return None

supertokens_python/recipe/session/session_request_functions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ async def get_session_from_request(
210210

211211
async def create_new_session_in_request(
212212
request: Any,
213+
tenant_id: str,
213214
user_context: Dict[str, Any],
214215
recipe_instance: SessionRecipe,
215216
access_token_payload: Dict[str, Any],
@@ -275,6 +276,7 @@ async def create_new_session_in_request(
275276

276277
disable_anti_csrf = output_transfer_method == "header"
277278
session = await recipe_instance.recipe_implementation.create_new_session(
279+
tenant_id,
278280
user_id,
279281
final_access_token_payload,
280282
session_data_in_database,

supertokens_python/recipe/session/syncio/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
def create_new_session(
4242
request: Any,
43+
tenant_id: str,
4344
user_id: str,
4445
access_token_payload: Union[Dict[str, Any], None] = None,
4546
session_data_in_database: Union[Dict[str, Any], None] = None,
@@ -52,6 +53,7 @@ def create_new_session(
5253
return sync(
5354
async_create_new_session(
5455
request,
56+
tenant_id,
5557
user_id,
5658
access_token_payload,
5759
session_data_in_database,
@@ -61,6 +63,7 @@ def create_new_session(
6163

6264

6365
def create_new_session_without_request_response(
66+
tenant_id: str,
6467
user_id: str,
6568
access_token_payload: Union[Dict[str, Any], None] = None,
6669
session_data_in_database: Union[Dict[str, Any], None] = None,
@@ -73,6 +76,7 @@ def create_new_session_without_request_response(
7376

7477
return sync(
7578
async_create_new_session_without_request_response(
79+
tenant_id,
7680
user_id,
7781
access_token_payload,
7882
session_data_in_database,

supertokens_python/recipe/thirdparty/api/implementation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ async def sign_in_up_post(
126126
user = signinup_response.user
127127
session = await create_new_session(
128128
api_options.request,
129+
user.tenant_id,
129130
user.user_id,
130131
user_context=user_context,
131132
)

0 commit comments

Comments
 (0)