2022-10-08 00:44:02 +00:00
|
|
|
import requests
|
2022-05-04 23:09:46 +00:00
|
|
|
import time
|
|
|
|
from flask import *
|
|
|
|
from urllib.parse import quote
|
|
|
|
|
|
|
|
from files.helpers.security import *
|
|
|
|
from files.helpers.wrappers import *
|
|
|
|
from files.helpers.const import *
|
2022-05-25 22:26:03 +00:00
|
|
|
from files.helpers.get import *
|
2022-06-15 19:33:21 +00:00
|
|
|
from files.helpers.actions import *
|
2022-05-04 23:09:46 +00:00
|
|
|
from files.classes import *
|
2022-10-08 00:44:02 +00:00
|
|
|
from files.__main__ import app, limiter
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def send_mail(to_address, subject, html):
|
2022-10-08 00:44:02 +00:00
|
|
|
if MAILGUN_KEY == 'blahblahblah': return
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-08 00:44:02 +00:00
|
|
|
url = f"https://api.mailgun.net/v3/{SITE}/messages"
|
|
|
|
|
|
|
|
auth = ("api", MAILGUN_KEY)
|
|
|
|
|
|
|
|
data = {"from": EMAIL,
|
|
|
|
"to": [to_address],
|
|
|
|
"subject": subject,
|
|
|
|
"html": html,
|
|
|
|
}
|
|
|
|
|
|
|
|
requests.post(url, auth=auth, data=data)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def send_verification_email(user, email=None):
|
|
|
|
|
|
|
|
if not email:
|
|
|
|
email = user.email
|
|
|
|
|
2022-06-24 16:15:37 +00:00
|
|
|
url = f"https://{SITE}/activate"
|
2022-05-04 23:09:46 +00:00
|
|
|
now = int(time.time())
|
|
|
|
|
|
|
|
token = generate_hash(f"{email}+{user.id}+{now}")
|
|
|
|
params = f"?email={quote(email)}&id={user.id}&time={now}&token={token}"
|
|
|
|
|
|
|
|
link = url + params
|
|
|
|
|
|
|
|
send_mail(to_address=email,
|
2022-09-04 23:15:37 +00:00
|
|
|
html=render_template("email/email_verify.html",
|
|
|
|
action_url=link,
|
|
|
|
v=user),
|
|
|
|
subject=f"Validate your {SITE_NAME} account email."
|
|
|
|
)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.post("/verify_email")
|
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-08-11 04:05:23 +00:00
|
|
|
def verify_email(v):
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
send_verification_email(v)
|
|
|
|
|
|
|
|
return {"message": "Email has been sent (ETA ~5 minutes)"}
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/activate")
|
|
|
|
@auth_required
|
|
|
|
def activate(v):
|
|
|
|
|
|
|
|
email = request.values.get("email", "").strip().lower()
|
|
|
|
|
|
|
|
if not email_regex.fullmatch(email):
|
|
|
|
return render_template("message.html", v=v, title="Invalid email.", error="Invalid email."), 400
|
|
|
|
|
|
|
|
|
|
|
|
id = request.values.get("id", "").strip()
|
|
|
|
timestamp = int(request.values.get("time", "0"))
|
|
|
|
token = request.values.get("token", "").strip()
|
|
|
|
|
|
|
|
if int(time.time()) - timestamp > 3600:
|
|
|
|
return render_template("message.html", v=v, title="Verification link expired.",
|
2022-09-13 09:59:29 +00:00
|
|
|
message="This link has expired. Visit your settings to send yourself another verification email."), 410
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-05-25 22:26:03 +00:00
|
|
|
user = get_account(id)
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if not validate_hash(f"{email}+{id}+{timestamp}", token):
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
if user.is_activated and user.email == email:
|
|
|
|
return render_template("message_success.html", v=v, title="Email already verified.", message="Email already verified."), 404
|
|
|
|
|
|
|
|
user.email = email
|
|
|
|
user.is_activated = True
|
|
|
|
|
2022-06-15 19:33:21 +00:00
|
|
|
badge_grant(user=user, badge_id=2)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
g.db.add(user)
|
|
|
|
|
|
|
|
return render_template("message_success.html", v=v, title="Email verified.", message=f"Your email {email} has been verified. Thank you.")
|