2024-04-24 14:06:43 -07:00
|
|
|
|
import base64
|
2024-01-24 07:55:14 -08:00
|
|
|
|
import json
|
2024-03-19 13:23:22 -07:00
|
|
|
|
import os
|
2023-12-08 21:43:52 -05:00
|
|
|
|
from datetime import datetime
|
2023-12-04 16:59:05 -05:00
|
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from flask import Blueprint, current_app, jsonify, request
|
2021-03-11 20:26:44 +00:00
|
|
|
|
from itsdangerous import BadData, SignatureExpired
|
2016-02-24 14:01:19 +00:00
|
|
|
|
|
2024-01-24 07:55:14 -08:00
|
|
|
|
from app import redis_store
|
2017-07-19 13:50:29 +01:00
|
|
|
|
from app.config import QueueNames
|
2016-02-25 11:22:36 +00:00
|
|
|
|
from app.dao.invited_user_dao import (
|
2023-12-20 09:51:43 -05:00
|
|
|
|
get_expired_invite_by_service_and_id,
|
2023-11-10 16:14:03 -05:00
|
|
|
|
get_expired_invited_users_for_service,
|
2021-03-11 20:26:44 +00:00
|
|
|
|
get_invited_user_by_id,
|
2021-03-12 12:50:07 +00:00
|
|
|
|
get_invited_user_by_service_and_id,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
get_invited_users_for_service,
|
|
|
|
|
|
save_invited_user,
|
2016-02-25 11:22:36 +00:00
|
|
|
|
)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2024-01-16 07:37:21 -05:00
|
|
|
|
from app.enums import InvitedUserStatus, KeyType, NotificationType
|
2021-03-11 20:26:44 +00:00
|
|
|
|
from app.errors import InvalidRequest, register_errors
|
2024-01-18 10:28:50 -05:00
|
|
|
|
from app.models import Service
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
|
persist_notification,
|
|
|
|
|
|
send_notification_to_queue,
|
|
|
|
|
|
)
|
2016-02-26 12:00:16 +00:00
|
|
|
|
from app.schemas import invited_user_schema
|
2024-04-19 09:27:58 -07:00
|
|
|
|
from app.utils import hilite
|
2024-05-16 10:17:45 -04:00
|
|
|
|
from notifications_utils.url_safe_token import check_token, generate_token
|
2016-02-24 14:01:19 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service_invite = Blueprint("service_invite", __name__)
|
2016-02-24 14:01:19 +00:00
|
|
|
|
|
2021-03-05 20:13:56 +00:00
|
|
|
|
register_errors(service_invite)
|
2016-02-24 14:01:19 +00:00
|
|
|
|
|
2023-12-08 21:43:52 -05:00
|
|
|
|
|
2023-12-04 16:59:05 -05:00
|
|
|
|
def _create_service_invite(invited_user, invite_link_host):
|
2024-04-19 09:14:36 -07:00
|
|
|
|
# TODO REMOVE DEBUG
|
|
|
|
|
|
print(hilite("ENTER _create_service_invite"))
|
|
|
|
|
|
# END DEBUG
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_id = current_app.config["INVITATION_EMAIL_TEMPLATE_ID"]
|
2020-09-15 16:45:24 +01:00
|
|
|
|
|
|
|
|
|
|
template = dao_get_template_by_id(template_id)
|
2023-12-15 13:14:53 -05:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service = Service.query.get(current_app.config["NOTIFY_SERVICE_ID"])
|
2024-04-02 13:18:21 -07:00
|
|
|
|
|
|
|
|
|
|
token = generate_token(
|
|
|
|
|
|
str(invited_user.email_address),
|
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
|
)
|
2024-04-24 14:06:43 -07:00
|
|
|
|
|
|
|
|
|
|
# The raw permissions are in the form "a,b,c,d"
|
|
|
|
|
|
# but need to be in the form ["a", "b", "c", "d"]
|
|
|
|
|
|
data = {}
|
|
|
|
|
|
permissions = invited_user.permissions
|
|
|
|
|
|
permissions = permissions.split(",")
|
2024-05-06 15:35:04 -04:00
|
|
|
|
data["from_user_id"] = str(invited_user.from_user.id)
|
2024-04-24 14:06:43 -07:00
|
|
|
|
data["service_id"] = str(invited_user.service.id)
|
2024-04-25 13:21:26 -07:00
|
|
|
|
data["permissions"] = permissions
|
2024-04-24 14:06:43 -07:00
|
|
|
|
data["folder_permissions"] = invited_user.folder_permissions
|
|
|
|
|
|
data["invited_user_id"] = str(invited_user.id)
|
|
|
|
|
|
data["invited_user_email"] = invited_user.email_address
|
|
|
|
|
|
|
2024-04-02 13:18:21 -07:00
|
|
|
|
url = os.environ["LOGIN_DOT_GOV_REGISTRATION_URL"]
|
|
|
|
|
|
url = url.replace("NONCE", token)
|
2024-04-24 14:06:43 -07:00
|
|
|
|
|
|
|
|
|
|
user_data_url_safe = get_user_data_url_safe(data)
|
|
|
|
|
|
|
|
|
|
|
|
url = url.replace("STATE", user_data_url_safe)
|
2024-04-02 13:18:21 -07:00
|
|
|
|
|
2024-01-22 10:55:09 -08:00
|
|
|
|
personalisation = {
|
|
|
|
|
|
"user_name": invited_user.from_user.name,
|
|
|
|
|
|
"service_name": invited_user.service.name,
|
2024-04-02 13:18:21 -07:00
|
|
|
|
"url": url,
|
2024-01-22 10:55:09 -08:00
|
|
|
|
}
|
2024-01-24 07:55:14 -08:00
|
|
|
|
|
2016-12-20 15:59:31 +00:00
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
|
recipient=invited_user.email_address,
|
2017-01-10 13:41:16 +00:00
|
|
|
|
service=service,
|
2024-03-22 11:18:47 -07:00
|
|
|
|
personalisation={},
|
2024-02-28 12:40:52 -05:00
|
|
|
|
notification_type=NotificationType.EMAIL,
|
2016-12-20 15:59:31 +00:00
|
|
|
|
api_key_id=None,
|
2024-01-18 10:28:50 -05:00
|
|
|
|
key_type=KeyType.NORMAL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
reply_to_text=invited_user.from_user.email_address,
|
2016-12-20 15:59:31 +00:00
|
|
|
|
)
|
2024-01-22 10:55:09 -08:00
|
|
|
|
saved_notification.personalisation = personalisation
|
2024-01-24 07:55:14 -08:00
|
|
|
|
redis_store.set(
|
|
|
|
|
|
f"email-personalisation-{saved_notification.id}",
|
|
|
|
|
|
json.dumps(personalisation),
|
|
|
|
|
|
ex=1800,
|
|
|
|
|
|
)
|
2023-08-25 12:09:00 -07:00
|
|
|
|
send_notification_to_queue(saved_notification, queue=QueueNames.NOTIFY)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
2023-12-04 16:59:05 -05:00
|
|
|
|
|
|
|
|
|
|
@service_invite.route("/service/<service_id>/invite", methods=["POST"])
|
|
|
|
|
|
def create_invited_user(service_id):
|
|
|
|
|
|
request_json = request.get_json()
|
|
|
|
|
|
invited_user = invited_user_schema.load(request_json)
|
|
|
|
|
|
save_invited_user(invited_user)
|
|
|
|
|
|
|
|
|
|
|
|
_create_service_invite(invited_user, request_json.get("invite_link_host"))
|
|
|
|
|
|
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_user)), 201
|
2016-02-25 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-11-10 16:14:03 -05:00
|
|
|
|
@service_invite.route("/service/<service_id>/invite/expired", methods=["GET"])
|
|
|
|
|
|
def get_expired_invited_users_by_service(service_id):
|
|
|
|
|
|
expired_invited_users = get_expired_invited_users_for_service(service_id)
|
|
|
|
|
|
return jsonify(data=invited_user_schema.dump(expired_invited_users, many=True)), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@service_invite.route("/service/<service_id>/invite", methods=["GET"])
|
2016-02-25 11:22:36 +00:00
|
|
|
|
def get_invited_users_by_service(service_id):
|
|
|
|
|
|
invited_users = get_invited_users_for_service(service_id)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_users, many=True)), 200
|
2016-02-25 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@service_invite.route("/service/<service_id>/invite/<invited_user_id>", methods=["GET"])
|
2020-08-17 11:59:55 +01:00
|
|
|
|
def get_invited_user_by_service(service_id, invited_user_id):
|
2021-03-12 12:50:07 +00:00
|
|
|
|
invited_user = get_invited_user_by_service_and_id(service_id, invited_user_id)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_user)), 200
|
2020-08-17 11:59:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@service_invite.route(
|
|
|
|
|
|
"/service/<service_id>/invite/<invited_user_id>", methods=["POST"]
|
|
|
|
|
|
)
|
2016-03-01 13:33:20 +00:00
|
|
|
|
def update_invited_user(service_id, invited_user_id):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
fetched = get_invited_user_by_service_and_id(
|
|
|
|
|
|
service_id=service_id, invited_user_id=invited_user_id
|
|
|
|
|
|
)
|
2016-03-01 13:33:20 +00:00
|
|
|
|
|
2022-05-06 15:52:44 +01:00
|
|
|
|
current_data = dict(invited_user_schema.dump(fetched).items())
|
2016-03-01 13:33:20 +00:00
|
|
|
|
current_data.update(request.get_json())
|
2022-05-06 15:25:14 +01:00
|
|
|
|
update_dict = invited_user_schema.load(current_data)
|
2016-03-01 13:33:20 +00:00
|
|
|
|
save_invited_user(update_dict)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(fetched)), 200
|
2016-03-01 13:33:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-12-04 16:59:05 -05:00
|
|
|
|
@service_invite.route(
|
|
|
|
|
|
"/service/<service_id>/invite/<invited_user_id>/resend", methods=["POST"]
|
|
|
|
|
|
)
|
|
|
|
|
|
def resend_service_invite(service_id, invited_user_id):
|
|
|
|
|
|
"""Resend an expired invite.
|
|
|
|
|
|
|
|
|
|
|
|
This resets the invited user's created date and status to make it a new invite, and
|
|
|
|
|
|
sends the new invite out to the user.
|
|
|
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
|
This ignores the POST data entirely.
|
|
|
|
|
|
"""
|
2023-12-20 09:51:43 -05:00
|
|
|
|
fetched = get_expired_invite_by_service_and_id(
|
2023-12-20 08:15:19 -05:00
|
|
|
|
service_id=service_id,
|
|
|
|
|
|
invited_user_id=invited_user_id,
|
2023-12-04 16:59:05 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
fetched.created_at = datetime.utcnow()
|
2024-01-18 10:28:50 -05:00
|
|
|
|
fetched.status = InvitedUserStatus.PENDING
|
2023-12-04 16:59:05 -05:00
|
|
|
|
|
|
|
|
|
|
current_data = {k: v for k, v in invited_user_schema.dump(fetched).items()}
|
|
|
|
|
|
update_dict = invited_user_schema.load(current_data)
|
2023-12-15 13:14:53 -05:00
|
|
|
|
|
2023-12-04 16:59:05 -05:00
|
|
|
|
save_invited_user(update_dict)
|
|
|
|
|
|
|
|
|
|
|
|
_create_service_invite(fetched, current_app.config["ADMIN_BASE_URL"])
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(data=invited_user_schema.dump(fetched)), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-20 14:38:49 +00:00
|
|
|
|
def invited_user_url(invited_user_id, invite_link_host=None):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
token = generate_token(
|
|
|
|
|
|
str(invited_user_id),
|
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
|
)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
2017-12-20 14:38:49 +00:00
|
|
|
|
if invite_link_host is None:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
invite_link_host = current_app.config["ADMIN_BASE_URL"]
|
2017-12-20 14:38:49 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
return "{0}/invitation/{1}".format(invite_link_host, token)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@service_invite.route("/invite/service/<uuid:invited_user_id>", methods=["GET"])
|
2021-03-11 20:26:44 +00:00
|
|
|
|
def get_invited_user(invited_user_id):
|
|
|
|
|
|
invited_user = get_invited_user_by_id(invited_user_id)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_user)), 200
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@service_invite.route("/invite/service/<token>", methods=["GET"])
|
|
|
|
|
|
@service_invite.route("/invite/service/check/<token>", methods=["GET"])
|
2021-03-11 20:26:44 +00:00
|
|
|
|
def validate_service_invitation_token(token):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
max_age_seconds = 60 * 60 * 24 * current_app.config["INVITATION_EXPIRATION_DAYS"]
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
try:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
invited_user_id = check_token(
|
|
|
|
|
|
token,
|
|
|
|
|
|
current_app.config["SECRET_KEY"],
|
|
|
|
|
|
current_app.config["DANGEROUS_SALT"],
|
|
|
|
|
|
max_age_seconds,
|
|
|
|
|
|
)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
except SignatureExpired:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
errors = {
|
2024-05-15 08:22:04 -07:00
|
|
|
|
"invitation": "Your invitation to Notify.gov has expired. "
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"Please ask the person that invited you to send you another one"
|
|
|
|
|
|
}
|
2021-03-11 20:26:44 +00:00
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
except BadData:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
errors = {
|
|
|
|
|
|
"invitation": "Something’s wrong with this link. Make sure you’ve copied the whole thing."
|
|
|
|
|
|
}
|
2021-03-11 20:26:44 +00:00
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
invited_user = get_invited_user_by_id(invited_user_id)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_user)), 200
|
2024-04-19 14:02:38 -07:00
|
|
|
|
|
|
|
|
|
|
|
2024-04-24 14:06:43 -07:00
|
|
|
|
def get_user_data_url_safe(data):
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
data = base64.b64encode(data.encode("utf8"))
|
|
|
|
|
|
return data.decode("utf8")
|