2021-08-25 08:31:51 +00:00
|
|
|
import requests
|
2021-08-25 13:39:24 +00:00
|
|
|
from starlette.responses import PlainTextResponse
|
|
|
|
from starlette.exceptions import HTTPException
|
2021-08-25 08:31:51 +00:00
|
|
|
|
|
|
|
from auth.authenticate import EmailAuthenticate
|
|
|
|
|
2021-08-25 17:12:01 +00:00
|
|
|
from settings import BACKEND_URL, MAILGUN_API_KEY, MAILGUN_DOMAIN
|
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
|
|
|
|
|
|
|
async def send_auth_email(user):
|
|
|
|
token = await EmailAuthenticate.get_email_token(user)
|
|
|
|
|
|
|
|
to = "%s <%s>" % (user.username, user.email)
|
2021-08-25 13:39:24 +00:00
|
|
|
text = "%s?token=%s" % (AUTH_URL, 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",
|
|
|
|
"text": text
|
|
|
|
}
|
|
|
|
)
|
|
|
|
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:
|
|
|
|
raise HTTPException(500, "invalid url")
|
|
|
|
auth_token, user = await EmailAuthenticate.authenticate(token)
|
|
|
|
return PlainTextResponse(auth_token)
|