From 7e052ca95f2f887653257a5e79d8bcc32a4b438c Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Mon, 30 Dec 2024 16:57:29 +0530 Subject: [PATCH] fix: cyclic import --- .../recipe/oauth2provider/api/login.py | 3 +- .../recipe/oauth2provider/api/utils.py | 43 ++++++++++++++++++- .../recipe/oauth2provider/recipe.py | 4 +- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/supertokens_python/recipe/oauth2provider/api/login.py b/supertokens_python/recipe/oauth2provider/api/login.py index 56353782..953a2324 100644 --- a/supertokens_python/recipe/oauth2provider/api/login.py +++ b/supertokens_python/recipe/oauth2provider/api/login.py @@ -23,6 +23,8 @@ from supertokens_python.utils import send_200_response, send_non_200_response from http.cookies import SimpleCookie +from .utils import get_session + if TYPE_CHECKING: from ..interfaces import ( APIOptions, @@ -40,7 +42,6 @@ async def login( FrontendRedirectResponse, ErrorOAuth2Response, ) - from supertokens_python.recipe.session.asyncio import get_session from supertokens_python.recipe.session.exceptions import TryRefreshTokenError if api_implementation.disable_login_get is True: diff --git a/supertokens_python/recipe/oauth2provider/api/utils.py b/supertokens_python/recipe/oauth2provider/api/utils.py index 46471527..ebc56898 100644 --- a/supertokens_python/recipe/oauth2provider/api/utils.py +++ b/supertokens_python/recipe/oauth2provider/api/utils.py @@ -15,12 +15,18 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union from urllib.parse import parse_qs, urlparse import time from supertokens_python 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 +from supertokens_python.recipe.session.session_request_functions import ( + get_session_from_request, +) +from supertokens_python.types import MaybeAwaitable from ..constants import LOGIN_PATH, AUTH_PATH, END_SESSION_PATH if TYPE_CHECKING: @@ -340,3 +346,38 @@ async def handle_logout_internal_redirects( redirect_count += 1 return response + + +async def get_session( + request: Any, + session_required: Optional[bool] = None, + anti_csrf_check: Optional[bool] = None, + check_database: Optional[bool] = None, + override_global_claim_validators: Optional[ + Callable[ + [List[SessionClaimValidator], SessionContainer, Dict[str, Any]], + MaybeAwaitable[List[SessionClaimValidator]], + ] + ] = None, + user_context: Union[None, Dict[str, Any]] = None, +) -> Union[SessionContainer, None]: + if user_context is None: + user_context = {} + + if session_required is None: + session_required = True + + recipe_instance = SessionRecipe.get_instance() + recipe_interface_impl = recipe_instance.recipe_implementation + config = recipe_instance.config + + return await get_session_from_request( + request, + config, + recipe_interface_impl, + session_required=session_required, + anti_csrf_check=anti_csrf_check, + check_database=check_database, + override_global_claim_validators=override_global_claim_validators, + user_context=user_context, + ) diff --git a/supertokens_python/recipe/oauth2provider/recipe.py b/supertokens_python/recipe/oauth2provider/recipe.py index edbb0635..3ead1827 100644 --- a/supertokens_python/recipe/oauth2provider/recipe.py +++ b/supertokens_python/recipe/oauth2provider/recipe.py @@ -18,9 +18,6 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union from supertokens_python.exceptions import SuperTokensError, raise_general_exception -from supertokens_python.recipe.oauth2provider.api.introspect_token import ( - introspect_token_post, -) from supertokens_python.recipe.oauth2provider.exceptions import OAuth2ProviderError from supertokens_python.recipe_module import APIHandled, RecipeModule from supertokens_python.types import User @@ -57,6 +54,7 @@ revoke_token_post, token_post, user_info_get, + introspect_token_post, ) from .constants import ( LOGIN_PATH,