Skip to content

Commit

Permalink
fixes stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Oct 11, 2024
1 parent 74ddd39 commit 45ff58f
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 25 deletions.
6 changes: 6 additions & 0 deletions supertokens_python/recipe/emailpassword/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def __init__(self, email: str, user_id: str):
self.email = email
self.user_id = user_id

def to_json(self) -> Dict[str, Any]:
return {
"email": self.email,
"userId": self.user_id,
}


class PasswordResetTokenInvalidError(APIResponse):
status: str = "RESET_PASSWORD_INVALID_TOKEN_ERROR"
Expand Down
18 changes: 18 additions & 0 deletions supertokens_python/recipe/emailpassword/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def __init__(
self.recipe_user_id = recipe_user_id
self.email = email

def to_json(self) -> Dict[str, Any]:
return {
"id": self.id,
"recipeUserId": (
self.recipe_user_id.get_as_string()
if self.recipe_user_id is not None
else None
),
"email": self.email,
}


class PasswordResetEmailTemplateVars:
def __init__(
Expand All @@ -87,6 +98,13 @@ def __init__(
self.password_reset_link = password_reset_link
self.tenant_id = tenant_id

def to_json(self) -> Dict[str, Any]:
return {
"user": self.user.to_json(),
"passwordResetLink": self.password_reset_link,
"tenantId": self.tenant_id,
}


# Export:
EmailTemplateVars = PasswordResetEmailTemplateVars
Expand Down
3 changes: 3 additions & 0 deletions supertokens_python/recipe/emailverification/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class VerifyEmailUsingTokenOkResult:
def __init__(self, user: EmailVerificationUser):
self.user = user

def to_json(self) -> Dict[str, Any]:
return {"user": self.user.to_json(), "status": self.status}


class VerifyEmailUsingTokenInvalidTokenError:
pass
Expand Down
8 changes: 7 additions & 1 deletion supertokens_python/recipe/emailverification/types.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 Union
from typing import Any, Dict, Union

from supertokens_python.ingredients.emaildelivery import EmailDeliveryIngredient
from supertokens_python.ingredients.emaildelivery.types import (
Expand All @@ -28,6 +28,12 @@ def __init__(self, recipe_user_id: RecipeUserId, email: str):
self.recipe_user_id = recipe_user_id
self.email = email

def to_json(self) -> Dict[str, Any]:
return {
"recipeUserId": self.recipe_user_id.get_as_string(),
"email": self.email,
}


class VerificationEmailTemplateVarsUser:
def __init__(self, _id: str, recipe_user_id: RecipeUserId, email: str):
Expand Down
7 changes: 4 additions & 3 deletions supertokens_python/recipe/thirdparty/api/signinup.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, Any, Dict
from typing import TYPE_CHECKING, Any, Dict, Optional
from supertokens_python.recipe.thirdparty.interfaces import SignInUpPostOkResult
from supertokens_python.recipe.thirdparty.provider import RedirectUriInfo

Expand Down Expand Up @@ -78,8 +78,9 @@ async def handle_sign_in_up_api(

provider = provider_response

redirect_uri_info_parsed: Optional[RedirectUriInfo] = None
if redirect_uri_info is not None:
redirect_uri_info = RedirectUriInfo(
redirect_uri_info_parsed = RedirectUriInfo(
redirect_uri_on_provider_dashboard=redirect_uri_info.get(
"redirectURIOnProviderDashboard"
),
Expand All @@ -102,7 +103,7 @@ async def handle_sign_in_up_api(

result = await api_implementation.sign_in_up_post(
provider=provider,
redirect_uri_info=redirect_uri_info,
redirect_uri_info=redirect_uri_info_parsed,
oauth_tokens=oauth_tokens,
tenant_id=tenant_id,
api_options=api_options,
Expand Down
16 changes: 16 additions & 0 deletions supertokens_python/recipe/thirdparty/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ def __init__(
self.from_id_token_payload = from_id_token_payload
self.from_user_info_api = from_user_info_api

def to_json(self) -> Dict[str, Any]:
return {
"fromIdTokenPayload": self.from_id_token_payload,
"fromUserInfoApi": self.from_user_info_api,
}


class UserInfoEmail:
def __init__(self, email: str, is_verified: bool):
self.id: str = email
self.is_verified: bool = is_verified

def to_json(self) -> Dict[str, Any]:
return {"id": self.id, "isVerified": self.is_verified}


class UserInfo:
def __init__(
Expand All @@ -63,6 +72,13 @@ def __init__(
raw_user_info_from_provider or RawUserInfoFromProvider({}, {})
)

def to_json(self) -> Dict[str, Any]:
return {
"thirdPartyUserId": self.third_party_user_id,
"email": self.email.to_json() if self.email is not None else None,
"rawUserInfoFromProvider": self.raw_user_info_from_provider.to_json(),
}


class AccessTokenAPI:
def __init__(self, url: str, params: Dict[str, str]):
Expand Down
25 changes: 19 additions & 6 deletions tests/test-server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ def origin_func( # pylint: disable=unused-argument, dangerous-default-value

def toCamelCase(snake_case: str) -> str:
components = snake_case.split("_")
return components[0] + "".join(x.title() for x in components[1:])
res = components[0] + "".join(x.title() for x in components[1:])
# Convert 'post', 'get', or 'put' at the end to uppercase
if res.endswith("Post"):
res = res[:-4] + "POST"
if res.endswith("Get"):
res = res[:-3] + "GET"
if res.endswith("Put"):
res = res[:-3] + "PUT"
return res


def create_override(
Expand All @@ -110,11 +118,16 @@ def create_override(
originalFunction = getattr(implementation, functionName)

async def finalFunction(*args: Any, **kwargs: Any):
override_logging.log_override_event(
name + "." + toCamelCase(functionName),
"CALL",
{"args": args, "kwargs": kwargs},
)
if len(args) > 0:
override_logging.log_override_event(
name + "." + toCamelCase(functionName),
"CALL",
args,
)
else:
override_logging.log_override_event(
name + "." + toCamelCase(functionName), "CALL", kwargs
)
try:
if inspect.iscoroutinefunction(originalFunction):
res = await originalFunction(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion tests/test-server/emailverification.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def f2(): # type: ignore

tenant_id = data.get("tenantId", "public")
token = data["token"]
attempt_account_linking = data.get("attemptAccountLinking", False)
attempt_account_linking = data.get("attemptAccountLinking", True)
user_context = data.get("userContext", {})

response = verify_email_using_token(
Expand Down
42 changes: 41 additions & 1 deletion tests/test-server/override_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@
LinkAccountsOkResult,
)
from supertokens_python.recipe.accountlinking.types import AccountInfoWithRecipeId
from supertokens_python.recipe.emailpassword.types import FormField
from supertokens_python.recipe.emailpassword.types import (
FormField,
PasswordResetEmailTemplateVars,
)
from supertokens_python.recipe.emailpassword.interfaces import (
APIOptions as EmailPasswordAPIOptions,
ConsumePasswordResetTokenOkResult,
CreateResetPasswordOkResult,
GeneratePasswordResetTokenPostOkResult,
PasswordResetPostOkResult,
SignUpOkResult,
SignUpPostOkResult,
UpdateEmailOrPasswordOkResult,
)
from supertokens_python.recipe.emailverification.interfaces import (
CreateEmailVerificationTokenEmailAlreadyVerifiedError,
CreateEmailVerificationTokenOkResult,
GetEmailForUserIdOkResult,
VerifyEmailUsingTokenOkResult,
)
from supertokens_python.recipe.session.interfaces import ClaimsValidationResult
from supertokens_python.recipe.session.session_class import Session
Expand All @@ -23,6 +37,8 @@
from supertokens_python.recipe.passwordless.interfaces import (
APIOptions as PasswordlessAPIOptions,
)
from supertokens_python.recipe.thirdparty.provider import ProviderConfigForClient
from supertokens_python.recipe.thirdparty.types import UserInfo as TPUserInfo
from supertokens_python.types import AccountInfo, RecipeUserId, User

override_logs: List[Dict[str, Any]] = []
Expand Down Expand Up @@ -94,4 +110,28 @@ def transform_logged_data(data: Any, visited: Union[Set[Any], None] = None) -> A
return data.to_json()
if isinstance(data, ClaimsValidationResult):
return data.to_json()
if isinstance(data, ProviderConfigForClient):
return data.to_json()
if isinstance(data, TPUserInfo):
return data.to_json()
if isinstance(data, GeneratePasswordResetTokenPostOkResult):
return data.to_json()
if isinstance(data, CreateEmailVerificationTokenOkResult):
return {"token": data.token, "status": data.status}
if isinstance(data, GetEmailForUserIdOkResult):
return {"email": data.email, "status": "OK"}
if isinstance(data, VerifyEmailUsingTokenOkResult):
return {"status": data.status}
if isinstance(data, CreateResetPasswordOkResult):
return {"token": data.token, "status": "OK"}
if isinstance(data, PasswordResetEmailTemplateVars):
return data.to_json()
if isinstance(data, ConsumePasswordResetTokenOkResult):
return data.to_json()
if isinstance(data, UpdateEmailOrPasswordOkResult):
return {"status": "OK"}
if isinstance(data, CreateEmailVerificationTokenEmailAlreadyVerifiedError):
return {"status": "EMAIL_ALREADY_VERIFIED_ERROR"}
if isinstance(data, PasswordResetPostOkResult):
return {"status": "OK", "user": data.user.to_json(), "email": data.email}
return data
27 changes: 14 additions & 13 deletions tests/test-server/test_functions_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
FormField,
)
from supertokens_python.recipe.session import SessionContainer
from supertokens_python.recipe.thirdparty.provider import RedirectUriInfo
from supertokens_python.recipe.thirdparty.types import (
RawUserInfoFromProvider,
UserInfo,
Expand Down Expand Up @@ -347,13 +348,13 @@ async def func(
def custom_provider(provider: Any):
if "custom-ev" in eval_str:

def exchange_auth_code_for_oauth_tokens1(
redirect_uri_info: Any, # pylint: disable=unused-argument
async def exchange_auth_code_for_oauth_tokens1(
redirect_uri_info: RedirectUriInfo,
user_context: Any, # pylint: disable=unused-argument
) -> Any:
return {}
return redirect_uri_info.redirect_uri_query_params

def get_user_info1(
async def get_user_info1(
oauth_tokens: Any,
user_context: Any, # pylint: disable=unused-argument
): # pylint: disable=unused-argument
Expand All @@ -377,13 +378,13 @@ def get_user_info1(

if "custom-no-ev" in eval_str:

def exchange_auth_code_for_oauth_tokens2(
async def exchange_auth_code_for_oauth_tokens2(
redirect_uri_info: Any, # pylint: disable=unused-argument
user_context: Any, # pylint: disable=unused-argument
) -> Any:
return {}
return redirect_uri_info

def get_user_info2(
async def get_user_info2(
oauth_tokens: Any, user_context: Any
): # pylint: disable=unused-argument
return UserInfo(
Expand All @@ -406,13 +407,13 @@ def get_user_info2(

if "custom2" in eval_str:

def exchange_auth_code_for_oauth_tokens3(
async def exchange_auth_code_for_oauth_tokens3(
redirect_uri_info: Any,
user_context: Any, # pylint: disable=unused-argument
) -> Any:
return redirect_uri_info["redirectURIQueryParams"]

def get_user_info3(
async def get_user_info3(
oauth_tokens: Any, user_context: Any
): # pylint: disable=unused-argument
return UserInfo(
Expand All @@ -435,13 +436,13 @@ def get_user_info3(

if "custom3" in eval_str:

def exchange_auth_code_for_oauth_tokens4(
async def exchange_auth_code_for_oauth_tokens4(
redirect_uri_info: Any,
user_context: Any, # pylint: disable=unused-argument
) -> Any:
return redirect_uri_info["redirectURIQueryParams"]

def get_user_info4(
async def get_user_info4(
oauth_tokens: Any, user_context: Any
): # pylint: disable=unused-argument
return UserInfo(
Expand All @@ -464,13 +465,13 @@ def get_user_info4(

if "custom" in eval_str:

def exchange_auth_code_for_oauth_tokens5(
async def exchange_auth_code_for_oauth_tokens5(
redirect_uri_info: Any,
user_context: Any, # pylint: disable=unused-argument
) -> Any:
return redirect_uri_info

def get_user_info5(
async def get_user_info5(
oauth_tokens: Any, user_context: Any
): # pylint: disable=unused-argument
if oauth_tokens.get("error"):
Expand Down

0 comments on commit 45ff58f

Please sign in to comment.