Skip to content

Commit 458949c

Browse files
Merge pull request #365 from supertokens/refactor/user-context
refactor: Pass default user context for APIs in the framework middleware itself
2 parents 72f299a + 3332b92 commit 458949c

Some content is hidden

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

68 files changed

+421
-239
lines changed

supertokens_python/framework/django/django_middleware.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def middleware(get_response: Any):
2828
from supertokens_python.supertokens import manage_session_post_response
2929

3030
from django.http import HttpRequest
31+
from supertokens_python.utils import default_user_context
3132

3233
if asyncio.iscoroutinefunction(get_response):
3334

@@ -37,8 +38,10 @@ async def __asyncMiddleware(request: HttpRequest):
3738
from django.http import HttpResponse
3839

3940
response = DjangoResponse(HttpResponse())
41+
user_context = default_user_context(custom_request)
42+
4043
try:
41-
result = await st.middleware(custom_request, response)
44+
result = await st.middleware(custom_request, response, user_context)
4245
if result is None:
4346
result = await get_response(request)
4447
result = DjangoResponse(result)

supertokens_python/framework/fastapi/fastapi_middleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
def get_middleware():
2525
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
26+
from supertokens_python.utils import default_user_context
2627

2728
class Middleware(BaseHTTPMiddleware):
2829
def __init__(self, app: FastAPI):
@@ -46,8 +47,9 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):
4647
try:
4748
custom_request = FastApiRequest(request)
4849
response = FastApiResponse(Response())
50+
user_context = default_user_context(custom_request)
4951
result: Union[BaseResponse, None] = await st.middleware(
50-
custom_request, response
52+
custom_request, response, user_context
5153
)
5254
if result is None:
5355
response = await call_next(request)

supertokens_python/framework/flask/flask_middleware.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def set_before_after_request(self):
3434
from supertokens_python.framework.flask.flask_request import FlaskRequest
3535
from supertokens_python.framework.flask.flask_response import FlaskResponse
3636
from supertokens_python.supertokens import manage_session_post_response
37+
from supertokens_python.utils import default_user_context
3738

3839
from flask.wrappers import Response
3940

@@ -50,8 +51,11 @@ def _():
5051

5152
request_ = FlaskRequest(request)
5253
response_ = FlaskResponse(Response())
54+
user_context = default_user_context(request_)
5355

54-
result: Union[BaseResponse, None] = sync(st.middleware(request_, response_))
56+
result: Union[BaseResponse, None] = sync(
57+
st.middleware(request_, response_, user_context)
58+
)
5559

5660
if result is not None:
5761
if isinstance(result, FlaskResponse):

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

0 commit comments

Comments
 (0)