Skip to content

Commit

Permalink
feat: removed request arg from consume_code functions
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMayankThakur committed Nov 20, 2023
1 parent d34492d commit 9ac3158
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
from typing import Any, Dict, Union, Optional
from supertokens_python import get_request_from_user_context
from supertokens_python.exceptions import raise_general_exception
Expand Down
2 changes: 1 addition & 1 deletion supertokens_python/recipe/emailverification/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ async def generate_email_verify_token_post(
await session.fetch_and_set_claim(EmailVerificationClaim, user_context)

email_verify_link = get_email_verify_link(
api_options.app_info, response.token, api_options.recipe_id, tenant_id
api_options.app_info, response.token, api_options.recipe_id, tenant_id, api_options.request, user_context
)

log_debug_message("Sending email verification email to %s", email_info)
Expand Down
1 change: 0 additions & 1 deletion supertokens_python/recipe/passwordless/api/create_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ async def create_code(
phone_number=phone_number,
tenant_id=tenant_id,
api_options=api_options,
api_options.request,
user_context=user_context,
)
return send_200_response(result.to_json(), api_options.response)
7 changes: 2 additions & 5 deletions supertokens_python/recipe/passwordless/api/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from typing import Any, Dict, Union
from supertokens_python.framework import BaseRequest

from supertokens_python.logger import log_debug_message
from supertokens_python.recipe.passwordless.interfaces import (
Expand Down Expand Up @@ -55,7 +54,6 @@ async def create_code_post(
phone_number: Union[str, None],
tenant_id: str,
api_options: APIOptions,
request: BaseRequest,
user_context: Dict[str, Any],
) -> Union[CreateCodePostOkResult, GeneralErrorResponse]:
user_input_code = None
Expand All @@ -71,7 +69,7 @@ async def create_code_post(
flow_type = api_options.config.flow_type
if flow_type in ("MAGIC_LINK", "USER_INPUT_CODE_AND_MAGIC_LINK"):
magic_link = (
api_options.app_info.get_website_domain(request, user_context).get_as_string_dangerous()
api_options.app_info.get_website_domain(api_options.request, user_context).get_as_string_dangerous()
+ api_options.app_info.website_base_path.get_as_string_dangerous()
+ "/verify"
+ "?rid="
Expand Down Expand Up @@ -134,7 +132,6 @@ async def resend_code_post(
pre_auth_session_id: str,
tenant_id: str,
api_options: APIOptions,
request: BaseRequest,
user_context: Dict[str, Any],
) -> Union[
ResendCodePostOkResult, ResendCodePostRestartFlowError, GeneralErrorResponse
Expand Down Expand Up @@ -183,7 +180,7 @@ async def resend_code_post(
flow_type = api_options.config.flow_type
if flow_type in ("MAGIC_LINK", "USER_INPUT_CODE_AND_MAGIC_LINK"):
magic_link = (
api_options.app_info.get_website_domain(request, user_context).get_as_string_dangerous()
api_options.app_info.get_website_domain(api_options.request, user_context).get_as_string_dangerous()
+ api_options.app_info.website_base_path.get_as_string_dangerous()
+ "/verify"
+ "?rid="
Expand Down
2 changes: 1 addition & 1 deletion supertokens_python/recipe/passwordless/api/resend_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ async def resend_code(
device_id = body["deviceId"]

result = await api_implementation.resend_code_post(
device_id, pre_auth_session_id, tenant_id, api_options, api_options.request,user_context
device_id, pre_auth_session_id, tenant_id, api_options, user_context
)
return send_200_response(result.to_json(), api_options.response)
1 change: 0 additions & 1 deletion supertokens_python/recipe/passwordless/asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
from typing import Any, Dict, List, Union
from supertokens_python import get_request_from_user_context
from supertokens_python.exceptions import raise_general_exception
Expand Down
2 changes: 0 additions & 2 deletions supertokens_python/recipe/passwordless/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ async def create_code_post(
phone_number: Union[str, None],
tenant_id: str,
api_options: APIOptions,
request: BaseRequest,
user_context: Dict[str, Any],
) -> Union[CreateCodePostOkResult, GeneralErrorResponse]:
pass
Expand All @@ -443,7 +442,6 @@ async def resend_code_post(
pre_auth_session_id: str,
tenant_id: str,
api_options: APIOptions,
request: BaseRequest,
user_context: Dict[str, Any],
) -> Union[
ResendCodePostOkResult, ResendCodePostRestartFlowError, GeneralErrorResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from typing import Any, Dict, List, Optional, Union
from supertokens_python import get_request_from_user_context
from supertokens_python.exceptions import raise_general_exception

from supertokens_python.recipe.thirdpartyemailpassword.recipe import (
ThirdPartyEmailPasswordRecipe,
Expand Down Expand Up @@ -206,8 +207,9 @@ async def create_reset_password_link(
recipe_instance.get_app_info(),
token.token,
recipe_instance.get_recipe_id(),
request,
tenant_id,
request,
user_context
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async def create_code_post(
user_context: Dict[str, Any],
) -> Union[CreateCodePostOkResult, GeneralErrorResponse]:
return await self.pless_create_code_post(
email, phone_number, tenant_id, api_options, api_options.request, user_context
email, phone_number, tenant_id, api_options, user_context
)

async def resend_code_post(
Expand All @@ -171,7 +171,7 @@ async def resend_code_post(
ResendCodePostOkResult, ResendCodePostRestartFlowError, GeneralErrorResponse
]:
return await self.pless_resend_code_post(
device_id, pre_auth_session_id, tenant_id, api_options, api_options.request, user_context
device_id, pre_auth_session_id, tenant_id, api_options, user_context
)

async def consume_code_post(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional, Union
from supertokens_python.framework import BaseRequest

from supertokens_python.recipe.session import SessionContainer
from supertokens_python.recipe.thirdparty import interfaces as ThirdPartyInterfaces
Expand Down Expand Up @@ -364,7 +363,6 @@ async def create_code_post(
phone_number: Union[str, None],
tenant_id: str,
api_options: PasswordlessAPIOptions,
request: BaseRequest,
user_context: Dict[str, Any],
) -> Union[CreateCodePostOkResult, GeneralErrorResponse]:
pass
Expand All @@ -376,7 +374,6 @@ async def resend_code_post(
pre_auth_session_id: str,
tenant_id: str,
api_options: PasswordlessAPIOptions,
request: BaseRequest,
user_context: Dict[str, Any],
) -> Union[
ResendCodePostOkResult, ResendCodePostRestartFlowError, GeneralErrorResponse
Expand Down
2 changes: 1 addition & 1 deletion supertokens_python/supertokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ def __init__(
self,
app_name: str,
api_domain: str,
website_domain: Optional[str],
api_gateway_path: str = "",
api_base_path: str = "/auth",
website_base_path: str = "/auth",
website_domain: Optional[str] = None,
origin: Optional[
Union[str, Callable[[BaseRequest, Optional[Dict[str, Any]]], str]]
] = None,
Expand Down
64 changes: 63 additions & 1 deletion tests/emailpassword/test_emailverify.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import asyncio
import base64
import json
from urllib.parse import urlparse
from typing import Any, Dict, Optional, Union

from fastapi import FastAPI
Expand All @@ -24,14 +25,17 @@
from supertokens_python.asyncio import delete_user
from supertokens_python.exceptions import BadInputError
from supertokens_python.framework.fastapi import get_middleware
from supertokens_python.framework.request import BaseRequest
from supertokens_python.querier import Querier
from supertokens_python.recipe import emailpassword, emailverification, session
from supertokens_python.recipe.emailpassword import emaildelivery
from supertokens_python.recipe.emailverification.asyncio import (
create_email_verification_token,
is_email_verified,
revoke_email_verification_tokens,
unverify_email,
verify_email_using_token,
send_email_verification_email
)
from supertokens_python.recipe.emailverification.interfaces import (
APIInterface,
Expand All @@ -51,7 +55,7 @@
refresh_session,
)
from supertokens_python.recipe.session.constants import ANTI_CSRF_HEADER_KEY
from supertokens_python.utils import is_version_gte
from supertokens_python.utils import is_version_gte, set_request_in_user_context_if_not_defined
from tests.utils import (
TEST_ACCESS_TOKEN_MAX_AGE_CONFIG_KEY,
email_verify_token_request,
Expand Down Expand Up @@ -1302,3 +1306,61 @@ async def send_email(
assert res.status_code == 200
assert res.json() == {"status": "OK"}
assert "front-token" not in res.headers


async def test_generate_email_verification_uses_correct_origin(
driver_config_client: TestClient,
):
email_verify_link = None
class CustomEmailService(
emailverification.EmailDeliveryInterface[emailverification.EmailTemplateVars]
):
async def send_email(
self,
template_vars: emailverification.EmailTemplateVars,
user_context: Dict[str, Any],
) -> None:
nonlocal email_verify_link
email_verify_link = template_vars.email_verify_link

def get_origin(req: BaseRequest, user_context: Optional[Dict[str, Any]]) -> str:
set_request_in_user_context_if_not_defined(user_context, req)
return user_context["url"]

init(
supertokens_config=SupertokensConfig("http://localhost:3567"),
app_info=InputAppInfo(
app_name="SuperTokens Demo",
api_domain="http://api.supertokens.io",
website_domain=None,
origin=get_origin,
api_base_path="/auth",
),
framework="fastapi",
recipe_list=[
session.init(),
emailverification.init("OPTIONAL", email_delivery=emailpassword.EmailDeliveryConfig(CustomEmailService())),
emailpassword.init(),
],
)
start_st()

# version = await Querier.get_instance().get_api_version()
# if not is_version_gte(version, "2.9"):
# # If the version less than 2.9, the recipe doesn't exist. So skip the test
# skip()

response_1 = sign_up_request(driver_config_client, "[email protected]", "testPass123")
assert response_1.status_code == 200
dict_response = json.loads(response_1.text)
assert dict_response["status"] == "OK"
user_id = dict_response["user"]["id"]
email = dict_response["user"]["email"]

await send_email_verification_email("public", user_id, email, {"url": "localhost:3000"})
url = urlparse(email_verify_link)
assert url.hostname == "localhost:3000"

await send_email_verification_email("public", user_id, email, {"url": "localhost:3002"})
url = urlparse(email_verify_link)
assert url.hostname == "localhost:3002"

0 comments on commit 9ac3158

Please sign in to comment.