22
33from aiohttp import web
44from aiohttp .web import RouteTableDef
5+ from common_library .user_messages import user_message
56from servicelib .aiohttp import status
67from servicelib .aiohttp .requests_validation import parse_request_body_as
7- from servicelib .mimetype_constants import MIMETYPE_APPLICATION_JSON
88
99from ....products import products_web
1010from ....products .models import Product
@@ -44,14 +44,11 @@ async def resend_2fa_code(request: web.Request):
4444
4545 user = await _auth_service .get_user_or_none (request .app , email = resend_2fa_ .email )
4646 if not user :
47- raise web .HTTPUnauthorized (
48- text = MSG_UNKNOWN_EMAIL , content_type = MIMETYPE_APPLICATION_JSON
49- )
47+ raise web .HTTPUnauthorized (text = MSG_UNKNOWN_EMAIL )
5048
5149 if not settings .LOGIN_2FA_REQUIRED :
5250 raise web .HTTPServiceUnavailable (
53- text = "2FA login is not available" ,
54- content_type = MIMETYPE_APPLICATION_JSON ,
51+ text = user_message ("2FA login is not available" )
5552 )
5653
5754 # Already a code?
@@ -75,8 +72,14 @@ async def resend_2fa_code(request: web.Request):
7572
7673 # sends via SMS
7774 if resend_2fa_ .via == "SMS" :
75+ user_phone_number = user .get ("phone" )
76+ if not user_phone_number :
77+ raise web .HTTPBadRequest (
78+ text = user_message ("User does not have a phone number registered" )
79+ )
80+
7881 await _twofa_service .send_sms_code (
79- phone_number = user [ "phone" ] ,
82+ phone_number = user_phone_number ,
8083 code = code ,
8184 twilio_auth = settings .LOGIN_TWILIO ,
8285 twilio_messaging_sid = product .twilio_messaging_sid ,
@@ -90,7 +93,7 @@ async def resend_2fa_code(request: web.Request):
9093 "name" : CODE_2FA_SMS_CODE_REQUIRED ,
9194 "parameters" : {
9295 "message" : MSG_2FA_CODE_SENT .format (
93- phone_number = _twofa_service .mask_phone_number (user [ "phone" ] )
96+ phone_number = _twofa_service .mask_phone_number (user_phone_number )
9497 ),
9598 "expiration_2fa" : settings .LOGIN_2FA_CODE_EXPIRATION_SEC ,
9699 },
0 commit comments