50 lines
1.9 KiB
Python
50 lines
1.9 KiB
Python
from flask import current_app, render_template
|
|
from flask_mailman import EmailMessage
|
|
from itsdangerous import URLSafeTimedSerializer
|
|
|
|
from common.utils.nginx_utils import prefixed_url_for
|
|
|
|
|
|
def confirm_token(token, expiration=3600):
|
|
serializer = URLSafeTimedSerializer(current_app.config['SECRET_KEY'])
|
|
try:
|
|
email = serializer.loads(token, salt=current_app.config['SECURITY_PASSWORD_SALT'], max_age=expiration)
|
|
except Exception as e:
|
|
current_app.logger.debug(f'Error confirming token: {e}')
|
|
raise
|
|
return email
|
|
|
|
|
|
def send_email(to, subject, template):
|
|
msg = EmailMessage(subject=subject,
|
|
body=template,
|
|
to=[to])
|
|
msg.content_subtype = "html"
|
|
msg.send()
|
|
|
|
|
|
def generate_reset_token(email):
|
|
serializer = URLSafeTimedSerializer(current_app.config['SECRET_KEY'])
|
|
return serializer.dumps(email, salt=current_app.config['SECURITY_PASSWORD_SALT'])
|
|
|
|
|
|
def generate_confirmation_token(email):
|
|
serializer = URLSafeTimedSerializer(current_app.config['SECRET_KEY'])
|
|
return serializer.dumps(email, salt=current_app.config['SECURITY_PASSWORD_SALT'])
|
|
|
|
|
|
def send_confirmation_email(user):
|
|
current_app.logger.debug(f'Sending confirmation email to {user.email}')
|
|
token = generate_confirmation_token(user.email)
|
|
confirm_url = prefixed_url_for('security_bp.confirm_email', token=token, _external=True)
|
|
current_app.logger.debug(f'Confirmation URL: {confirm_url}')
|
|
html = render_template('email/activate.html', confirm_url=confirm_url)
|
|
send_email(user.email, "Confirm your email", html)
|
|
|
|
|
|
def send_reset_email(user):
|
|
token = generate_reset_token(user.email)
|
|
reset_url = prefixed_url_for('security_bp.reset_password', token=token, _external=True)
|
|
html = render_template('email/reset_password.html', reset_url=reset_url)
|
|
send_email(user.email, "Reset Your Password", html)
|