def _send_validation_email(user): token = make_token( { "sub": user.username, "mail": user.mail }, audience=Audience.email_validation, ttl=app.config["ACTIVATION_TOKEN_EXPIRATION"], ) email_context = {"token": token, "user": user} email = Message( body=render_template("email-validation.txt", **email_context), html=render_template("email-validation.html", **email_context), recipients=[user.mail], subject=_("Verify your email address"), ) if app.config["DEBUG"]: # pragma: no cover app.logger.debug(email) try: mailer.send(email) except ConnectionRefusedError as e: app.logger.error( f"Impossible to send an address validation email: {e}") flash( _("We could not send you the address validation email, please retry later" ), "danger", )
def forgot_password_ask(): form = ForgottenPasswordForm() if form.validate_on_submit(): username = form.username.data lock = PasswordResetLock(username) valid_until = lock.valid_until() now = datetime.datetime.now() with handle_form_errors(form): if valid_until is not None and now < valid_until: wait_min = int((valid_until - now).total_seconds() / 60) wait_sec = int((valid_until - now).total_seconds() % 60) raise FormError( "non_field_errors", _( 'You have already requested a password reset, you need to wait ' '%(wait_min)s minute(s) and %(wait_sec)s seconds before you can request ' 'another.', wait_min=wait_min, wait_sec=wait_sec, ), ) try: user = User(ipa_admin.user_show(username)) except python_freeipa.exceptions.NotFound: raise FormError( "username", _("User %(username)s does not exist", username=username)) token = PasswordResetToken.from_user(user).as_string() # Send the email email_context = {"token": token, "username": username} email = Message( body=render_template("forgot-password-email.txt", **email_context), html=render_template("forgot-password-email.html", **email_context), recipients=[user.mail], subject="Password reset procedure", ) try: mailer.send(email) except ConnectionRefusedError as e: app.logger.error( f"Impossible to send a password reset email: {e}") flash(_("We could not send you an email, please retry later"), "danger") return redirect(url_for('root')) if app.config["DEBUG"]: # pragma: no cover app.logger.debug(email) lock.store() app.logger.info( f'{username} forgot their password and requested a token') flash( _('An email has been sent to your address with instructions on how to reset ' 'your password'), "success", ) return redirect(url_for('root')) return render_template('forgot-password-ask.html', form=form)
def _send_validation_email(user): token = EmailValidationToken.from_user(user).as_string() email_context = {"token": token, "user": user} email = Message( body=render_template("email-validation.txt", **email_context), html=render_template("email-validation.html", **email_context), recipients=[user.mail], subject=_("Verify your email address"), ) if app.config["DEBUG"]: # pragma: no cover app.logger.debug(email) try: mailer.send(email) except ConnectionRefusedError as e: app.logger.error( f"Impossible to send an address validation email: {e}") flash( _("We could not send you the address validation email, please retry later" ), "danger", )