Skip to content

fix: Remove error handlers from multitenancy recipe #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions supertokens_python/recipe/multitenancy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@

from ...recipe_module import RecipeModule
from .interfaces import TypeGetAllowedDomainsForTenantId
from .utils import InputErrorHandlers, InputOverrideConfig
from .utils import InputOverrideConfig


def init(
get_allowed_domains_for_tenant_id: Union[
TypeGetAllowedDomainsForTenantId, None
] = None,
error_handlers: Union[InputErrorHandlers, None] = None,
override: Union[InputOverrideConfig, None] = None,
) -> Callable[[AppInfo], RecipeModule]:
return recipe.MultitenancyRecipe.init(
get_allowed_domains_for_tenant_id,
error_handlers,
override,
)
6 changes: 1 addition & 5 deletions supertokens_python/recipe/multitenancy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,5 @@
from supertokens_python.exceptions import SuperTokensError


class TenantDoesNotExistError(SuperTokensError):
pass


class RecipeDisabledForTenantError(SuperTokensError):
class MultitenancyError(SuperTokensError):
pass
21 changes: 3 additions & 18 deletions supertokens_python/recipe/multitenancy/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

from .api import handle_login_methods_api
from .constants import LOGIN_METHODS
from .exceptions import TenantDoesNotExistError, RecipeDisabledForTenantError
from .exceptions import MultitenancyError
from .interfaces import (
LoginMethodsGetOkResult,
ThirdPartyProvider,
Expand All @@ -56,7 +56,6 @@
LoginMethodThirdParty,
)
from .utils import (
InputErrorHandlers,
InputOverrideConfig,
validate_and_normalise_user_input,
)
Expand All @@ -73,13 +72,11 @@ def __init__(
get_allowed_domains_for_tenant_id: Optional[
TypeGetAllowedDomainsForTenantId
] = None,
error_handlers: Union[InputErrorHandlers, None] = None,
override: Union[InputOverrideConfig, None] = None,
) -> None:
super().__init__(recipe_id, app_info)
self.config = validate_and_normalise_user_input(
get_allowed_domains_for_tenant_id,
error_handlers,
override,
)

Expand All @@ -105,7 +102,7 @@ def __init__(
)

def is_error_from_this_recipe_based_on_instance(self, err: Exception) -> bool:
return isinstance(err, (TenantDoesNotExistError, RecipeDisabledForTenantError))
return isinstance(err, MultitenancyError)

def get_apis_handled(self) -> List[APIHandled]:
return [
Expand Down Expand Up @@ -144,17 +141,7 @@ async def handle_api_request(
async def handle_error(
self, request: BaseRequest, err: SuperTokensError, response: BaseResponse
) -> BaseResponse:
if isinstance(err, TenantDoesNotExistError):
return await self.config.error_handlers.on_tenant_does_not_exist(
err, request, response
)

if isinstance(err, RecipeDisabledForTenantError):
return await self.config.error_handlers.on_recipe_disabled_for_tenant(
err, request, response
)

raise Exception("should never come here")
raise err

def get_all_cors_headers(self) -> List[str]:
return []
Expand All @@ -164,7 +151,6 @@ def init(
get_allowed_domains_for_tenant_id: Union[
TypeGetAllowedDomainsForTenantId, None
] = None,
error_handlers: Union[InputErrorHandlers, None] = None,
override: Union[InputOverrideConfig, None] = None,
):
def func(app_info: AppInfo):
Expand All @@ -173,7 +159,6 @@ def func(app_info: AppInfo):
MultitenancyRecipe.recipe_id,
app_info,
get_allowed_domains_for_tenant_id,
error_handlers,
override,
)

Expand Down
50 changes: 0 additions & 50 deletions supertokens_python/recipe/multitenancy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from supertokens_python.framework import BaseRequest, BaseResponse
from supertokens_python.utils import (
resolve,
send_non_200_response_with_message,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -62,45 +61,6 @@ async def on_recipe_disabled_for_tenant(
)


class InputErrorHandlers(ErrorHandlers):
def __init__(
self,
on_tenant_does_not_exist: Union[
None,
Callable[
[SuperTokensError, BaseRequest, BaseResponse],
Union[BaseResponse, Awaitable[BaseResponse]],
],
] = None,
on_recipe_disabled_for_tenant: Union[
None,
Callable[
[SuperTokensError, BaseRequest, BaseResponse],
Union[BaseResponse, Awaitable[BaseResponse]],
],
] = None,
):
if on_tenant_does_not_exist is None:
on_tenant_does_not_exist = default_on_tenant_does_not_exist

if on_recipe_disabled_for_tenant is None:
on_recipe_disabled_for_tenant = default_on_recipe_disabled_for_tenant

super().__init__(on_tenant_does_not_exist, on_recipe_disabled_for_tenant)


async def default_on_tenant_does_not_exist(
err: SuperTokensError, _: BaseRequest, response: BaseResponse
):
return send_non_200_response_with_message(str(err), 422, response)


async def default_on_recipe_disabled_for_tenant(
err: SuperTokensError, _: BaseRequest, response: BaseResponse
):
return send_non_200_response_with_message(str(err), 403, response)


class InputOverrideConfig:
def __init__(
self,
Expand All @@ -125,33 +85,23 @@ class MultitenancyConfig:
def __init__(
self,
get_allowed_domains_for_tenant_id: Optional[TypeGetAllowedDomainsForTenantId],
error_handlers: ErrorHandlers,
override: OverrideConfig,
):
self.get_allowed_domains_for_tenant_id = get_allowed_domains_for_tenant_id
self.error_handlers = error_handlers
self.override = override


def validate_and_normalise_user_input(
get_allowed_domains_for_tenant_id: Optional[TypeGetAllowedDomainsForTenantId],
error_handlers: Union[ErrorHandlers, None] = None,
override: Union[InputOverrideConfig, None] = None,
) -> MultitenancyConfig:
if error_handlers is not None and not isinstance(error_handlers, ErrorHandlers): # type: ignore
raise ValueError("error_handlers must be an instance of ErrorHandlers or None")

if override is not None and not isinstance(override, OverrideConfig): # type: ignore
raise ValueError("override must be of type OverrideConfig or None")

if error_handlers is None:
error_handlers = InputErrorHandlers()

if override is None:
override = InputOverrideConfig()

return MultitenancyConfig(
get_allowed_domains_for_tenant_id,
error_handlers,
OverrideConfig(override.functions, override.apis),
)
9 changes: 0 additions & 9 deletions supertokens_python/recipe/thirdparty/api/authorisation_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict
from supertokens_python.recipe.multitenancy.constants import DEFAULT_TENANT_ID
from supertokens_python.recipe.multitenancy.exceptions import (
RecipeDisabledForTenantError,
)


if TYPE_CHECKING:
Expand Down Expand Up @@ -57,11 +53,6 @@ async def handle_authorisation_url_api(
user_context=user_context,
)

if not provider_response.third_party_enabled:
raise RecipeDisabledForTenantError(
f"The third party recipe is disabled for {tenant_id if tenant_id != DEFAULT_TENANT_ID else 'default tenant'}"
)

provider = provider_response.provider
result = await api_implementation.authorisation_url_get(
provider=provider,
Expand Down
9 changes: 0 additions & 9 deletions supertokens_python/recipe/thirdparty/api/signinup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict
from supertokens_python.recipe.multitenancy.constants import DEFAULT_TENANT_ID
from supertokens_python.recipe.multitenancy.exceptions import (
RecipeDisabledForTenantError,
)
from supertokens_python.recipe.thirdparty.provider import RedirectUriInfo

if TYPE_CHECKING:
Expand Down Expand Up @@ -68,11 +64,6 @@ async def handle_sign_in_up_api(
user_context=user_context,
)

if not provider_response.third_party_enabled:
raise RecipeDisabledForTenantError(
f"The third party recipe is disabled for {tenant_id if tenant_id is not None and tenant_id != DEFAULT_TENANT_ID else 'default tenant'}"
)

provider = provider_response.provider

result = await api_implementation.sign_in_up_post(
Expand Down
3 changes: 1 addition & 2 deletions supertokens_python/recipe/thirdparty/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ def __init__(


class GetProviderOkResult:
def __init__(self, provider: Provider, third_party_enabled: bool):
def __init__(self, provider: Provider):
self.provider = provider
self.third_party_enabled = third_party_enabled


class RecipeInterface(ABC):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,4 @@ async def get_provider(

return GetProviderOkResult(
provider,
tenant_config.third_party.enabled,
)