2016-03-17 13:07:52 +00:00
|
|
|
import json
|
|
|
|
|
|
2023-08-25 08:57:24 -07:00
|
|
|
from flask import abort, current_app, flash, redirect, render_template, session, url_for
|
2018-02-20 11:22:17 +00:00
|
|
|
from itsdangerous import SignatureExpired
|
2016-03-17 13:07:52 +00:00
|
|
|
|
2018-02-20 11:22:17 +00:00
|
|
|
from app import user_api_client
|
2015-12-07 16:08:30 +00:00
|
|
|
from app.main import main
|
2016-03-17 13:07:52 +00:00
|
|
|
from app.main.forms import TwoFactorForm
|
2024-04-19 13:53:32 -07:00
|
|
|
from app.models.user import User
|
2021-06-14 11:09:42 +01:00
|
|
|
from app.utils.login import redirect_to_sign_in
|
2024-05-16 10:37:37 -04:00
|
|
|
from notifications_utils.url_safe_token import check_token
|
2016-03-17 13:07:52 +00:00
|
|
|
|
2015-12-04 16:21:01 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/verify", methods=["GET", "POST"])
|
2016-06-17 11:36:30 +01:00
|
|
|
@redirect_to_sign_in
|
2016-01-05 17:08:50 +00:00
|
|
|
def verify():
|
2023-08-25 09:12:23 -07:00
|
|
|
user_id = session["user_details"]["id"]
|
2016-01-19 22:47:42 +00:00
|
|
|
|
2016-03-17 13:07:52 +00:00
|
|
|
def _check_code(code):
|
2023-08-25 09:12:23 -07:00
|
|
|
return user_api_client.check_verify_code(user_id, code, "sms")
|
2016-03-10 14:48:33 +00:00
|
|
|
|
2016-03-17 13:07:52 +00:00
|
|
|
form = TwoFactorForm(_check_code)
|
2016-03-11 16:36:15 +00:00
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
if form.validate_on_submit():
|
2023-08-25 09:12:23 -07:00
|
|
|
session.pop("user_details", None)
|
2022-03-07 11:02:33 +00:00
|
|
|
return activate_user(user_id)
|
2016-01-19 22:47:42 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return render_template("views/two-factor-sms.html", form=form)
|
2016-03-17 13:07:52 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/verify-email/<token>")
|
2016-03-17 13:07:52 +00:00
|
|
|
def verify_email(token):
|
|
|
|
|
try:
|
2017-11-01 14:39:14 +00:00
|
|
|
token_data = check_token(
|
|
|
|
|
token,
|
2023-08-25 09:12:23 -07:00
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
current_app.config["EMAIL_EXPIRY_SECONDS"],
|
2017-11-01 14:39:14 +00:00
|
|
|
)
|
2016-03-17 13:07:52 +00:00
|
|
|
except SignatureExpired:
|
2024-07-25 12:38:33 -07:00
|
|
|
current_app.logger.error("Email link expired #notify-admin-1505")
|
2023-08-25 09:12:23 -07:00
|
|
|
flash(
|
|
|
|
|
"The link in the email we sent you has expired. We've sent you a new one."
|
|
|
|
|
)
|
|
|
|
|
return redirect(url_for("main.resend_email_verification"))
|
2017-11-01 14:39:14 +00:00
|
|
|
|
|
|
|
|
# token contains json blob of format: {'user_id': '...', 'secret_code': '...'} (secret_code is unused)
|
|
|
|
|
token_data = json.loads(token_data)
|
2023-08-25 09:12:23 -07:00
|
|
|
user = User.from_id(token_data["user_id"])
|
2017-11-01 14:39:14 +00:00
|
|
|
if not user:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
if user.is_active:
|
2024-07-24 07:13:34 -07:00
|
|
|
current_app.logger.error(
|
2024-07-25 12:38:33 -07:00
|
|
|
f"User is using an invite link but is already logged in {user.id} #notify-admin-1505"
|
|
|
|
|
)
|
2017-11-01 14:39:14 +00:00
|
|
|
flash("That verification link has expired.")
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for("main.sign_in"))
|
2017-11-01 14:39:14 +00:00
|
|
|
|
2022-03-03 14:09:07 +00:00
|
|
|
if user.email_auth:
|
2023-08-25 09:12:23 -07:00
|
|
|
session.pop("user_details", None)
|
2022-03-07 11:02:33 +00:00
|
|
|
return activate_user(user.id)
|
2022-02-23 17:42:49 +00:00
|
|
|
|
2019-05-23 15:27:35 +01:00
|
|
|
user.send_verify_code()
|
2023-08-25 09:12:23 -07:00
|
|
|
session["user_details"] = {"email": user.email_address, "id": user.id}
|
2024-07-24 07:13:34 -07:00
|
|
|
current_app.logger.info(f"Email verified for user {user.id} #notify-admin-1505")
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for("main.verify"))
|
2017-11-10 12:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def activate_user(user_id):
|
2019-05-23 15:27:35 +01:00
|
|
|
user = User.from_id(user_id)
|
2024-03-22 11:08:49 -07:00
|
|
|
|
2024-04-19 13:53:32 -07:00
|
|
|
# TODO add org invites back in the new way
|
2024-06-07 09:07:42 -07:00
|
|
|
# organization_id = redis_client.get(
|
2024-04-19 13:53:32 -07:00
|
|
|
# f"organization-invite-{user.email_address}"
|
|
|
|
|
# )
|
|
|
|
|
# user_api_client.add_user_to_organization(
|
|
|
|
|
# organization_id.decode("utf8"), user_id
|
|
|
|
|
# )
|
|
|
|
|
organization_id = None
|
2019-01-18 16:13:29 +00:00
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
if organization_id:
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for("main.organization_dashboard", org_id=organization_id))
|
2018-02-19 16:53:29 +00:00
|
|
|
else:
|
2024-04-19 13:53:32 -07:00
|
|
|
activated_user = user.activate()
|
2024-07-24 07:13:34 -07:00
|
|
|
current_app.logger.info(f"Activated user {user.id} #notify-admin-1505")
|
2024-04-19 13:53:32 -07:00
|
|
|
activated_user.login()
|
2024-07-24 07:13:34 -07:00
|
|
|
current_app.logger.info(f"Logged in user {user.id} #notify-admin-1505")
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for("main.add_service", first="first"))
|