Send the code to the email address, provided in the argument or to the email address of the current user, provided during the registration. Args: _email (str, optional): Email Address, where to send the OTP code. Defaults to None. Returns:
(_email: str = None)
| 48 | |
| 49 | |
| 50 | def _send_code_to_email(_email: str = None) -> (bool, int, str): |
| 51 | """ |
| 52 | Send the code to the email address, provided in the argument or to the |
| 53 | email address of the current user, provided during the registration. |
| 54 | |
| 55 | Args: |
| 56 | _email (str, optional): Email Address, where to send the OTP code. |
| 57 | Defaults to None. |
| 58 | |
| 59 | Returns: |
| 60 | (bool, int, str): Returns a set as (failed?, HTTP Code, message string) |
| 61 | If 'failed?' is True, message contains the error |
| 62 | message for the user, else it contains the success |
| 63 | message for the user to consume. |
| 64 | """ |
| 65 | |
| 66 | if not current_user.is_authenticated: |
| 67 | return False, 401, _("Not accessible") |
| 68 | |
| 69 | if _email is None: |
| 70 | _email = getattr(current_user, 'email', None) |
| 71 | |
| 72 | if _email is None: |
| 73 | return False, 401, _("No email address is available.") |
| 74 | |
| 75 | try: |
| 76 | session["mfa_email_code"] = __generate_otp() |
| 77 | subject = getattr(config, 'MFA_EMAIL_SUBJECT', None) |
| 78 | |
| 79 | if subject is None: |
| 80 | subject = _("{} - Verification Code").format(config.APP_NAME) |
| 81 | |
| 82 | send_mail( |
| 83 | subject, |
| 84 | _email, |
| 85 | "send_email_otp", |
| 86 | user=current_user, |
| 87 | code=session["mfa_email_code"] |
| 88 | ) |
| 89 | except OSError as ose: |
| 90 | current_app.logger.exception(ose) |
| 91 | return False, 503, _("Failed to send the code to email.") + \ |
| 92 | "\n" + str(ose) |
| 93 | |
| 94 | message = _( |
| 95 | "A verification code was sent to {}. Check your email and enter " |
| 96 | "the code." |
| 97 | ).format(_mask_email(_email)) |
| 98 | |
| 99 | return True, 200, message |
| 100 | |
| 101 | |
| 102 | def _mask_email(_email: str) -> str: |
no test coverage detected