Skip to content

Commit 762719b

Browse files
committed
fix: Add tenant_id in config functions
1 parent b30244b commit 762719b

File tree

23 files changed

+214
-335
lines changed

23 files changed

+214
-335
lines changed

supertokens_python/recipe/emailpassword/syncio/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ def update_email_or_password(
3131

3232
return sync(
3333
update_email_or_password(
34-
user_id, email, password, apply_password_policy, tenant_id_for_password_policy, user_context
34+
user_id,
35+
email,
36+
password,
37+
apply_password_policy,
38+
tenant_id_for_password_policy,
39+
user_context,
3540
)
3641
)
3742

supertokens_python/recipe/emailverification/emaildelivery/services/backward_compatibility/__init__.py

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# under the License.
1414

1515
from os import environ
16-
from typing import Any, Awaitable, Callable, Dict, Union
16+
from typing import Any, Dict
1717

1818
from httpx import AsyncClient
1919
from supertokens_python.ingredients.emaildelivery.types import EmailDeliveryInterface
@@ -26,27 +26,25 @@
2626
from supertokens_python.utils import handle_httpx_client_exceptions
2727

2828

29-
def default_create_and_send_custom_email(
30-
app_info: AppInfo,
31-
) -> Callable[[User, str, Dict[str, Any]], Awaitable[None]]:
32-
async def func(user: User, email_verification_url: str, _: Dict[str, Any]):
33-
if ("SUPERTOKENS_ENV" in environ) and (environ["SUPERTOKENS_ENV"] == "testing"):
34-
return
35-
data = {
36-
"email": user.email,
37-
"appName": app_info.app_name,
38-
"emailVerifyURL": email_verification_url,
39-
}
40-
try:
41-
async with AsyncClient() as client:
42-
resp = await client.post("https://api.supertokens.io/0/st/auth/email/verify", json=data, headers={"api-version": "0"}) # type: ignore
43-
resp.raise_for_status()
44-
log_debug_message("Email verification email sent to %s", user.email)
45-
except Exception as e:
46-
log_debug_message("Error sending verification email")
47-
handle_httpx_client_exceptions(e, data)
29+
async def create_and_send_email_using_supertokens_service(
30+
app_info: AppInfo, user: User, email_verification_url: str
31+
) -> None:
32+
if ("SUPERTOKENS_ENV" in environ) and (environ["SUPERTOKENS_ENV"] == "testing"):
33+
return
4834

49-
return func
35+
data = {
36+
"email": user.email,
37+
"appName": app_info.app_name,
38+
"emailVerifyURL": email_verification_url,
39+
}
40+
try:
41+
async with AsyncClient() as client:
42+
resp = await client.post("https://api.supertokens.io/0/st/auth/email/verify", json=data, headers={"api-version": "0"}) # type: ignore
43+
resp.raise_for_status()
44+
log_debug_message("Email verification email sent to %s", user.email)
45+
except Exception as e:
46+
log_debug_message("Error sending verification email")
47+
handle_httpx_client_exceptions(e, data)
5048

5149

5250
class BackwardCompatibilityService(
@@ -55,15 +53,8 @@ class BackwardCompatibilityService(
5553
def __init__(
5654
self,
5755
app_info: AppInfo,
58-
create_and_send_custom_email: Union[
59-
Callable[[User, str, Dict[str, Any]], Awaitable[None]], None
60-
] = None,
6156
) -> None:
62-
self.create_and_send_custom_email = (
63-
default_create_and_send_custom_email(app_info)
64-
if create_and_send_custom_email is None
65-
else create_and_send_custom_email
66-
)
57+
self.app_info = app_info
6758

6859
async def send_email(
6960
self,
@@ -72,8 +63,8 @@ async def send_email(
7263
) -> None:
7364
try:
7465
email_user = User(template_vars.user.id, template_vars.user.email)
75-
await self.create_and_send_custom_email(
76-
email_user, template_vars.email_verify_link, user_context
66+
await create_and_send_email_using_supertokens_service(
67+
self.app_info, email_user, template_vars.email_verify_link
7768
)
7869
except Exception as _:
7970
pass

supertokens_python/recipe/emailverification/recipe.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ def __init__(
9292
mode: MODE_TYPE,
9393
email_delivery: Union[EmailDeliveryConfig[EmailTemplateVars], None] = None,
9494
get_email_for_user_id: Optional[TypeGetEmailForUserIdFunction] = None,
95-
create_and_send_custom_email: Union[
96-
Callable[[User, str, Dict[str, Any]], Awaitable[None]], None
97-
] = None,
9895
override: Union[OverrideConfig, None] = None,
9996
) -> None:
10097
super().__init__(recipe_id, app_info)
@@ -103,7 +100,6 @@ def __init__(
103100
mode,
104101
email_delivery,
105102
get_email_for_user_id,
106-
create_and_send_custom_email,
107103
override,
108104
)
109105

@@ -208,9 +204,6 @@ def init(
208204
mode: MODE_TYPE,
209205
email_delivery: Union[EmailDeliveryConfig[EmailTemplateVars], None] = None,
210206
get_email_for_user_id: Optional[TypeGetEmailForUserIdFunction] = None,
211-
create_and_send_custom_email: Union[
212-
Callable[[User, str, Dict[str, Any]], Awaitable[None]], None
213-
] = None,
214207
override: Union[OverrideConfig, None] = None,
215208
):
216209
def func(app_info: AppInfo):
@@ -223,7 +216,6 @@ def func(app_info: AppInfo):
223216
mode,
224217
email_delivery,
225218
get_email_for_user_id,
226-
create_and_send_custom_email,
227219
override,
228220
)
229221

supertokens_python/recipe/emailverification/utils.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,8 @@ def validate_and_normalise_user_input(
6969
mode: MODE_TYPE,
7070
email_delivery: Union[EmailDeliveryConfig[EmailTemplateVars], None] = None,
7171
get_email_for_user_id: Optional[TypeGetEmailForUserIdFunction] = None,
72-
create_and_send_custom_email: Union[
73-
Callable[[User, str, Dict[str, Any]], Awaitable[None]], None
74-
] = None,
7572
override: Union[OverrideConfig, None] = None,
7673
) -> EmailVerificationConfig:
77-
if create_and_send_custom_email:
78-
deprecated_warn(
79-
"create_and_send_custom_email is deprecated. Please use email delivery config instead"
80-
)
81-
8274
if mode not in ["REQUIRED", "OPTIONAL"]:
8375
raise ValueError(
8476
"Email Verification recipe mode must be one of 'REQUIRED' or 'OPTIONAL'"
@@ -89,9 +81,7 @@ def get_email_delivery_config() -> EmailDeliveryConfigWithService[
8981
]:
9082
email_service = email_delivery.service if email_delivery is not None else None
9183
if email_service is None:
92-
email_service = BackwardCompatibilityService(
93-
app_info, create_and_send_custom_email
94-
)
84+
email_service = BackwardCompatibilityService(app_info)
9585

9686
if email_delivery is not None and email_delivery.override is not None:
9787
override = email_delivery.override

supertokens_python/recipe/passwordless/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def init(
5656
],
5757
override: Union[InputOverrideConfig, None] = None,
5858
get_custom_user_input_code: Union[
59-
Callable[[Dict[str, Any]], Awaitable[str]], None
59+
Callable[[str, Dict[str, Any]], Awaitable[str]], None
6060
] = None,
6161
email_delivery: Union[EmailDeliveryConfig[EmailTemplateVars], None] = None,
6262
sms_delivery: Union[SMSDeliveryConfig[SMSTemplateVars], None] = None,

supertokens_python/recipe/passwordless/api/create_code.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ async def create_code(
7575
):
7676
email = email.strip()
7777
validation_error = (
78-
await api_options.config.contact_config.validate_email_address(email)
78+
await api_options.config.contact_config.validate_email_address(
79+
email, tenant_id
80+
)
7981
)
8082
if validation_error is not None:
8183
api_options.response.set_json_content(
@@ -90,7 +92,9 @@ async def create_code(
9092
)
9193
):
9294
validation_error = (
93-
await api_options.config.contact_config.validate_phone_number(phone_number)
95+
await api_options.config.contact_config.validate_phone_number(
96+
phone_number, tenant_id
97+
)
9498
)
9599
if validation_error is not None:
96100
api_options.response.set_json_content(

supertokens_python/recipe/passwordless/api/implementation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async def create_code_post(
5959
user_input_code = None
6060
if api_options.config.get_custom_user_input_code is not None:
6161
user_input_code = await api_options.config.get_custom_user_input_code(
62-
user_context
62+
tenant_id, user_context
6363
)
6464
response = await api_options.recipe_implementation.create_code(
6565
email, phone_number, user_input_code, tenant_id, user_context
@@ -155,7 +155,7 @@ async def resend_code_post(
155155
user_input_code = None
156156
if api_options.config.get_custom_user_input_code is not None:
157157
user_input_code = await api_options.config.get_custom_user_input_code(
158-
user_context
158+
tenant_id, user_context
159159
)
160160
response = (
161161
await api_options.recipe_implementation.create_new_code_for_device(

supertokens_python/recipe/passwordless/emaildelivery/services/backward_compatibility/__init__.py

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import json
1818
from os import environ
19-
from typing import Any, Awaitable, Callable, Dict, Union
19+
from typing import Any, Dict
2020

2121
from httpx import AsyncClient, HTTPStatusError
2222
from supertokens_python.ingredients.emaildelivery import EmailDeliveryInterface
@@ -28,42 +28,41 @@
2828
from supertokens_python.utils import handle_httpx_client_exceptions
2929

3030

31-
def default_create_and_send_custom_email(
32-
app_info: AppInfo,
33-
) -> Callable[[PasswordlessLoginEmailTemplateVars, Dict[str, Any]], Awaitable[None]]:
34-
async def func(input_: PasswordlessLoginEmailTemplateVars, _: Dict[str, Any]):
35-
if ("SUPERTOKENS_ENV" in environ) and (environ["SUPERTOKENS_ENV"] == "testing"):
36-
return
37-
data = {
38-
"email": input_.email,
39-
"appName": app_info.app_name,
40-
"codeLifetime": input_.code_life_time,
41-
}
42-
if input_.url_with_link_code:
43-
data["urlWithLinkCode"] = input_.url_with_link_code
44-
if input_.user_input_code:
45-
data["userInputCode"] = input_.user_input_code
46-
try:
47-
async with AsyncClient() as client:
48-
resp = await client.post("https://api.supertokens.io/0/st/auth/passwordless/login", json=data, headers={"api-version": "0"}) # type: ignore
49-
resp.raise_for_status()
50-
log_debug_message("Passwordless login email sent to %s", input_.email)
51-
except Exception as e:
52-
log_debug_message("Error sending passwordless login email")
53-
handle_httpx_client_exceptions(e, data)
54-
# If the error is thrown from the API:
55-
if isinstance(e, HTTPStatusError):
56-
body: Dict[str, Any] = e.response.json() # type: ignore
57-
if body.get("err"):
58-
msg = body["err"]
59-
else:
60-
msg = json.dumps(body)
31+
async def create_and_send_email_with_supertokens_service(
32+
app_info: AppInfo, input_: PasswordlessLoginEmailTemplateVars
33+
) -> None:
34+
if ("SUPERTOKENS_ENV" in environ) and (environ["SUPERTOKENS_ENV"] == "testing"):
35+
return
6136

62-
raise Exception(msg)
37+
data = {
38+
"email": input_.email,
39+
"appName": app_info.app_name,
40+
"codeLifetime": input_.code_life_time,
41+
}
42+
if input_.url_with_link_code:
43+
data["urlWithLinkCode"] = input_.url_with_link_code
44+
if input_.user_input_code:
45+
data["userInputCode"] = input_.user_input_code
6346

64-
raise e
47+
try:
48+
async with AsyncClient() as client:
49+
resp = await client.post("https://api.supertokens.io/0/st/auth/passwordless/login", json=data, headers={"api-version": "0"}) # type: ignore
50+
resp.raise_for_status()
51+
log_debug_message("Passwordless login email sent to %s", input_.email)
52+
except Exception as e:
53+
log_debug_message("Error sending passwordless login email")
54+
handle_httpx_client_exceptions(e, data)
55+
# If the error is thrown from the API:
56+
if isinstance(e, HTTPStatusError):
57+
body: Dict[str, Any] = e.response.json() # type: ignore
58+
if body.get("err"):
59+
msg = body["err"]
60+
else:
61+
msg = json.dumps(body)
6562

66-
return func
63+
raise Exception(msg)
64+
65+
raise e
6766

6867

6968
class BackwardCompatibilityService(
@@ -72,24 +71,14 @@ class BackwardCompatibilityService(
7271
def __init__(
7372
self,
7473
app_info: AppInfo,
75-
create_and_send_custom_email: Union[
76-
Callable[
77-
[PasswordlessLoginEmailTemplateVars, Dict[str, Any]], Awaitable[None]
78-
],
79-
None,
80-
] = None,
8174
) -> None:
82-
self.create_and_send_custom_email = (
83-
create_and_send_custom_email
84-
if create_and_send_custom_email is not None
85-
else default_create_and_send_custom_email(app_info)
86-
)
75+
self.app_info = app_info
8776

8877
async def send_email(
8978
self,
9079
template_vars: PasswordlessLoginEmailTemplateVars,
9180
user_context: Dict[str, Any],
9281
) -> None:
93-
await self.create_and_send_custom_email(
94-
template_vars, user_context
82+
await create_and_send_email_with_supertokens_service(
83+
self.app_info, template_vars
9584
) # Note: intentionally not using try-except (unlike other recipes)

supertokens_python/recipe/passwordless/recipe.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __init__(
9090
ingredients: PasswordlessIngredients,
9191
override: Union[OverrideConfig, None] = None,
9292
get_custom_user_input_code: Union[
93-
Callable[[Dict[str, Any]], Awaitable[str]], None
93+
Callable[[str, Dict[str, Any]], Awaitable[str]], None
9494
] = None,
9595
email_delivery: Union[
9696
EmailDeliveryConfig[PasswordlessLoginEmailTemplateVars], None
@@ -243,7 +243,7 @@ def init(
243243
],
244244
override: Union[OverrideConfig, None] = None,
245245
get_custom_user_input_code: Union[
246-
Callable[[Dict[str, Any]], Awaitable[str]], None
246+
Callable[[str, Dict[str, Any]], Awaitable[str]], None
247247
] = None,
248248
email_delivery: Union[
249249
EmailDeliveryConfig[PasswordlessLoginEmailTemplateVars], None
@@ -299,7 +299,9 @@ async def create_magic_link(
299299
) -> str:
300300
user_input_code = None
301301
if self.config.get_custom_user_input_code is not None:
302-
user_input_code = await self.config.get_custom_user_input_code(user_context)
302+
user_input_code = await self.config.get_custom_user_input_code(
303+
tenant_id, user_context
304+
)
303305

304306
code_info = await self.recipe_implementation.create_code(
305307
email=email,

0 commit comments

Comments
 (0)