2017-11-07 16:11:31 +00:00
|
|
|
import json
|
2018-02-20 11:22:17 +00:00
|
|
|
|
2023-08-25 08:57:24 -07:00
|
|
|
from flask import current_app, redirect, render_template, request, session, url_for
|
2019-05-23 15:27:35 +01:00
|
|
|
from flask_login import current_user
|
2018-02-20 11:22:17 +00:00
|
|
|
from itsdangerous import SignatureExpired
|
|
|
|
|
|
2018-03-19 16:38:57 +00:00
|
|
|
from app import user_api_client
|
2015-12-07 16:56:11 +00:00
|
|
|
from app.main import main
|
|
|
|
|
from app.main.forms import TwoFactorForm
|
2019-05-23 15:27:35 +01:00
|
|
|
from app.models.user import User
|
2021-06-14 11:15:57 +01:00
|
|
|
from app.utils.login import (
|
2021-06-14 12:40:12 +01:00
|
|
|
email_needs_revalidating,
|
2021-06-14 11:15:57 +01:00
|
|
|
log_in_user,
|
|
|
|
|
redirect_to_sign_in,
|
|
|
|
|
redirect_when_logged_in,
|
|
|
|
|
)
|
2024-05-16 10:37:37 -04:00
|
|
|
from notifications_utils.url_safe_token import check_token
|
2017-11-07 16:11:31 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/two-factor-email-sent", methods=["GET"])
|
2017-11-07 16:11:31 +00:00
|
|
|
def two_factor_email_sent():
|
2023-08-25 09:12:23 -07:00
|
|
|
title = "Email resent" if request.args.get("email_resent") else "Check your email"
|
2017-11-07 16:11:31 +00:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/two-factor-email.html",
|
2020-10-09 11:41:24 +01:00
|
|
|
title=title,
|
2023-08-25 09:12:23 -07:00
|
|
|
redirect_url=request.args.get("next"),
|
2017-11-07 16:11:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/email-auth/<token>", methods=["GET"])
|
2020-05-04 12:27:51 +01:00
|
|
|
def two_factor_email_interstitial(token):
|
2023-08-25 09:12:23 -07:00
|
|
|
return render_template("views/email-link-interstitial.html")
|
2020-05-04 12:27:51 +01:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/email-auth/<token>", methods=["POST"])
|
2017-11-07 16:11:31 +00:00
|
|
|
def two_factor_email(token):
|
2023-08-25 09:12:23 -07:00
|
|
|
redirect_url = request.args.get("next")
|
2017-11-07 16:11:31 +00:00
|
|
|
if current_user.is_authenticated:
|
2019-05-23 15:27:35 +01:00
|
|
|
return redirect_when_logged_in(platform_admin=current_user.platform_admin)
|
2017-11-07 16:11:31 +00:00
|
|
|
|
|
|
|
|
# checks url is valid, and hasn't timed out
|
|
|
|
|
try:
|
2023-08-25 09:12:23 -07:00
|
|
|
token_data = json.loads(
|
|
|
|
|
check_token(
|
|
|
|
|
token,
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
current_app.config["EMAIL_EXPIRY_SECONDS"],
|
|
|
|
|
)
|
|
|
|
|
)
|
2018-11-13 10:49:35 +00:00
|
|
|
except SignatureExpired:
|
2023-08-25 09:12:23 -07:00
|
|
|
return render_template(
|
|
|
|
|
"views/email-link-invalid.html", redirect_url=redirect_url
|
|
|
|
|
)
|
2017-11-07 16:11:31 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
user_id = token_data["user_id"]
|
2017-11-07 16:11:31 +00:00
|
|
|
# checks if code was already used
|
2023-08-25 09:12:23 -07:00
|
|
|
logged_in, msg = user_api_client.check_verify_code(
|
|
|
|
|
user_id, token_data["secret_code"], "email"
|
|
|
|
|
)
|
2017-11-07 16:11:31 +00:00
|
|
|
|
|
|
|
|
if not logged_in:
|
2023-08-25 09:12:23 -07:00
|
|
|
return render_template(
|
|
|
|
|
"views/email-link-invalid.html", redirect_url=redirect_url
|
|
|
|
|
)
|
2017-11-07 16:11:31 +00:00
|
|
|
return log_in_user(user_id)
|
2015-12-07 16:56:11 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/two-factor-sms", methods=["GET", "POST"])
|
2016-06-17 11:36:30 +01:00
|
|
|
@redirect_to_sign_in
|
2021-05-14 19:15:12 +01:00
|
|
|
def two_factor_sms():
|
2023-08-25 09:12:23 -07:00
|
|
|
user_id = session["user_details"]["id"]
|
2020-01-27 18:10:45 +00:00
|
|
|
user = User.from_id(user_id)
|
2016-01-27 12:22:32 +00:00
|
|
|
|
|
|
|
|
def _check_code(code):
|
2016-03-30 09:58:10 +01:00
|
|
|
return user_api_client.check_verify_code(user_id, code, "sms")
|
2016-01-27 12:22:32 +00:00
|
|
|
|
|
|
|
|
form = TwoFactorForm(_check_code)
|
2023-08-25 09:12:23 -07:00
|
|
|
redirect_url = request.args.get("next")
|
2015-12-07 16:56:11 +00:00
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2021-06-14 12:40:12 +01:00
|
|
|
if email_needs_revalidating(user):
|
2023-08-25 09:12:23 -07:00
|
|
|
user_api_client.send_verify_code(user.id, "email", None, redirect_url)
|
|
|
|
|
return redirect(url_for(".revalidate_email_sent", next=redirect_url))
|
2021-06-14 12:40:12 +01:00
|
|
|
else:
|
|
|
|
|
return log_in_user(user_id)
|
2016-01-05 17:08:50 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return render_template(
|
|
|
|
|
"views/two-factor-sms.html", form=form, redirect_url=redirect_url
|
|
|
|
|
)
|
2016-03-14 16:30:48 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/re-validate-email", methods=["GET"])
|
2020-01-27 18:10:45 +00:00
|
|
|
def revalidate_email_sent():
|
2023-08-25 09:12:23 -07:00
|
|
|
title = "Email resent" if request.args.get("email_resent") else "Check your email"
|
|
|
|
|
redirect_url = request.args.get("next")
|
|
|
|
|
return render_template(
|
|
|
|
|
"views/re-validate-email-sent.html", title=title, redirect_url=redirect_url
|
|
|
|
|
)
|