Skip to content

Commit

Permalink
fix: Pass user context everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
KShivendu committed Jul 21, 2023
1 parent 1c8eb52 commit 9cc0daf
Show file tree
Hide file tree
Showing 62 changed files with 348 additions and 220 deletions.
4 changes: 2 additions & 2 deletions supertokens_python/recipe/dashboard/api/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Dict, Any

from httpx import AsyncClient

Expand All @@ -35,7 +35,7 @@


async def handle_analytics_post(
_: APIInterface, api_options: APIOptions
_: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
) -> AnalyticsResponse:
if not Supertokens.get_instance().telemetry:
return AnalyticsResponse()
Expand Down
11 changes: 6 additions & 5 deletions supertokens_python/recipe/dashboard/api/api_key_protector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# under the License.
from __future__ import annotations

from typing import TYPE_CHECKING, Callable, Optional, Awaitable
from typing import TYPE_CHECKING, Callable, Optional, Awaitable, Dict, Any

from supertokens_python.framework import BaseResponse

Expand All @@ -25,7 +25,6 @@
from supertokens_python.types import APIResponse

from supertokens_python.utils import (
default_user_context,
send_200_response,
send_non_200_response_with_message,
)
Expand All @@ -34,9 +33,11 @@
async def api_key_protector(
api_implementation: APIInterface,
api_options: APIOptions,
api_function: Callable[[APIInterface, APIOptions], Awaitable[APIResponse]],
api_function: Callable[
[APIInterface, APIOptions, Dict[str, Any]], Awaitable[APIResponse]
],
user_context: Dict[str, Any],
) -> Optional[BaseResponse]:
user_context = default_user_context(api_options.request)
should_allow_access = await api_options.recipe_implementation.should_allow_access(
api_options.request, api_options.config, user_context
)
Expand All @@ -46,5 +47,5 @@ async def api_key_protector(
"Unauthorised access", 401, api_options.response
)

response = await api_function(api_implementation, api_options)
response = await api_function(api_implementation, api_options, user_context)
return send_200_response(response.to_json(), api_options.response)
9 changes: 4 additions & 5 deletions supertokens_python/recipe/dashboard/api/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# under the License.
from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Optional, Dict, Any

from supertokens_python.framework import BaseResponse

Expand All @@ -23,16 +23,15 @@
APIInterface,
)

from supertokens_python.utils import default_user_context


async def handle_dashboard_api(
api_implementation: APIInterface, api_options: APIOptions
api_implementation: APIInterface,
api_options: APIOptions,
user_context: Dict[str, Any],
) -> Optional[BaseResponse]:
if api_implementation.dashboard_get is None:
return None

user_context = default_user_context(api_options.request)
html_str = await api_implementation.dashboard_get(api_options, user_context)

api_options.response.set_html_content(html_str)
Expand Down
6 changes: 4 additions & 2 deletions supertokens_python/recipe/dashboard/api/search/getTags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# under the License.
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Dict, Any

if TYPE_CHECKING:
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions
Expand All @@ -23,7 +23,9 @@
from supertokens_python.recipe.dashboard.interfaces import SearchTagsOK


async def handle_get_tags(_: APIInterface, __: APIOptions) -> SearchTagsOK:
async def handle_get_tags(
_: APIInterface, __: APIOptions, _user_context: Dict[str, Any]
) -> SearchTagsOK:
response = await Querier.get_instance().send_get_request(
NormalisedURLPath("/user/search/tags")
)
Expand Down
6 changes: 4 additions & 2 deletions supertokens_python/recipe/dashboard/api/signin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# under the License.
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Dict, Any

if TYPE_CHECKING:
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions
Expand All @@ -24,7 +24,9 @@
from supertokens_python.utils import send_200_response


async def handle_emailpassword_signin_api(_: APIInterface, api_options: APIOptions):
async def handle_emailpassword_signin_api(
_: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
):
body = await api_options.request.json()
if body is None:
raise_bad_input_exception("Please send body")
Expand Down
4 changes: 2 additions & 2 deletions supertokens_python/recipe/dashboard/api/signout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# under the License.
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Dict, Any

if TYPE_CHECKING:
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions
Expand All @@ -26,7 +26,7 @@


async def handle_emailpassword_signout_api(
_: APIInterface, api_options: APIOptions
_: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
) -> SignOutOK:
if api_options.config.auth_mode == "api-key":
return SignOutOK()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Any, Dict

from ...interfaces import APIInterface, APIOptions, UserDeleteAPIResponse
from supertokens_python.exceptions import raise_bad_input_exception
from supertokens_python import Supertokens


async def handle_user_delete(
_api_interface: APIInterface, api_options: APIOptions
_api_interface: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
) -> UserDeleteAPIResponse:
user_id = api_options.request.get_query_param("userId")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
FeatureNotEnabledError,
)

from typing import Union
from typing import Union, Dict, Any


async def handle_user_email_verify_get(
_api_interface: APIInterface, api_options: APIOptions
_api_interface: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]
) -> Union[UserEmailVerifyGetAPIResponse, FeatureNotEnabledError]:
req = api_options.request
user_id = req.get_query_param("userId")
Expand All @@ -25,5 +25,5 @@ async def handle_user_email_verify_get(
except Exception:
return FeatureNotEnabledError()

is_verified = await is_email_verified(user_id)
is_verified = await is_email_verified(user_id, user_context=user_context)
return UserEmailVerifyGetAPIResponse(is_verified)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


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

if verified:
token_response = await create_email_verification_token(user_id)
token_response = await create_email_verification_token(
user_id, user_context=user_context
)

if isinstance(
token_response, CreateEmailVerificationTokenEmailAlreadyVerifiedError
):
return UserEmailVerifyPutAPIResponse()

verify_response = await verify_email_using_token(token_response.token)
verify_response = await verify_email_using_token(
token_response.token, user_context=user_context
)

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

else:
await unverify_email(user_id)
await unverify_email(user_id, user_context=user_context)

return UserEmailVerifyPutAPIResponse()
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


async def handle_email_verify_token_post(
_api_interface: APIInterface, api_options: APIOptions
_api_interface: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]
) -> Union[
UserEmailVerifyTokenPostAPIOkResponse,
UserEmailVerifyTokenPostAPIEmailAlreadyVerifiedErrorResponse,
Expand All @@ -42,13 +42,15 @@ async def handle_email_verify_token_post(
)

email_response = await EmailVerificationRecipe.get_instance().get_email_for_user_id(
user_id, {}
user_id, user_context
)

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

email_verification_token = await create_email_verification_token(user_id)
email_verification_token = await create_email_verification_token(
user_id, user_context=user_context
)

if isinstance(
email_verification_token, CreateEmailVerificationTokenEmailAlreadyVerifiedError
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union
from typing import Union, Dict, Any

from supertokens_python.exceptions import raise_bad_input_exception
from supertokens_python.recipe.dashboard.utils import get_user_for_recipe_id
Expand All @@ -16,7 +16,7 @@


async def handle_user_get(
_api_interface: APIInterface, api_options: APIOptions
_api_interface: APIInterface, api_options: APIOptions, _user_context: Dict[str, Any]
) -> Union[
UserGetAPINoUserFoundError,
UserGetAPIOkResponse,
Expand Down Expand Up @@ -51,7 +51,7 @@ async def handle_user_get(

return UserGetAPIOkResponse(recipe_id, user)

user_metadata = await get_user_metadata(user_id)
user_metadata = await get_user_metadata(user_id, user_context=_user_context)
first_name = user_metadata.metadata.get("first_name", "")
last_name = user_metadata.metadata.get("last_name", "")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from supertokens_python.exceptions import raise_bad_input_exception
from supertokens_python.recipe.usermetadata import UserMetadataRecipe
from supertokens_python.recipe.usermetadata.asyncio import get_user_metadata
from typing import Union
from typing import Union, Dict, Any


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

Expand All @@ -23,5 +23,5 @@ async def handle_metadata_get(
except Exception:
return FeatureNotEnabledError()

metadata_response = await get_user_metadata(user_id)
metadata_response = await get_user_metadata(user_id, user_context=user_context)
return UserMetadataGetAPIOkResponse(metadata_response.metadata)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


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

await clear_user_metadata(user_id)
await update_user_metadata(user_id, parsed_data)
await clear_user_metadata(user_id, user_context)
await update_user_metadata(user_id, parsed_data, user_context)

return UserMetadataPutAPIResponse()
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@


async def handle_user_password_put(
_api_interface: APIInterface, api_options: APIOptions
_api_interface: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]
) -> Union[UserPasswordPutAPIResponse, UserPasswordPutAPIInvalidPasswordErrorResponse]:
request_body: Dict[str, Any] = await api_options.request.json() # type: ignore
user_id = request_body.get("userId")
Expand Down Expand Up @@ -72,13 +72,13 @@ async def handle_user_password_put(
async def reset_password(
form_fields: List[NormalisedFormField],
create_reset_password_token: Callable[
[str],
[str, Dict[str, Any]],
Awaitable[
Union[CreateResetPasswordOkResult, CreateResetPasswordWrongUserIdError]
],
],
reset_password_using_token: Callable[
[str, str],
[str, str, Dict[str, Any]],
Awaitable[
Union[
ResetPasswordUsingTokenOkResult,
Expand All @@ -100,15 +100,15 @@ async def reset_password(
password_validation_error
)

password_reset_token = await create_reset_password_token(user_id) # type: ignore # FIXME
password_reset_token = await create_reset_password_token(user_id, user_context)

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

password_reset_response = await reset_password_using_token(
password_reset_token.token, new_password
password_reset_token.token, new_password, user_context
)

if isinstance(
Expand Down
Loading

0 comments on commit 9cc0daf

Please sign in to comment.