2021-08-25 08:31:51 +00:00
|
|
|
import requests
|
2022-06-09 10:52:56 +00:00
|
|
|
from starlette.responses import RedirectResponse
|
2021-08-25 13:39:24 +00:00
|
|
|
from starlette.exceptions import HTTPException
|
2021-08-25 08:31:51 +00:00
|
|
|
|
2022-01-13 12:16:35 +00:00
|
|
|
from auth.authenticate import EmailAuthenticate, ResetPassword
|
2021-08-25 08:31:51 +00:00
|
|
|
|
2022-06-23 09:13:27 +00:00
|
|
|
from settings import BACKEND_URL, MAILGUN_API_KEY, MAILGUN_DOMAIN, RESET_PWD_URL, \
|
|
|
|
CONFIRM_EMAIL_URL, ERROR_URL_ON_FRONTEND
|
2021-08-25 08:31:51 +00:00
|
|
|
|
|
|
|
MAILGUN_API_URL = "https://api.mailgun.net/v3/%s/messages" % (MAILGUN_DOMAIN)
|
|
|
|
MAILGUN_FROM = "postmaster <postmaster@%s>" % (MAILGUN_DOMAIN)
|
|
|
|
|
2021-08-25 17:12:01 +00:00
|
|
|
AUTH_URL = "%s/email_authorize" % (BACKEND_URL)
|
2021-08-25 08:31:51 +00:00
|
|
|
|
2022-06-23 09:57:07 +00:00
|
|
|
email_templates = {"confirm_email" : "", "auth_email" : "", "reset_password_email" : ""}
|
|
|
|
|
|
|
|
def load_email_templates():
|
|
|
|
for name in email_templates:
|
|
|
|
filename = "templates/%s.tmpl" % name
|
|
|
|
with open(filename) as f:
|
|
|
|
email_templates[name] = f.read()
|
2022-07-21 11:58:50 +00:00
|
|
|
print("[auth.email] templates loaded")
|
2022-06-23 09:57:07 +00:00
|
|
|
|
2021-08-26 09:24:46 +00:00
|
|
|
async def send_confirm_email(user):
|
2022-06-23 09:57:07 +00:00
|
|
|
text = email_templates["confirm_email"]
|
2022-01-13 12:16:35 +00:00
|
|
|
token = await EmailAuthenticate.get_email_token(user)
|
|
|
|
await send_email(user, AUTH_URL, text, token)
|
2021-08-26 09:24:46 +00:00
|
|
|
|
2021-08-25 08:31:51 +00:00
|
|
|
async def send_auth_email(user):
|
2022-06-23 09:57:07 +00:00
|
|
|
text = email_templates["auth_email"]
|
2021-08-25 08:31:51 +00:00
|
|
|
token = await EmailAuthenticate.get_email_token(user)
|
2022-01-13 12:16:35 +00:00
|
|
|
await send_email(user, AUTH_URL, text, token)
|
|
|
|
|
|
|
|
async def send_reset_password_email(user):
|
2022-06-23 09:57:07 +00:00
|
|
|
text = email_templates["reset_password_email"]
|
2022-01-13 12:16:35 +00:00
|
|
|
token = await ResetPassword.get_reset_token(user)
|
|
|
|
await send_email(user, RESET_PWD_URL, text, token)
|
2021-08-25 08:31:51 +00:00
|
|
|
|
2022-01-13 12:16:35 +00:00
|
|
|
async def send_email(user, url, text, token):
|
2021-08-25 08:31:51 +00:00
|
|
|
to = "%s <%s>" % (user.username, user.email)
|
2022-06-09 10:52:56 +00:00
|
|
|
url_with_token = "%s?token=%s" % (url, token)
|
2022-01-13 12:16:35 +00:00
|
|
|
text = text % (url_with_token)
|
2021-08-25 08:31:51 +00:00
|
|
|
response = requests.post(
|
|
|
|
MAILGUN_API_URL,
|
|
|
|
auth = ("api", MAILGUN_API_KEY),
|
|
|
|
data = {
|
|
|
|
"from": MAILGUN_FROM,
|
|
|
|
"to": to,
|
|
|
|
"subject": "authorize log in",
|
2021-08-26 09:24:46 +00:00
|
|
|
"html": text
|
2021-08-25 08:31:51 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
response.raise_for_status()
|
2021-08-25 13:39:24 +00:00
|
|
|
|
|
|
|
async def email_authorize(request):
|
|
|
|
token = request.query_params.get('token')
|
|
|
|
if not token:
|
2022-06-23 09:13:27 +00:00
|
|
|
url_with_error = "%s?error=%s" % (ERROR_URL_ON_FRONTEND, "INVALID_TOKEN")
|
|
|
|
return RedirectResponse(url = url_with_error)
|
2022-06-09 10:52:56 +00:00
|
|
|
|
2022-06-23 09:13:27 +00:00
|
|
|
try:
|
|
|
|
auth_token, user = await EmailAuthenticate.authenticate(token)
|
|
|
|
except:
|
|
|
|
url_with_error = "%s?error=%s" % (ERROR_URL_ON_FRONTEND, "INVALID_TOKEN")
|
|
|
|
return RedirectResponse(url = url_with_error)
|
2022-06-09 10:52:56 +00:00
|
|
|
|
|
|
|
if not user.emailConfirmed:
|
|
|
|
with local_session() as session:
|
|
|
|
user.emailConfirmed = True
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
response = RedirectResponse(url = CONFIRM_EMAIL_URL)
|
|
|
|
response.set_cookie("token", auth_token)
|
|
|
|
return response
|