Skip to content

Commit 9cc0daf

Browse files
committed
fix: Pass user context everywhere
1 parent 1c8eb52 commit 9cc0daf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+348
-220
lines changed

supertokens_python/recipe/dashboard/api/analytics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from __future__ import annotations
1616

17-
from typing import TYPE_CHECKING
17+
from typing import TYPE_CHECKING, Dict, Any
1818

1919
from httpx import AsyncClient
2020

@@ -35,7 +35,7 @@
3535

3636

3737
async def handle_analytics_post(
38-
_: APIInterface, api_options: APIOptions
38+
_: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
3939
) -> AnalyticsResponse:
4040
if not Supertokens.get_instance().telemetry:
4141
return AnalyticsResponse()

supertokens_python/recipe/dashboard/api/api_key_protector.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# under the License.
1414
from __future__ import annotations
1515

16-
from typing import TYPE_CHECKING, Callable, Optional, Awaitable
16+
from typing import TYPE_CHECKING, Callable, Optional, Awaitable, Dict, Any
1717

1818
from supertokens_python.framework import BaseResponse
1919

@@ -25,7 +25,6 @@
2525
from supertokens_python.types import APIResponse
2626

2727
from supertokens_python.utils import (
28-
default_user_context,
2928
send_200_response,
3029
send_non_200_response_with_message,
3130
)
@@ -34,9 +33,11 @@
3433
async def api_key_protector(
3534
api_implementation: APIInterface,
3635
api_options: APIOptions,
37-
api_function: Callable[[APIInterface, APIOptions], Awaitable[APIResponse]],
36+
api_function: Callable[
37+
[APIInterface, APIOptions, Dict[str, Any]], Awaitable[APIResponse]
38+
],
39+
user_context: Dict[str, Any],
3840
) -> Optional[BaseResponse]:
39-
user_context = default_user_context(api_options.request)
4041
should_allow_access = await api_options.recipe_implementation.should_allow_access(
4142
api_options.request, api_options.config, user_context
4243
)
@@ -46,5 +47,5 @@ async def api_key_protector(
4647
"Unauthorised access", 401, api_options.response
4748
)
4849

49-
response = await api_function(api_implementation, api_options)
50+
response = await api_function(api_implementation, api_options, user_context)
5051
return send_200_response(response.to_json(), api_options.response)

supertokens_python/recipe/dashboard/api/dashboard.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# under the License.
1414
from __future__ import annotations
1515

16-
from typing import TYPE_CHECKING, Optional
16+
from typing import TYPE_CHECKING, Optional, Dict, Any
1717

1818
from supertokens_python.framework import BaseResponse
1919

@@ -23,16 +23,15 @@
2323
APIInterface,
2424
)
2525

26-
from supertokens_python.utils import default_user_context
27-
2826

2927
async def handle_dashboard_api(
30-
api_implementation: APIInterface, api_options: APIOptions
28+
api_implementation: APIInterface,
29+
api_options: APIOptions,
30+
user_context: Dict[str, Any],
3131
) -> Optional[BaseResponse]:
3232
if api_implementation.dashboard_get is None:
3333
return None
3434

35-
user_context = default_user_context(api_options.request)
3635
html_str = await api_implementation.dashboard_get(api_options, user_context)
3736

3837
api_options.response.set_html_content(html_str)

supertokens_python/recipe/dashboard/api/search/getTags.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# under the License.
1414
from __future__ import annotations
1515

16-
from typing import TYPE_CHECKING
16+
from typing import TYPE_CHECKING, Dict, Any
1717

1818
if TYPE_CHECKING:
1919
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions
@@ -23,7 +23,9 @@
2323
from supertokens_python.recipe.dashboard.interfaces import SearchTagsOK
2424

2525

26-
async def handle_get_tags(_: APIInterface, __: APIOptions) -> SearchTagsOK:
26+
async def handle_get_tags(
27+
_: APIInterface, __: APIOptions, _user_context: Dict[str, Any]
28+
) -> SearchTagsOK:
2729
response = await Querier.get_instance().send_get_request(
2830
NormalisedURLPath("/user/search/tags")
2931
)

supertokens_python/recipe/dashboard/api/signin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# under the License.
1414
from __future__ import annotations
1515

16-
from typing import TYPE_CHECKING
16+
from typing import TYPE_CHECKING, Dict, Any
1717

1818
if TYPE_CHECKING:
1919
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions
@@ -24,7 +24,9 @@
2424
from supertokens_python.utils import send_200_response
2525

2626

27-
async def handle_emailpassword_signin_api(_: APIInterface, api_options: APIOptions):
27+
async def handle_emailpassword_signin_api(
28+
_: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
29+
):
2830
body = await api_options.request.json()
2931
if body is None:
3032
raise_bad_input_exception("Please send body")

supertokens_python/recipe/dashboard/api/signout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# under the License.
1414
from __future__ import annotations
1515

16-
from typing import TYPE_CHECKING
16+
from typing import TYPE_CHECKING, Dict, Any
1717

1818
if TYPE_CHECKING:
1919
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions
@@ -26,7 +26,7 @@
2626

2727

2828
async def handle_emailpassword_signout_api(
29-
_: APIInterface, api_options: APIOptions
29+
_: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
3030
) -> SignOutOK:
3131
if api_options.config.auth_mode == "api-key":
3232
return SignOutOK()

supertokens_python/recipe/dashboard/api/userdetails/user_delete.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from typing import Any, Dict
2+
13
from ...interfaces import APIInterface, APIOptions, UserDeleteAPIResponse
24
from supertokens_python.exceptions import raise_bad_input_exception
35
from supertokens_python import Supertokens
46

57

68
async def handle_user_delete(
7-
_api_interface: APIInterface, api_options: APIOptions
9+
_api_interface: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
810
) -> UserDeleteAPIResponse:
911
user_id = api_options.request.get_query_param("userId")
1012

supertokens_python/recipe/dashboard/api/userdetails/user_email_verify_get.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
FeatureNotEnabledError,
99
)
1010

11-
from typing import Union
11+
from typing import Union, Dict, Any
1212

1313

1414
async def handle_user_email_verify_get(
15-
_api_interface: APIInterface, api_options: APIOptions
15+
_api_interface: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]
1616
) -> Union[UserEmailVerifyGetAPIResponse, FeatureNotEnabledError]:
1717
req = api_options.request
1818
user_id = req.get_query_param("userId")
@@ -25,5 +25,5 @@ async def handle_user_email_verify_get(
2525
except Exception:
2626
return FeatureNotEnabledError()
2727

28-
is_verified = await is_email_verified(user_id)
28+
is_verified = await is_email_verified(user_id, user_context=user_context)
2929
return UserEmailVerifyGetAPIResponse(is_verified)

supertokens_python/recipe/dashboard/api/userdetails/user_email_verify_put.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
async def handle_user_email_verify_put(
22-
_api_interface: APIInterface, api_options: APIOptions
22+
_api_interface: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]
2323
) -> UserEmailVerifyPutAPIResponse:
2424
request_body: Dict[str, Any] = await api_options.request.json() # type: ignore
2525
user_id = request_body.get("userId")
@@ -36,20 +36,24 @@ async def handle_user_email_verify_put(
3636
)
3737

3838
if verified:
39-
token_response = await create_email_verification_token(user_id)
39+
token_response = await create_email_verification_token(
40+
user_id, user_context=user_context
41+
)
4042

4143
if isinstance(
4244
token_response, CreateEmailVerificationTokenEmailAlreadyVerifiedError
4345
):
4446
return UserEmailVerifyPutAPIResponse()
4547

46-
verify_response = await verify_email_using_token(token_response.token)
48+
verify_response = await verify_email_using_token(
49+
token_response.token, user_context=user_context
50+
)
4751

4852
if isinstance(verify_response, VerifyEmailUsingTokenInvalidTokenError):
4953
# This should never happen because we consume the token immediately after creating it
5054
raise Exception("Should not come here")
5155

5256
else:
53-
await unverify_email(user_id)
57+
await unverify_email(user_id, user_context=user_context)
5458

5559
return UserEmailVerifyPutAPIResponse()

supertokens_python/recipe/dashboard/api/userdetails/user_email_verify_token_post.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
async def handle_email_verify_token_post(
31-
_api_interface: APIInterface, api_options: APIOptions
31+
_api_interface: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]
3232
) -> Union[
3333
UserEmailVerifyTokenPostAPIOkResponse,
3434
UserEmailVerifyTokenPostAPIEmailAlreadyVerifiedErrorResponse,
@@ -42,13 +42,15 @@ async def handle_email_verify_token_post(
4242
)
4343

4444
email_response = await EmailVerificationRecipe.get_instance().get_email_for_user_id(
45-
user_id, {}
45+
user_id, user_context
4646
)
4747

4848
if not isinstance(email_response, GetEmailForUserIdOkResult):
4949
raise Exception("Should not come here")
5050

51-
email_verification_token = await create_email_verification_token(user_id)
51+
email_verification_token = await create_email_verification_token(
52+
user_id, user_context=user_context
53+
)
5254

5355
if isinstance(
5456
email_verification_token, CreateEmailVerificationTokenEmailAlreadyVerifiedError

supertokens_python/recipe/dashboard/api/userdetails/user_get.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union
1+
from typing import Union, Dict, Any
22

33
from supertokens_python.exceptions import raise_bad_input_exception
44
from supertokens_python.recipe.dashboard.utils import get_user_for_recipe_id
@@ -16,7 +16,7 @@
1616

1717

1818
async def handle_user_get(
19-
_api_interface: APIInterface, api_options: APIOptions
19+
_api_interface: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
2020
) -> Union[
2121
UserGetAPINoUserFoundError,
2222
UserGetAPIOkResponse,
@@ -51,7 +51,7 @@ async def handle_user_get(
5151

5252
return UserGetAPIOkResponse(recipe_id, user)
5353

54-
user_metadata = await get_user_metadata(user_id)
54+
user_metadata = await get_user_metadata(user_id, user_context=_user_context)
5555
first_name = user_metadata.metadata.get("first_name", "")
5656
last_name = user_metadata.metadata.get("last_name", "")
5757

supertokens_python/recipe/dashboard/api/userdetails/user_metadata_get.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
from supertokens_python.exceptions import raise_bad_input_exception
88
from supertokens_python.recipe.usermetadata import UserMetadataRecipe
99
from supertokens_python.recipe.usermetadata.asyncio import get_user_metadata
10-
from typing import Union
10+
from typing import Union, Dict, Any
1111

1212

1313
async def handle_metadata_get(
14-
_api_interface: APIInterface, api_options: APIOptions
14+
_api_interface: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]
1515
) -> Union[UserMetadataGetAPIOkResponse, FeatureNotEnabledError]:
1616
user_id = api_options.request.get_query_param("userId")
1717

@@ -23,5 +23,5 @@ async def handle_metadata_get(
2323
except Exception:
2424
return FeatureNotEnabledError()
2525

26-
metadata_response = await get_user_metadata(user_id)
26+
metadata_response = await get_user_metadata(user_id, user_context=user_context)
2727
return UserMetadataGetAPIOkResponse(metadata_response.metadata)

supertokens_python/recipe/dashboard/api/userdetails/user_metadata_put.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
async def handle_metadata_put(
15-
_api_interface: APIInterface, api_options: APIOptions
15+
_api_interface: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]
1616
) -> UserMetadataPutAPIResponse:
1717
request_body: Dict[str, Any] = await api_options.request.json() # type: ignore
1818
user_id = request_body.get("userId")
@@ -50,7 +50,7 @@ async def handle_metadata_put(
5050
#
5151
# Removing first ensures that the final data is exactly what the user wanted it to be
5252

53-
await clear_user_metadata(user_id)
54-
await update_user_metadata(user_id, parsed_data)
53+
await clear_user_metadata(user_id, user_context)
54+
await update_user_metadata(user_id, parsed_data, user_context)
5555

5656
return UserMetadataPutAPIResponse()

supertokens_python/recipe/dashboard/api/userdetails/user_password_put.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838

3939
async def handle_user_password_put(
40-
_api_interface: APIInterface, api_options: APIOptions
40+
_api_interface: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]
4141
) -> Union[UserPasswordPutAPIResponse, UserPasswordPutAPIInvalidPasswordErrorResponse]:
4242
request_body: Dict[str, Any] = await api_options.request.json() # type: ignore
4343
user_id = request_body.get("userId")
@@ -72,13 +72,13 @@ async def handle_user_password_put(
7272
async def reset_password(
7373
form_fields: List[NormalisedFormField],
7474
create_reset_password_token: Callable[
75-
[str],
75+
[str, Dict[str, Any]],
7676
Awaitable[
7777
Union[CreateResetPasswordOkResult, CreateResetPasswordWrongUserIdError]
7878
],
7979
],
8080
reset_password_using_token: Callable[
81-
[str, str],
81+
[str, str, Dict[str, Any]],
8282
Awaitable[
8383
Union[
8484
ResetPasswordUsingTokenOkResult,
@@ -100,15 +100,15 @@ async def reset_password(
100100
password_validation_error
101101
)
102102

103-
password_reset_token = await create_reset_password_token(user_id) # type: ignore # FIXME
103+
password_reset_token = await create_reset_password_token(user_id, user_context)
104104

105105
if isinstance(password_reset_token, CreateResetPasswordWrongUserIdError):
106106
# Techincally it can but its an edge case so we assume that it wont
107-
# UNKNOWN_USER_ID_ERROR FIXME
107+
# UNKNOWN_USER_ID_ERROR
108108
raise Exception("Should never come here")
109109

110110
password_reset_response = await reset_password_using_token(
111-
password_reset_token.token, new_password
111+
password_reset_token.token, new_password, user_context
112112
)
113113

114114
if isinstance(

0 commit comments

Comments
 (0)