Skip to content

Commit

Permalink
fix: cyclic import
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Dec 30, 2024
1 parent f2b94ab commit 571dae6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async def login_get(
return response

resp_after_internal_redirects = await handle_login_internal_redirects(
app_info=options.app_info,
response=response,
cookie=options.request.get_header("cookie") or "",
recipe_implementation=options.recipe_implementation,
Expand Down Expand Up @@ -98,6 +99,7 @@ async def auth_get(
return response

return await handle_login_internal_redirects(
app_info=options.app_info,
response=response,
recipe_implementation=options.recipe_implementation,
cookie=cookie or "",
Expand Down Expand Up @@ -230,6 +232,7 @@ async def end_session_get(
return response

return await handle_logout_internal_redirects(
app_info=options.app_info,
response=response,
session=session,
recipe_implementation=options.recipe_implementation,
Expand All @@ -255,6 +258,7 @@ async def end_session_post(
return response

return await handle_logout_internal_redirects(
app_info=options.app_info,
response=response,
session=session,
recipe_implementation=options.recipe_implementation,
Expand All @@ -280,6 +284,7 @@ async def logout_post(
return response

res = await handle_logout_internal_redirects(
app_info=options.app_info,
response=response,
recipe_implementation=options.recipe_implementation,
session=session,
Expand Down
26 changes: 13 additions & 13 deletions supertokens_python/recipe/oauth2provider/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from urllib.parse import parse_qs, urlparse
import time

from supertokens_python.supertokens import Supertokens
from supertokens_python.recipe.multitenancy.constants import DEFAULT_TENANT_ID
from supertokens_python.recipe.session.interfaces import SessionClaimValidator
from supertokens_python.recipe.session.recipe import SessionRecipe
Expand All @@ -36,6 +35,7 @@
RedirectResponse,
)
from supertokens_python.recipe.session.interfaces import SessionContainer
from supertokens_python.supertokens import AppInfo


async def login_get(
Expand Down Expand Up @@ -218,26 +218,25 @@ def merge_set_cookie_headers(
return set_cookie1 + set_cookie2


def is_login_internal_redirect(redirect_to: str) -> bool:
instance = Supertokens.get_instance()
api_domain = instance.app_info.api_domain.get_as_string_dangerous()
api_base_path = instance.app_info.api_base_path.get_as_string_dangerous()
def is_login_internal_redirect(app_info: AppInfo, redirect_to: str) -> bool:
api_domain = app_info.api_domain.get_as_string_dangerous()
api_base_path = app_info.api_base_path.get_as_string_dangerous()
base_path = f"{api_domain}{api_base_path}"

return any(
redirect_to.startswith(f"{base_path}{path}") for path in [LOGIN_PATH, AUTH_PATH]
)


def is_logout_internal_redirect(redirect_to: str) -> bool:
instance = Supertokens.get_instance()
api_domain = instance.app_info.api_domain.get_as_string_dangerous()
api_base_path = instance.app_info.api_base_path.get_as_string_dangerous()
def is_logout_internal_redirect(app_info: AppInfo, redirect_to: str) -> bool:
api_domain = app_info.api_domain.get_as_string_dangerous()
api_base_path = app_info.api_base_path.get_as_string_dangerous()
base_path = f"{api_domain}{api_base_path}"
return redirect_to.startswith(f"{base_path}{END_SESSION_PATH}")


async def handle_login_internal_redirects(
app_info: AppInfo,
response: RedirectResponse,
recipe_implementation: RecipeInterface,
session: Optional[SessionContainer],
Expand All @@ -247,14 +246,14 @@ async def handle_login_internal_redirects(
) -> Union[RedirectResponse, ErrorOAuth2Response]:
from ..interfaces import RedirectResponse, ErrorOAuth2Response

if not is_login_internal_redirect(response.redirect_to):
if not is_login_internal_redirect(app_info, response.redirect_to):
return response

max_redirects = 10
redirect_count = 0

while redirect_count < max_redirects and is_login_internal_redirect(
response.redirect_to
app_info, response.redirect_to
):
cookie = get_merged_cookies(cookie, response.cookies)

Expand Down Expand Up @@ -314,19 +313,20 @@ async def handle_login_internal_redirects(


async def handle_logout_internal_redirects(
app_info: AppInfo,
response: RedirectResponse,
recipe_implementation: RecipeInterface,
session: Optional[SessionContainer],
user_context: Dict[str, Any],
) -> Union[RedirectResponse, ErrorOAuth2Response]:
if not is_logout_internal_redirect(response.redirect_to):
if not is_logout_internal_redirect(app_info, response.redirect_to):
return response

max_redirects = 10
redirect_count = 0

while redirect_count < max_redirects and is_logout_internal_redirect(
response.redirect_to
app_info, response.redirect_to
):
query_string = (
response.redirect_to.split("?", 1)[1] if "?" in response.redirect_to else ""
Expand Down
3 changes: 3 additions & 0 deletions supertokens_python/recipe/oauth2provider/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

if TYPE_CHECKING:
from supertokens_python.framework import BaseRequest, BaseResponse
from supertokens_python.supertokens import AppInfo
from .utils import OAuth2ProviderConfig


Expand Down Expand Up @@ -1267,12 +1268,14 @@ async def reject_logout_request(
class APIOptions:
def __init__(
self,
app_info: AppInfo,
request: BaseRequest,
response: BaseResponse,
recipe_id: str,
config: OAuth2ProviderConfig,
recipe_implementation: RecipeInterface,
):
self.app_info: AppInfo = app_info
self.request: BaseRequest = request
self.response: BaseResponse = response
self.recipe_id: str = recipe_id
Expand Down
11 changes: 6 additions & 5 deletions supertokens_python/recipe/oauth2provider/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ async def handle_api_request(
user_context: Dict[str, Any],
) -> Union[BaseResponse, None]:
api_options = APIOptions(
request,
response,
self.recipe_id,
self.config,
self.recipe_implementation,
app_info=self.app_info,
request=request,
response=response,
recipe_id=self.recipe_id,
config=self.config,
recipe_implementation=self.recipe_implementation,
)
if request_id == LOGIN_PATH:
return await login(
Expand Down

0 comments on commit 571dae6

Please sign in to comment.