Skip to content

Commit 619ac25

Browse files
committed
feat: Changes suggested in feedback
1 parent 63eb59b commit 619ac25

File tree

20 files changed

+70
-89
lines changed

20 files changed

+70
-89
lines changed

supertokens_python/recipe/emailverification/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141

4242
def init(
43-
mode: MODE_TYPE = "OPTIONAL",
43+
mode: MODE_TYPE,
4444
email_delivery: Union[EmailDeliveryConfig[EmailTemplateVars], None] = None,
4545
get_email_for_user_id: Optional[TypeGetEmailForUserIdFunction] = None,
4646
create_and_send_custom_email: Union[

supertokens_python/recipe/emailverification/api/email_verify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def handle_email_verify_api(
5252
)
5353

5454
result = await api_implementation.email_verify_post(
55-
token, api_options, user_context, session
55+
token, api_options, session, user_context
5656
)
5757
else:
5858
if api_implementation.disable_is_email_verified_get:
@@ -65,7 +65,7 @@ async def handle_email_verify_api(
6565
)
6666

6767
result = await api_implementation.is_email_verified_get(
68-
api_options, user_context, session
68+
api_options, session, user_context
6969
)
7070

7171
return send_200_response(result.to_json(), api_options.response)

supertokens_python/recipe/emailverification/api/generate_email_verify_token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ async def handle_generate_email_verify_token_api(
3535
assert session is not None
3636

3737
result = await api_implementation.generate_email_verify_token_post(
38-
api_options, user_context, session
38+
api_options, session, user_context
3939
)
4040
return send_200_response(result.to_json(), api_options.response)

supertokens_python/recipe/emailverification/interfaces.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ def to_json(self) -> Dict[str, Any]:
125125

126126

127127
class EmailVerifyPostInvalidTokenError(APIResponse):
128-
status = "EMAIL_VERIFICATION_INVALID_TOKEN_ERROR"
128+
def __init__(self):
129+
self.status = "EMAIL_VERIFICATION_INVALID_TOKEN_ERROR"
129130

130131
def to_json(self) -> Dict[str, Any]:
131132
return {"status": self.status}
@@ -167,8 +168,8 @@ async def email_verify_post(
167168
self,
168169
token: str,
169170
api_options: APIOptions,
171+
session: Optional[SessionContainer],
170172
user_context: Dict[str, Any],
171-
session: Optional[SessionContainer] = None,
172173
) -> Union[
173174
EmailVerifyPostOkResult, EmailVerifyPostInvalidTokenError, GeneralErrorResponse
174175
]:
@@ -178,17 +179,17 @@ async def email_verify_post(
178179
async def is_email_verified_get(
179180
self,
180181
api_options: APIOptions,
182+
session: Optional[SessionContainer],
181183
user_context: Dict[str, Any],
182-
session: Optional[SessionContainer] = None,
183184
) -> Union[IsEmailVerifiedGetOkResult, GeneralErrorResponse]:
184185
pass
185186

186187
@abstractmethod
187188
async def generate_email_verify_token_post(
188189
self,
189190
api_options: APIOptions,
190-
user_context: Dict[str, Any],
191191
session: SessionContainer,
192+
user_context: Dict[str, Any],
192193
) -> Union[
193194
GenerateEmailVerifyTokenPostOkResult,
194195
GenerateEmailVerifyTokenPostEmailAlreadyVerifiedError,

supertokens_python/recipe/emailverification/recipe.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def get_all_cors_headers(self) -> List[str]:
185185

186186
@staticmethod
187187
def init(
188-
mode: MODE_TYPE = "OPTIONAL",
188+
mode: MODE_TYPE,
189189
email_delivery: Union[EmailDeliveryConfig[EmailTemplateVars], None] = None,
190190
get_email_for_user_id: Optional[TypeGetEmailForUserIdFunction] = None,
191191
create_and_send_custom_email: Union[
@@ -263,7 +263,7 @@ async def get_email_for_user_id(
263263

264264
return UnknownUserIdError()
265265

266-
def add_get_email_for_user_id_func(self, f: Callable[[str, Dict[str, Any]], Any]):
266+
def add_get_email_for_user_id_func(self, f: TypeGetEmailForUserIdFunction):
267267
self.get_email_for_user_id_funcs_from_other_recipes.append(f)
268268

269269

@@ -297,8 +297,8 @@ async def email_verify_post(
297297
self,
298298
token: str,
299299
api_options: APIOptions,
300+
session: Optional[SessionContainer],
300301
user_context: Dict[str, Any],
301-
session: Optional[SessionContainer] = None,
302302
) -> Union[EmailVerifyPostOkResult, EmailVerifyPostInvalidTokenError]:
303303

304304
response = await api_options.recipe_implementation.verify_email_using_token(
@@ -314,8 +314,8 @@ async def email_verify_post(
314314
async def is_email_verified_get(
315315
self,
316316
api_options: APIOptions,
317+
session: Optional[SessionContainer],
317318
user_context: Dict[str, Any],
318-
session: Optional[SessionContainer] = None,
319319
) -> IsEmailVerifiedGetOkResult:
320320
if session is None:
321321
raise Exception("Session is undefined. Should not come here.")
@@ -335,8 +335,8 @@ async def is_email_verified_get(
335335
async def generate_email_verify_token_post(
336336
self,
337337
api_options: APIOptions,
338-
user_context: Dict[str, Any],
339338
session: SessionContainer,
339+
user_context: Dict[str, Any],
340340
) -> Union[
341341
GenerateEmailVerifyTokenPostOkResult,
342342
GenerateEmailVerifyTokenPostEmailAlreadyVerifiedError,

supertokens_python/recipe/emailverification/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ def __init__(
4848

4949

5050
class ParentRecipeEmailVerificationConfig:
51-
# TODO: Rename this? ^
5251
def __init__(
5352
self,
54-
mode: MODE_TYPE = "OPTIONAL",
53+
mode: MODE_TYPE,
5554
email_delivery: Union[EmailDeliveryConfig[EmailTemplateVars], None] = None,
5655
get_email_for_user_id: Optional[TypeGetEmailForUserIdFunction] = None,
5756
create_and_send_custom_email: Union[
@@ -96,6 +95,11 @@ def validate_and_normalise_user_input(
9695
"config must be an instance of ParentRecipeEmailVerificationConfig"
9796
)
9897

98+
if config.mode not in ["REQUIRED", "OPTIONAL"]:
99+
raise ValueError(
100+
"Email Verification recipe mode must be one of 'REQUIRED' or 'OPTIONAL'"
101+
)
102+
99103
def get_email_delivery_config() -> EmailDeliveryConfigWithService[
100104
VerificationEmailTemplateVars
101105
]:

supertokens_python/recipe/passwordless/api/implementation.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,20 +274,18 @@ async def consume_code_post(
274274

275275
if user.email is not None:
276276
ev_instance = EmailVerificationRecipe.get_instance_optional()
277-
assert ev_instance is not None
278-
token_response = (
279-
await ev_instance.recipe_implementation.create_email_verification_token(
277+
if ev_instance is not None:
278+
token_response = await ev_instance.recipe_implementation.create_email_verification_token(
280279
user.user_id,
281280
user.email,
282281
user_context,
283282
)
284-
)
285283

286-
if isinstance(token_response, CreateEmailVerificationTokenOkResult):
287-
await ev_instance.recipe_implementation.verify_email_using_token(
288-
token_response.token,
289-
user_context,
290-
)
284+
if isinstance(token_response, CreateEmailVerificationTokenOkResult):
285+
await ev_instance.recipe_implementation.verify_email_using_token(
286+
token_response.token,
287+
user_context,
288+
)
291289

292290
session = await create_new_session(
293291
api_options.request, user.user_id, {}, {}, user_context=user_context

supertokens_python/recipe/passwordless/recipe.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ async def signinup(
333333
return consume_code_result
334334
raise Exception("Failed to create user. Please retry")
335335

336-
async def get_email_for_user_id(self, user_id: str, user_context: Dict[str, Any]):
336+
async def get_email_for_user_id(
337+
self, user_id: str, user_context: Dict[str, Any]
338+
) -> Union[GetEmailForUserIdOkResult, EmailDoesNotExistError, UnknownUserIdError]:
337339
user_info = await self.recipe_implementation.get_user_by_id(
338340
user_id, user_context
339341
)

supertokens_python/recipe/session/asyncio/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
# under the License.
1414
from typing import Any, Dict, List, Union, TypeVar, Callable, Optional
1515

16-
# TODO: Finalize how to import Optional
17-
1816
from supertokens_python.recipe.openid.interfaces import (
1917
GetOpenIdDiscoveryConfigurationResult,
2018
)
@@ -39,8 +37,6 @@
3937
GetJWKSResult,
4038
)
4139

42-
# TODO: https://github.com/supertokens/supertokens-python/pull/209#discussion_r932049999
43-
# There have been changes to this function as well. See this:
4440
_T = TypeVar("_T")
4541

4642

supertokens_python/recipe/session/claim_base_classes/primitive_claim.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ async def validate(
128128

129129

130130
class PrimitiveClaimValidators(Generic[_T]):
131-
# TODO: We should discuss how someone would override existing validators
132131
def __init__(self, claim: SessionClaim[_T]) -> None:
133132
self.claim = claim
134133

@@ -174,9 +173,6 @@ def add_to_payload_(
174173
def remove_from_payload_by_merge_(
175174
self, payload: JSONObject, user_context: Dict[str, Any]
176175
) -> JSONObject:
177-
# TODO: https://github.com/supertokens/supertokens-python/pull/209#discussion_r931922854
178-
# Not sure if this will actually work in python -> cause there is no diff between "null" and
179-
# "undefined" in python (both are None). So setting this to None is as good as deleting it from the map?
180176
payload[self.key] = None
181177
return payload
182178

supertokens_python/recipe/thirdparty/api/implementation.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,17 @@ async def sign_in_up_post(
164164

165165
if email_verified:
166166
ev_instance = EmailVerificationRecipe.get_instance_optional()
167-
assert ev_instance is not None
168-
token_response = (
169-
await ev_instance.recipe_implementation.create_email_verification_token(
167+
if ev_instance is not None:
168+
token_response = await ev_instance.recipe_implementation.create_email_verification_token(
170169
user_id=signinup_response.user.user_id,
171170
email=signinup_response.user.email,
172171
user_context=user_context,
173172
)
174-
)
175173

176-
if isinstance(token_response, CreateEmailVerificationTokenOkResult):
177-
await ev_instance.recipe_implementation.verify_email_using_token(
178-
token=token_response.token, user_context=user_context
179-
)
174+
if isinstance(token_response, CreateEmailVerificationTokenOkResult):
175+
await ev_instance.recipe_implementation.verify_email_using_token(
176+
token=token_response.token, user_context=user_context
177+
)
180178

181179
user = signinup_response.user
182180
session = await create_new_session(

supertokens_python/recipe/thirdpartypasswordless/recipe.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -318,23 +318,3 @@ def reset():
318318
):
319319
raise Exception(None, "calling testing function in non testing env")
320320
ThirdPartyPasswordlessRecipe.__instance = None
321-
322-
async def get_email_for_user_id(
323-
self, user_id: str, user_context: Dict[str, Any]
324-
) -> str:
325-
user_info = await self.recipe_implementation.get_user_by_id(
326-
user_id, user_context
327-
)
328-
if user_info is None:
329-
raise Exception("Unknown User ID provided")
330-
if user_info.third_party_info is None:
331-
if user_info.email is not None:
332-
return user_info.email
333-
# this is a passwordless user with only a phone number.
334-
# returning an empty string here is not a problem since
335-
# we override the email verification functions above to
336-
# send that the email is already verified for passwordless users.
337-
return ""
338-
if user_info.email is None:
339-
raise Exception("Should never come here")
340-
return user_info.email

tests/auth-react/django3x/mysite/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,22 +265,25 @@ def override_email_verification_apis(
265265
async def email_verify_post(
266266
token: str,
267267
api_options: EVAPIOptions,
268+
session: Optional[SessionContainer],
268269
user_context: Dict[str, Any],
269-
session: Optional[SessionContainer] = None,
270270
):
271271
is_general_error = await check_for_general_error(
272272
"body", api_options.request
273273
)
274274
if is_general_error:
275275
return GeneralErrorResponse("general error from API email verify")
276276
return await original_email_verify_post(
277-
token, api_options, user_context, session
277+
token,
278+
api_options,
279+
session,
280+
user_context,
278281
)
279282

280283
async def generate_email_verify_token_post(
281284
api_options: EVAPIOptions,
282-
user_context: Dict[str, Any],
283285
session: SessionContainer,
286+
user_context: Dict[str, Any],
284287
):
285288
is_general_error = await check_for_general_error(
286289
"body", api_options.request
@@ -290,7 +293,7 @@ async def generate_email_verify_token_post(
290293
"general error from API email verification code"
291294
)
292295
return await original_generate_email_verify_token_post(
293-
api_options, user_context, session
296+
api_options, session, user_context
294297
)
295298

296299
original_implementation_email_verification.email_verify_post = email_verify_post

tests/auth-react/fastapi-server/app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,22 +318,22 @@ def override_email_verification_apis(
318318
async def email_verify_post(
319319
token: str,
320320
api_options: EVAPIOptions,
321+
session: Optional[SessionContainer],
321322
user_context: Dict[str, Any],
322-
session: Optional[SessionContainer] = None,
323323
):
324324
is_general_error = await check_for_general_error(
325325
"body", api_options.request
326326
)
327327
if is_general_error:
328328
return GeneralErrorResponse("general error from API email verify")
329329
return await original_email_verify_post(
330-
token, api_options, user_context, session
330+
token, api_options, session, user_context
331331
)
332332

333333
async def generate_email_verify_token_post(
334334
api_options: EVAPIOptions,
335-
user_context: Dict[str, Any],
336335
session: SessionContainer,
336+
user_context: Dict[str, Any],
337337
):
338338
is_general_error = await check_for_general_error(
339339
"body", api_options.request
@@ -344,8 +344,8 @@ async def generate_email_verify_token_post(
344344
)
345345
return await original_generate_email_verify_token_post(
346346
api_options,
347-
user_context,
348347
session,
348+
user_context,
349349
)
350350

351351
original_implementation_email_verification.email_verify_post = email_verify_post

tests/auth-react/flask-server/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,25 @@ def override_email_verification_apis(
287287
async def email_verify_post(
288288
token: str,
289289
api_options: EVAPIOptions,
290+
session: Optional[SessionContainer],
290291
user_context: Dict[str, Any],
291-
session: Optional[SessionContainer] = None,
292292
):
293293
is_general_error = await check_for_general_error(
294294
"body", api_options.request
295295
)
296296
if is_general_error:
297297
return GeneralErrorResponse("general error from API email verify")
298298
return await original_email_verify_post(
299-
token, api_options, user_context, session
299+
token,
300+
api_options,
301+
session,
302+
user_context,
300303
)
301304

302305
async def generate_email_verify_token_post(
303306
api_options: EVAPIOptions,
304-
user_context: Dict[str, Any],
305307
session: SessionContainer,
308+
user_context: Dict[str, Any],
306309
):
307310
is_general_error = await check_for_general_error(
308311
"body", api_options.request
@@ -312,7 +315,7 @@ async def generate_email_verify_token_post(
312315
"general error from API email verification code"
313316
)
314317
return await original_generate_email_verify_token_post(
315-
api_options, user_context, session
318+
api_options, session, user_context
316319
)
317320

318321
original_implementation_email_verification.email_verify_post = email_verify_post

0 commit comments

Comments
 (0)