Files
fast_api_template/{{cookiecutter.project_slug}}/backend/app/app/utils.py
T

112 lines
3.6 KiB
Python
Raw Normal View History

import logging
from datetime import datetime, timedelta
from pathlib import Path
2019-02-23 18:44:29 +04:00
from typing import Optional
import emails
import jwt
from emails.template import JinjaTemplate
from jwt.exceptions import InvalidTokenError
2019-02-23 18:44:29 +04:00
from app.core import config
password_reset_jwt_subject = "preset"
def send_email(email_to: str, subject_template="", html_template="", environment={}):
2019-02-23 18:44:29 +04:00
assert config.EMAILS_ENABLED, "no provided configuration for email variables"
message = emails.Message(
subject=JinjaTemplate(subject_template),
html=JinjaTemplate(html_template),
2019-02-23 18:44:29 +04:00
mail_from=(config.EMAILS_FROM_NAME, config.EMAILS_FROM_EMAIL),
)
2019-02-23 18:44:29 +04:00
smtp_options = {"host": config.SMTP_HOST, "port": config.SMTP_PORT}
if config.SMTP_TLS:
smtp_options["tls"] = True
2019-02-23 18:44:29 +04:00
if config.SMTP_USER:
smtp_options["user"] = config.SMTP_USER
if config.SMTP_PASSWORD:
smtp_options["password"] = config.SMTP_PASSWORD
response = message.send(to=email_to, render=environment, smtp=smtp_options)
logging.info(f"send email result: {response}")
def send_test_email(email_to: str):
2019-02-23 18:44:29 +04:00
project_name = config.PROJECT_NAME
subject = f"{project_name} - Test email"
with open(Path(config.EMAIL_TEMPLATES_DIR) / "test_email.html") as f:
template_str = f.read()
send_email(
email_to=email_to,
subject_template=subject,
html_template=template_str,
2019-02-23 18:44:29 +04:00
environment={"project_name": config.PROJECT_NAME, "email": email_to},
)
2019-02-23 18:44:29 +04:00
def send_reset_password_email(email_to: str, email: str, token: str):
project_name = config.PROJECT_NAME
subject = f"{project_name} - Password recovery for user {email}"
with open(Path(config.EMAIL_TEMPLATES_DIR) / "reset_password.html") as f:
template_str = f.read()
if hasattr(token, "decode"):
use_token = token.decode()
else:
use_token = token
2019-02-23 18:44:29 +04:00
server_host = config.SERVER_HOST
link = f"{server_host}/reset-password?token={use_token}"
send_email(
email_to=email_to,
subject_template=subject,
html_template=template_str,
environment={
2019-02-23 18:44:29 +04:00
"project_name": config.PROJECT_NAME,
"username": email,
"email": email_to,
2019-02-23 18:44:29 +04:00
"valid_hours": config.EMAIL_RESET_TOKEN_EXPIRE_HOURS,
"link": link,
},
)
def send_new_account_email(email_to: str, username: str, password: str):
2019-02-23 18:44:29 +04:00
project_name = config.PROJECT_NAME
subject = f"{project_name} - New account for user {username}"
2019-02-23 18:44:29 +04:00
with open(Path(config.EMAIL_TEMPLATES_DIR) / "new_account.html") as f:
template_str = f.read()
2019-02-23 18:44:29 +04:00
link = config.SERVER_HOST
send_email(
email_to=email_to,
subject_template=subject,
html_template=template_str,
environment={
2019-02-23 18:44:29 +04:00
"project_name": config.PROJECT_NAME,
"username": username,
"password": password,
"email": email_to,
"link": link,
},
)
2019-02-23 18:44:29 +04:00
def generate_password_reset_token(email):
delta = timedelta(hours=config.EMAIL_RESET_TOKEN_EXPIRE_HOURS)
now = datetime.utcnow()
expires = now + delta
exp = expires.timestamp()
encoded_jwt = jwt.encode(
2019-02-23 18:44:29 +04:00
{"exp": exp, "nbf": now, "sub": password_reset_jwt_subject, "email": email},
config.SECRET_KEY,
algorithm="HS256",
)
return encoded_jwt
2019-02-23 18:44:29 +04:00
def verify_password_reset_token(token) -> Optional[str]:
try:
2019-02-23 18:44:29 +04:00
decoded_token = jwt.decode(token, config.SECRET_KEY, algorithms=["HS256"])
assert decoded_token["sub"] == password_reset_jwt_subject
2019-02-23 18:44:29 +04:00
return decoded_token["email"]
except InvalidTokenError:
2019-02-23 18:44:29 +04:00
return None