2016-03-07 18:18:52 +00:00
|
|
|
import json
|
|
|
|
|
|
2018-02-20 11:22:17 +00:00
|
|
|
from flask import (
|
|
|
|
|
current_app,
|
|
|
|
|
flash,
|
|
|
|
|
redirect,
|
|
|
|
|
render_template,
|
2020-10-09 11:40:28 +01:00
|
|
|
request,
|
2018-02-20 11:22:17 +00:00
|
|
|
session,
|
|
|
|
|
url_for,
|
|
|
|
|
)
|
2016-03-07 18:18:52 +00:00
|
|
|
from itsdangerous import SignatureExpired
|
2016-01-05 17:52:09 +00:00
|
|
|
|
|
|
|
|
from app.main import main
|
2016-01-06 17:37:07 +00:00
|
|
|
from app.main.forms import NewPasswordForm
|
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 log_in_user
|
2024-05-16 10:37:37 -04:00
|
|
|
from notifications_utils.url_safe_token import check_token
|
2016-01-05 17:52:09 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/new-password/<path:token>", methods=["GET", "POST"])
|
2016-01-06 17:37:07 +00:00
|
|
|
def new_password(token):
|
2016-03-07 18:18:52 +00:00
|
|
|
try:
|
2023-08-25 09:12:23 -07:00
|
|
|
token_data = check_token(
|
|
|
|
|
token,
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
current_app.config["EMAIL_EXPIRY_SECONDS"],
|
|
|
|
|
)
|
2016-03-07 18:18:52 +00:00
|
|
|
except SignatureExpired:
|
2023-08-25 09:12:23 -07:00
|
|
|
flash(
|
|
|
|
|
"The link in the email we sent you has expired. Enter your email address to resend."
|
|
|
|
|
)
|
|
|
|
|
return redirect(url_for(".forgot_password"))
|
2016-01-08 15:12:14 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
email_address = json.loads(token_data)["email"]
|
2019-05-23 15:27:35 +01:00
|
|
|
user = User.from_email_address(email_address)
|
2023-08-25 09:12:23 -07:00
|
|
|
if user.password_changed_more_recently_than(json.loads(token_data)["created_at"]):
|
|
|
|
|
flash("The link in the email has already been used")
|
|
|
|
|
return redirect(url_for("main.index"))
|
2016-01-08 15:12:14 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
if request.method == "GET":
|
2021-08-17 16:14:47 +01:00
|
|
|
user.update_email_access_validated_at()
|
|
|
|
|
|
2016-01-06 17:37:07 +00:00
|
|
|
form = NewPasswordForm()
|
2016-01-08 15:12:14 +00:00
|
|
|
|
2016-01-06 17:37:07 +00:00
|
|
|
if form.validate_on_submit():
|
2019-05-23 15:27:35 +01:00
|
|
|
user.reset_failed_login_count()
|
2023-08-25 09:12:23 -07:00
|
|
|
session["user_details"] = {
|
|
|
|
|
"id": user.id,
|
|
|
|
|
"email": user.email_address,
|
|
|
|
|
"password": form.new_password.data,
|
|
|
|
|
}
|
2021-05-12 17:24:47 +01:00
|
|
|
if user.email_auth:
|
2017-11-28 15:25:29 +00:00
|
|
|
# they've just clicked an email link, so have done an email auth journey anyway. Just log them in.
|
|
|
|
|
return log_in_user(user.id)
|
|
|
|
|
else:
|
|
|
|
|
# send user a 2fa sms code
|
2019-05-23 15:27:35 +01:00
|
|
|
user.send_verify_code()
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(
|
|
|
|
|
url_for("main.two_factor_sms", next=request.args.get("next"))
|
|
|
|
|
)
|
2016-01-06 17:37:07 +00:00
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
return render_template(
|
|
|
|
|
"views/new-password.html", token=token, form=form, user=user
|
|
|
|
|
)
|