Skip to content

Commit

Permalink
refactor: Add tenant_id variable in session functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KShivendu committed Jul 13, 2023
1 parent caec759 commit 09b0320
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 93 deletions.
4 changes: 3 additions & 1 deletion supertokens_python/recipe/emailverification/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ class EmailVerificationClaimClass(BooleanClaim):
def __init__(self):
default_max_age_in_sec = 300

async def fetch_value(user_id: str, user_context: Dict[str, Any]) -> bool:
async def fetch_value(
user_id: str, _tenant_id: str, user_context: Dict[str, Any]
) -> bool:
recipe = EmailVerificationRecipe.get_instance()
email_info = await recipe.get_email_for_user_id(user_id, user_context)

Expand Down
65 changes: 0 additions & 65 deletions supertokens_python/recipe/multitenancy/allowed_domains_claim.py

This file was deleted.

48 changes: 25 additions & 23 deletions supertokens_python/recipe/multitenancy/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,41 +259,43 @@ async def login_methods_get(

class AllowedDomainsClaimClass(PrimitiveArrayClaim[List[str]]):
def __init__(self):
async def fetch_value(_user_id: str, user_context: Dict[str, Any]) -> List[str]:
default_max_age_in_sec = 60 * 60 * 24 * 7

async def fetch_value(
_: str, tenant_id: str, user_context: Dict[str, Any]
) -> Optional[List[str]]:
recipe = MultitenancyRecipe.get_instance()
tenant_id = (
None # TODO fetch value will be passed with tenant_id as well later
)

if recipe.config.get_allowed_domains_for_tenant_id is None:
return (
[]
) # User did not provide a function to get allowed domains, but is using a validator. So we don't allow any domains by default
if recipe.get_allowed_domains_for_tenant_id is None:
# User did not provide a function to get allowed domains, but is using a validator. So we don't allow any domains by default
return None

domains_res = await recipe.config.get_allowed_domains_for_tenant_id(
return await recipe.get_allowed_domains_for_tenant_id(
tenant_id, user_context
)
return domains_res

super().__init__(
key="st-tenant-domains",
fetch_value=fetch_value,
default_max_age_in_sec=3600,
)
super().__init__("st-t-dmns", fetch_value, default_max_age_in_sec)

def get_value_from_payload(
self, payload: JSONObject, user_context: Union[Dict[str, Any], None] = None
) -> Union[List[str], None]:
if self.key not in payload:
self, payload: JSONObject, user_context: Optional[Dict[str, Any]] = None
) -> Optional[List[str]]:
_ = user_context

res = payload.get(self.key, {}).get("v")
if res is None:
return []
return super().get_value_from_payload(payload, user_context)
return res

def get_last_refetch_time(
self, payload: JSONObject, user_context: Union[Dict[str, Any], None] = None
) -> Union[int, None]:
if self.key not in payload:
self, payload: JSONObject, user_context: Optional[Dict[str, Any]] = None
) -> Optional[int]:
_ = user_context

res = payload.get(self.key, {}).get("t")
if res is None:
return get_timestamp_ms()
return super().get_last_refetch_time(payload, user_context)

return res


AllowedDomainsClaim = AllowedDomainsClaimClass()
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
self,
key: str,
fetch_value: Callable[
[str, Dict[str, Any]],
[str, str, Dict[str, Any]],
MaybeAwaitable[Optional[bool]],
],
default_max_age_in_sec: Optional[int] = None,
Expand Down
2 changes: 1 addition & 1 deletion supertokens_python/recipe/session/recipe_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ async def fetch_and_set_claim(
return False

access_token_payload_update = await claim.build(
session_info.user_id, user_context
session_info.user_id, tenant_id, user_context
)
return await self.merge_into_access_token_payload(
session_handle, access_token_payload_update, user_context
Expand Down
2 changes: 1 addition & 1 deletion supertokens_python/recipe/session/session_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ async def fetch_and_set_claim(
if user_context is None:
user_context = {}

update = await claim.build(self.get_user_id(), user_context)
update = await claim.build(self.get_user_id(), tenant_id, user_context)
return await self.merge_into_access_token_payload(update, user_context)

async def set_claim_value(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ async def create_new_session_in_request(
final_access_token_payload = {**access_token_payload, "iss": issuer}

for claim in claims_added_by_other_recipes:
update = await claim.build(user_id, user_context)
update = await claim.build(user_id, tenant_id, user_context)
final_access_token_payload = {**final_access_token_payload, **update}

log_debug_message("createNewSession: Access token payload built")
Expand Down

0 comments on commit 09b0320

Please sign in to comment.