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-04-14 12:00:55 +01:00
|
|
|
from notifications_utils.url_safe_token import check_token
|
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
|
2021-03-08 10:01:12 +00:00
|
|
|
from app.models.user import InvitedOrgUser, InvitedUser, User
|
2021-06-14 11:09:42 +01:00
|
|
|
from app.utils.login import redirect_to_sign_in
|
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:
|
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:
|
|
|
|
|
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}
|
|
|
|
|
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)
|
2017-11-10 12:35:21 +00:00
|
|
|
# the user will have a new current_session_id set by the API - store it in the cookie for future requests
|
2023-08-25 09:12:23 -07:00
|
|
|
session["current_session_id"] = user.current_session_id
|
|
|
|
|
organization_id = session.get("organization_id")
|
2019-05-23 15:27:35 +01:00
|
|
|
activated_user = user.activate()
|
|
|
|
|
activated_user.login()
|
2019-01-18 16:13:29 +00:00
|
|
|
|
2021-03-08 12:51:35 +00:00
|
|
|
invited_user = InvitedUser.from_session()
|
2019-01-18 16:13:29 +00:00
|
|
|
if invited_user:
|
|
|
|
|
service_id = _add_invited_user_to_service(invited_user)
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for("main.service_dashboard", service_id=service_id))
|
2019-01-18 16:13:29 +00:00
|
|
|
|
2021-03-08 10:01:12 +00:00
|
|
|
invited_org_user = InvitedOrgUser.from_session()
|
2019-01-18 16:13:29 +00:00
|
|
|
if invited_org_user:
|
2023-07-12 12:09:44 -04:00
|
|
|
user_api_client.add_user_to_organization(invited_org_user.organization, user_id)
|
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:
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for("main.add_service", first="first"))
|
2019-01-18 16:13:29 +00:00
|
|
|
|
|
|
|
|
|
2021-03-08 12:51:35 +00:00
|
|
|
def _add_invited_user_to_service(invitation):
|
2023-08-25 09:12:23 -07:00
|
|
|
user = User.from_id(session["user_id"])
|
2021-03-08 12:51:35 +00:00
|
|
|
service_id = invitation.service
|
2021-03-05 12:19:36 +00:00
|
|
|
user.add_to_service(
|
|
|
|
|
service_id,
|
|
|
|
|
invitation.permissions,
|
|
|
|
|
invitation.folder_permissions,
|
|
|
|
|
invitation.from_user.id,
|
|
|
|
|
)
|
2019-01-18 16:13:29 +00:00
|
|
|
return service_id
|